-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.asm
1562 lines (1265 loc) · 27.5 KB
/
snake.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[org 0x0100]
jmp start
;defining the variables required
fruitpos: dw 0
dangerousFruit: dw 0
fruitArray: dw 0x0dea, 0x0704, 0x0905, 0x0a06, 0x0b0D, 0x0d0F, 0x02e5
fruit: dw 0x002A
score: dw 0
oldTimerIsr: dd 0
oldKbisr: dd 0
ticks: dw 0
size: dw 20
game_over_flag: db 0
lives: dw 3
direction: db 'r' ;moves in the right direction by default
snake: times 240 dw -1
speed: dw 6
finish: db 'GAME OVER! YOU LOSE!'
won: db 'CONGRATULATIONS! YOU WIN!'
scorestr: db 'Score:'
tlivesstr: db ' Total Lives : '
rlivesstr: db 'Remaining Lives: '
levelstr: db 'Level: '
sizestr: db 'Size: '
tickcount: dw 0
min: dw 4
sec: dw 0
level: dw 0
lvl2: dw 0
lvl3: dw 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;display
a0: db 0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2
a1: db 0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2
a2: db 0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20
a3: db 0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20
a4: db 0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0x20,0x20,0xb2,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2
a5: db 0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0xb2,0x20,0x20,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2
a6: db 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0x20,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20
a7: db 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20
a8: db 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2
a9: db 0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0xb2,0xb2,0xb2,0x20,0x20,0x20,0x20,0xb2,0xb2,0x20,0x20,0x20,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2
a10: db 'Press any key to continue. . .'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
level2:
pusha
mov ax, 0xb800
mov es, ax
mov di,1440
mov cx,10
mov dx,3
hurdles: ;creating hurdles for the second level
mov si,di
j: add si,32
mov ax,[cs:fruitpos]
cmp si,ax
je newfruit
cont:mov word[es:si],0x0fdb
dec dx
cmp dx,0
jge j
mov dx,3
add di,160
loop hurdles
popa
ret
newfruit: mov word[es:si],0x0020
call fruitGeneration
jmp cont
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
level3:
pusha
mov ax, 0xb800
mov es, ax
mov di,1600
mov si,1758
mov cx,20
k1: sub si,2
add di,2
mov ax,[cs:fruitpos]
cmp si,ax
je newfruit
cmp di,ax
je newfruit
mov word[es:di],0x0fdc
mov word[es:si],0x0fdc
loop k1
mov di,2560
mov si,2718
mov cx,20
k2: sub si,2
add di,2
mov ax,[cs:fruitpos]
cmp si,ax
je newfruit
cmp di,ax
je newfruit
mov word[es:di],0x0fdc
mov word[es:si],0x0fdc
loop k2
mov di,380
mov si,420
mov cx,6
k3: add si,160
add di,160
mov ax,[cs:fruitpos]
cmp si,ax
je newfruit
cmp di,ax
je newfruit
mov word[es:di],0x0fdB
mov word[es:si],0x0fdB
loop k3
mov di,3740
mov si,3780
mov cx,6
k4: mov ax,[cs:fruitpos]
cmp si,ax
je newfruit
cmp di,ax
je newfruit
mov word[es:di],0x0fdB
mov word[es:si],0x0fdB
sub si,160
sub di,160
loop k4
popa
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;initializing the required level based on snake size
;and time remaining
_level:
cmp word[level], 1
je checkLevel1
cmp word[lvl2], 1
je checkLevel2
cmp word[lvl3], 1
je checkLevel3
checkLevel1: ;if the size is greater than 240 in the first level, we need to initialize the second level
cmp word[cs:size], 240
jge cmp1
jmp checkTime
checkLevel2:
cmp word[cs:size], 140 ;if the size is greater than 140 in the second level, we start the third one
jge cmp2
jmp checkTime
checkLevel3:
cmp word[cs:size], 100 ;the game is won if the size surpasses 100 in the third level
jge intermediate
checkTime:
cmp word[cs:min], 0 ;if the time ends and the size is lesser than required, a life is decreased
jne samelevel
cmp word[cs:sec], 0
je cmp1
samelevel: ret
cmp1: cmp word[cs:size],240
jl cmp2
mov word[cs:speed], 9
mov word[cs:size], 20
add word[cs:score],50
mov word[cs:min],4
mov word[cs:sec],0
mov word[cs:lvl2],1
inc word[cs:level]
push 2031
call beep_long
call clearScreen
call createSnake
ret
intermediate: jmp cmp3
cmp2: cmp word[cs:lvl2], 1
jne decreaseLife
cmp word[cs:size],140
jl cmp3
mov word[cs:speed],9
mov word[cs:size], 20
add word[cs:score],100
mov word[cs:min],4
mov word[cs:sec],0
mov word[cs:lvl3],1
mov word[cs:lvl2],0
inc word[cs:level]
push 2031
call beep_long
call clearScreen
call createSnake
ret
cmp3: cmp word[cs:lvl3],1
jne decreaseLife
cmp word[cs:size],100
jl samelevel
call gameWon
ret
decreaseLife:
push word 9121
call beep_long
mov byte[direction], 'r'
sub word[lives], 1
mov word[sec],0
mov word[min],4
mov word[speed], 18
call clearScreen
call createSnake
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
clearScreen:
push es
push ax
push cx
push di
mov ax, 0xb800
mov es, ax
xor di, di
mov ax, 0x0020 ;black background
mov cx, 2000
cld
rep stosw
call borders
pop di
pop cx
pop ax
pop es
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alarm:
push bp
mov bp,sp
mov dx,2000 ; Number of times to repeat whole routine.
mov bx,1 ; Frequency value.
mov al, 10110110b ; The Magic Number (use this binary number only)
out 43h, al ; Send it to the initializing port 43h Timer 2.
next: ; This is were we will jump back to 2000 times.
mov ax, bx ; Move our Frequency value into ax.
out 42h, al ; Send LSb to port 42h.
mov al, ah ; Move MSb into al
out 42h, al ; Send MSb to port 42h.
in al, 61h ; Get current value of port 61h.
OR al, 00000011b ; OR al to this value, forcing first two bits high.
out 61h, al ; copy it to port 61h of the PPI chip
; to turn ON the speaker.
mov cx, 2000 ; Repeat loop 20 times
delay: ; here is where we loop back too.
loop delay ; Jump repeatedly to delay until cx = 0
inc bx ; Incrementing the value of bx lowers
; the frequency each time we repeat the
; whole routine
dec dx ; decrement repeat routine count
cmp dx, 0 ; Is dx (repeat count) = to 0
jnz next ; If not jump to next
; and do whole routine again.
; else dx = 0 time to turn speaker OFF
in al,61h ; Get current value of port 61h.
and al,11111100b ; and al to this value, forcing first two bits low.
out 61h,al ; copy it to port 61h of the PPI chip
pop bp
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
beep:
push bp
mov bp,sp
push ax
push bx
push cx
mov al, 182 ; Prepare the speaker for the
out 43h, al ; note.
mov ax, [bp+4] ; Frequency number (in decimal)
; for middle C.
out 42h, al ; Output low byte.
mov al, ah ; Output high byte.
out 42h, al
in al, 61h ; Turn on note (get value from
; port 61h).
or al, 00000011b ; Set bits 1 and 0.
out 61h, al ; Send new value.
mov bx, 15 ; Pause for duration of note.
pause1:
mov cx, 0xfff
pause2:
dec cx
jne pause2
dec bx
jne pause1
in al, 61h ; Turn off note (get value from
; port 61h).
and al, 11111100b ; Reset bits 1 and 0.
out 61h, al ; Send new value.
pop cx
pop bx
pop ax
pop bp
ret 2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
beep_long:
push bp
mov bp,sp
push ax
push bx
push cx
mov al, 182 ; Prepare the speaker for the
out 43h, al ; note.
mov ax, [bp+4] ; Frequency number (in decimal)
; for middle C.
out 42h, al ; Output low byte.
mov al, ah ; Output high byte.
out 42h, al
in al, 61h ; Turn on note (get value from
; port 61h).
or al, 00000011b ; Set bits 1 and 0.
out 61h, al ; Send new value.
mov bx, 15 ; Pause for duration of note.
_pause1:
mov cx, 0xffff
_pause2:
dec cx
jne _pause2
dec bx
jne _pause1
in al, 61h ; Turn off note (get value from
; port 61h).
and al, 11111100b ; Reset bits 1 and 0.
out 61h, al ; Send new value.
pop cx
pop bx
pop ax
pop bp
ret 2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
printnum:
push bp
mov bp, sp
push es
push ax
push bx
push cx
push dx
mov ax, 0xb800
mov es, ax
mov ax, [bp+4]
mov bx, 10
mov cx, [bp+6]
nextdigit: mov dx, 0
div bx
add dl, 0x30
push dx
loop nextdigit
mov cx, [bp+6] ;count of digits passed as parameter
nextpos: pop dx
mov dh, 0x0f
mov [es:di], dx
add di, 2
loop nextpos
pop dx
pop cx
pop bx
pop ax
pop es
pop bp
ret 4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
borders:
push bx
push cx
push dx
push es
push ax
push si
push di
push bp
mov ax, 0xb800
mov es, ax
mov si, 320
mov di, 478
drawVerticalSides:
mov ah, 0x0f
mov al, 176
mov word[es:si], ax
mov word[es:di], ax
add si, 160
add di, 160
cmp di, 3998
jle drawVerticalSides
mov si, 320
mov di, 3840
drawHorizontal:
mov ah, 0x0f
mov al, 176
mov word[es:si], ax
mov word[es:di], ax
add si, 2
add di, 2
cmp si, 480
jne drawHorizontal
;printing the score, size, lives etc
mov ah, 0x13
mov al, 0
mov bh, 0
mov bl, 0x0f
mov dx, 0x0023
mov cx, 6
push cs
pop es
mov bp, sizestr
int 0x10
push 3
push word[size]
mov di, 82
call printnum
mov ah, 0x13
mov al, 0
mov bh, 0
mov bl, 0x0f
mov dx, 0x0000
mov cx, 7
push cs
pop es
mov bp, scorestr
int 0x10
push 4
push word[score]
mov di, 12
call printnum
mov ah, 0x13
mov al, 0
mov bh, 0
mov bl, 0x0f
mov dx, 0x0100
mov cx, 7
push cs
pop es
mov bp, levelstr
int 0x10
push 1
mov di, 176
push word[level]
call printnum
mov ah, 0x13
mov al, 0
mov bh, 0
mov bl, 0x0f
mov dx, 0x0039
mov cx, 17
push cs
pop es
mov bp, tlivesstr
int 0x10
mov ah, 0x13
mov al, 0
mov bh, 0
mov bl, 0x0f
mov dx, 0x0139
mov cx, 17
push cs
pop es
mov bp, rlivesstr
int 0x10
mov ax, 0xb800
mov es, ax
mov cx,3
mov si, 148
l0: mov word[es:si], 0x0f03
add si, 2
mov word[es:si], 0x0f20
add si, 2
loop l0
cmp word[lives], 0
mov cx, [lives]
je return
cmp word[lives], -1
je return
mov si, 308
l2: mov word[es:si], 0x0f03
add si, 2
mov word[es:si], 0x0f20
add si, 2
loop l2
return:
pop bp
pop di
pop si
pop ax
pop es
pop dx
pop cx
pop bx
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
displaySnake: ;for printing the snake
push ax
push si
push di
push cx
push es
mov cx, [size]
sub cx, 1
mov ax, 0xb800
mov es, ax
mov di, 0
mov si, [snake+di]
mov ah, 0x0c
mov al, 176 ;printing head out of the loop since it has a different character
mov word[es:si], ax
add di, 2
mov ah, 0x0c
display: ;printing the rest of the snake in a loop
mov si, [snake+di]
mov al, 219
mov word[es:si], ax
add di, 2
loop display
mov di, [fruitpos] ;printing the fruit
mov cx, [fruit]
mov word[es:di], cx
pop es
pop cx
pop di
pop si
pop ax
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
randomNumberGenerator:
;to calculate the position of fruits
push bp
mov bp, sp
push ax
push dx
push bx
push es
push di
mov ax, 0xb800
mov es, ax
mov al, 00
out 0x70, al
in al, 0x71 ;get current seconds
mov ah, 0
shl ax, 9
mov bx, 3680 ;multiply current seconds with 3680
div bx
mov di, dx
add di, 320 ;add 320 to skip the first two lines
mov ax, 0x01 ;checking whether the number is even, since we can only display on even coordinates, due to the attribute byte
test di, ax
jz isEven
add di, 1
isEven:
mov [bp+4], di
pop di
pop es
pop bx
pop dx
pop ax
pop bp
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
fruitGeneration:
push di
push es
push ax
push si
push cx
push 0
call randomNumberGenerator
pop di ;the position is now in di
mov si, 0
mov cx, [size]
mov ax, 0xb800
mov es, ax
check:
cmp word[es:di], 0x0020 ;if there is anything other than space on video memory, we need
;another location
;since we do not want to print the fruit on the hurdles etc
je printFruit
getAnotherNumber:
push 0
call randomNumberGenerator
pop di
jmp check ;if another number was generated, check all the conditions again
printFruit:
mov al, 00
out 0x70, al
in al, 0x71
mov ah, 0
mov bl, 7
div bl ;now ah contains seconds%7
shr ax, 8
mov si, ax ;choosing one of the fruits from the fruits array
shl si, 1
mov cx, [fruitArray+si]
mov [fruit], cx
mov [fruitpos], di
mov word[es:di], cx
pop cx
pop si
pop ax
pop es
pop di
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
generateDangerousFruit:
pusha
push 0
call randomNumberGenerator
pop di
mov ax, 0xb800
mov es, ax
check2:
cmp word[es:di], 0x0020 ;if there is anything other than space on video memory, we need
;another location
;since we do not want to print the fruit on the hurdles etc
je printFruit2
getAnotherNumber2:
push 0
call randomNumberGenerator
pop di
jmp check2
printFruit2:
mov [dangerousFruit], di
mov si, di
mov word[es:si], 0xC099
popa
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
increaseSize:
push ax
push bx
push si
push di
push cx
call fruitGeneration
mov di, [size]
shl di, 1
sub di, 2 ;last index
mov si, di
sub si, 2 ;second last index
mov ax, [snake+di] ;last element
mov bx, [snake+si]
sub bx, ax ;difference
add word[size], 4
cmp word[size], 240
jge maxSize
add di, 2
mov [snake+di], ax
add [snake+di], bx
mov cx, 3
l3: mov ax, [snake+di]
add di, 2
mov [snake+di], ax
add [snake+di], bx
loop l3
jmp continue
maxSize: call _level
continue:
pop cx
pop di
pop si
pop bx
pop ax
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
gameWon:
push ax
push bx
push cx
push dx
push bp
call alarm
xor ax, ax ;unhooking our interrupts
mov es, ax
mov ax, [oldKbisr]
mov [es:9*4], ax
mov ax, [oldKbisr+2]
mov [es:9*4+2], ax
mov ax, [oldTimerIsr]
mov [es:8*4], ax
mov ax, [oldTimerIsr+2]
mov [es:8*4+2], ax
call clearScreen
mov ah, 0x13 ;printing winning message using bios service
mov al, 0
mov bh, 0
mov bl, 0x4f
mov dx, 0x0F28
mov cx, 25
push cs
pop es
mov bp, won
int 0x10
pop bp
pop dx
pop cx
pop bx
pop ax
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
move: ;for moving the snake
push ax
push di
push si
push es
push cx
push bx
cmp word[lvl2],1
jne nxtcmp
call level2
nxtcmp:
cmp word[lvl3],1
jne ignore
call level3
ignore:
mov ax, 0xb800
mov es,ax
mov ax, [fruitpos]
;conditions to check whether the head of the snake hits some wall or itself
;conditions for when the snake is moving right
mov si, [snake]
cmp byte[direction], 'r'
jne _left
add si, 2
jmp checkFruit
_left:
cmp byte[direction], 'l'
jne _up
sub si, 2
jmp checkFruit
_up:
cmp byte[direction], 'u'
jne _down
sub si, 160
jmp checkFruit
_down:
cmp byte[direction], 'd'
add si, 160
checkFruit:
cmp si, [dangerousFruit]
jne checkHit
push word 9121
call beep_long
mov word[dangerousFruit], -1
cmp word[score], 0
je checkHit
sub word[score], 5
checkHit:
cmp word[es:si], 0x0fcd
je intermediateHit
cmp word[es:si], 0x0fdc
je intermediateHit
cmp word[es:si], 0x0fdb
je intermediateHit
cmp word[es:si], 0x0fb0
je intermediateHit
cmp word[es:si], 0x0fba
je intermediateHit
cmp si, ax
je fruitFound
jmp skip
fruitFound:
add word[score], 10
call displaySnake
push word 1207
call beep
call increaseSize
skip:
mov di, [size]
shl di, 1
sub di, 2 ;last index of snake
shift:
mov ax, [snake+di-2] ;shifting the snake, except the head, to the place of the character before it, i.e the last is moved to the place of the second last and so on
mov [snake+di], ax
sub di, 2
cmp di, 0
jne shift
;now changing the position of the head
cmp byte[direction], 'r'
jne left
right: add word[snake+di], 2
jmp moved
intermediateHit: jmp hit
left: cmp byte[direction], 'l'
jne up
sub word[snake+di], 2
jmp moved
up: cmp byte[direction], 'u'
jne down
sub word[snake+di], 160
jmp moved
down: add word[snake+di], 160
jmp moved
hit:
push word 9121
call beep_long
mov byte[direction], 'r'
sub word[lives], 1
mov word[sec],0
mov word[min],4
mov word[speed], 18
call clearScreen
call createSnake
cmp word[lives], -1
je callGameOver
jmp endMove
callGameOver: call gameOver
jmp endMove
moved:
mov ax, [snake]
mov di, 2
mov cx, [size]
sub cx, 1
compare: cmp ax, [snake+di]
je hit
add di, 2
loop compare
endMove:
pop bx
pop cx
pop es
pop si
pop di