-
Notifications
You must be signed in to change notification settings - Fork 4
/
Snake.asm
executable file
·1167 lines (848 loc) · 17.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
;
;
; Snake.asm
; Snake
;
; Created by Asaf Fisher on 6/5/2015.
; Copyright © 2015 Asaf Fisher. All rights reserved.
;
;
;
;
;========MACROS=======
random macro range
mov ah,00h
int 1Ah
mov ax,dx
xor dx,dx
mov bx, range
div bx
inc dx
endm
;========================================================================
org 100h
MainMenu:
call ClearAllRegAndVars
call DrawMainMenu
call FunctionalMainMenu
pop ax
cmp ax,1
je StartTheGame
cmp ax,2
je MoreOptions
cmp ax,3
je Exit
jmp MainMenu
Exit:
MOV AX, 4C00h
int 21h
MoreOptions:
StartTheGame:
call ClearAllRegAndVars
call SetUpSnake
Step:
call Sleep
;call CheckDirectionChange
call SnakeMove
pop ax
cmp ax,1
je MainMenu
call CheckDirectionChange
;call CheckWin
jmp Step
hlt
;=================================================================================================
proc FunctionalMainMenu
Options:
mov ah,1h ;Check if any key was pressed in the keyboard!
int 16h
jnz UpOrDown;key was pressed!
jmp Options ;key wasent pressed!
UpOrDown:
mov ah,0h;Get the key that was pressed!
int 16h
cmp ah,48h ;up key
je UpKey
cmp ah,50h ;down key
je DownKey
cmp al,13
je OptionClicked
jmp Options
UpKey:
cmp OptionsPosition,1
je Buttom
mov dh,OptionsPosition
mov dl,0
push dx
call ChangeManuPosition
dec OptionsPosition
mov dh,OptionsPosition
mov dl,1
push dx
call ChangeManuPosition
jmp Options
;upper then 1?
Buttom:
mov dh,OptionsPosition ;unmark befor
mov dl,0
push dx
call ChangeManuPosition
mov OptionsPosition,3
mov dh,OptionsPosition ;mark after
mov dl,1
push dx
call ChangeManuPosition
jmp Options
DownKey:
cmp OptionsPosition,3
je Top
mov dh,OptionsPosition
mov dl,0
push dx
call ChangeManuPosition
inc OptionsPosition
mov dh,OptionsPosition
mov dl,1
push dx
call ChangeManuPosition
jmp Options
;lower then 3?
Top:
mov dh,OptionsPosition ;unmark befor
mov dl,0
push dx
call ChangeManuPosition
mov OptionsPosition,1
mov dh,OptionsPosition ;mark after
mov dl,1
push dx
call ChangeManuPosition
jmp Options
OptionClicked:
xor ax,ax
pop [150]
mov al, OptionsPosition
push ax
push [150]
ret
endp FunctionalMainMenu
;chnage the selected menu item...
;get param in stack High- Option kind. Low- 0=true 1=false
proc ChangeManuPosition
pop [150]
pop dx
cmp dh,1
je SC
cmp dh,2
je MOC
cmp dh,3
;Exit Unmark/mark
EC:
cmp dl,1
je ECmark
ECunmark:;unmark
mov dx,ELLocation
mov bp,offset ExitLabelFalse
jmp doneMarking
ECmark:;mark
mov dx,ELLocation
mov bp,offset ExitLabelTrue
jmp doneMarking
;Start unmark/mark
SC:
cmp dl,1
je SCmark
SCunmark:;unmark
mov dx,SLLocation
mov bp,offset StartLabelFalse
jmp doneMarking
SCmark:;mark
mov dx,SLLocation
mov bp,offset StartLabelTrue
jmp doneMarking
;More Options mark/unmark
MOC:
cmp dl,1
je MOCmark
MOCunmark:;unmark
mov dx,OLLocation
mov bp,offset OptionsLabelFalse
jmp doneMarking
MOCmark:;mark
mov dx,OLLocation
mov bp,offset OptionsLabelTrue
doneMarking:
mov bh,0
mov ah,13h
mov al,0
mov bl,10
mov cx,16d
int 10h
push [150]
ret
endp ChangeManuPosition
proc DrawMainMenu
call ClearScreen
TitleMsg:
mov bh,0
mov ah,13h
mov al,0
mov dh,1
mov dl,13
mov bl,10
mov cx,52d
mov bp,offset T1
int 10h
inc dh
mov bp,offset T2
int 10h
inc dh
mov bp,offset T3
int 10h
inc dh
mov bp,offset T4
int 10h
inc dh
mov bp,offset T5
int 10h
inc dh
mov bp,offset T6
int 10h
inc dh
mov bp,offset T7
int 10h
inc dh
mov bp,offset T8
int 10h
inc dh
mov bp,offset T9
int 10h
inc dh
mov bp,offset T10
int 10h
inc dh
mov bp,offset T11
int 10h
mov cx,10
waitsec2:
call Sleep
loop waitsec2
mov bh,0
mov ah,13h
mov al,0
mov dl,32d
add dh,3d
mov bl,10
mov cx,16d
mov bp,offset StartLabelTrue
int 10h
mov SLLocation,dx
add dh,3
mov bp,offset OptionsLabelFalse
int 10h
mov OLLocation,dx
add dh,3
mov bp,offset ExitLabelFalse
int 10h
mov ELLocation,dx
;call SetPoint
ret
endp DrawMainMenu
proc SetUpSnake
call ClearScreen
mov dl,0 ;set cursor to 0,0
mov dh,0
BuildField:
;Drow the top border
T_draw:
mov bh, 0
mov ah, 0x2
int 0x10 ;set mod
; dh = y
; dl = x
mov dl,0
mov dh,0
mov cx, 80 ; print chars
mov bh, 0
mov bl, 20d ; green bg/blue fg
mov al, ' ';'*';0x20 ; blank char
mov ah, 0x9
int 0x10
L_draw: ;draw the left border
inc dh
mov cl,' '
mov ch,20d
push dx
push cx
push dx
call SetPoint
pop dx
cmp dh,25d
jne L_draw
mov dl,79
mov dh,0
R_draw: ;draw the right border
inc dh
mov cl,' '
mov ch,20d
push dx
push cx
push dx
call SetPoint
pop dx
cmp dh,25d
jne R_draw
B_draw: ;draw the buttom border
mov bh, 0
mov ah, 0x2
int 0x10
mov dl,0
mov dh,24
mov ah, 0x2
int 0x10
mov cx, 80 ; print chars
mov bh, 0
mov bl, 20d ; green bg/blue fg
mov al, ' ';'*';0x20 ; blank char
mov ah, 0x9
int 0x10
;place the snake...
mov cl,Snake_Shape
mov ch,Snake_Color
push cx
mov dh,Head_Y
mov dl,Head_X
push dx
call SetPoint
;place the snake food...
call GenerateNewFood
ret
endp SetUpSnake
proc ClearScreen
ClearField:
mov dl,0 ;set cursor to 0,0
mov dh,0
clear: ;clear the console...
mov bh, 0
mov ah, 0x2
int 0x10
mov cx, 80 ; print chars
mov bh, 0
mov bl, 00d ; green bg/blue fg
mov al, 0;'*';0x20 ; blank char
mov ah, 0x9
int 0x10
inc dh
cmp dh,25d
jne clear
ret
endp ClearScreen
;===========================TESTS============================
;proc SetPoint;Use stack to cx ch=Colors cl= Letters Ascii
; pop [160]
; pop bx ; Place
; mov al,80d
; mul bl
; add al,bh
; mov bx,ax
;
; mov ch,0ah
; mov cl,'*'
; mov ax,0B800h ; Letter Color
; mov es,ax ; B|ack ground
; ; ||
; mov es:bx,cx; ||
; push [160] ; 0c31h
; ret ; ||
;endp SetPoint ; Letter
;proc SetPoint
; pop [150]
; pop dx;place
; ; dh = y
; ; dl = x Set cursor to top left-most corner of screen
; mov bh, 0
; mov ah, 0x2
; int 0x10
;
; mov cx, 1 ; print chars
; mov bh, 0
; mov bl, Snake_Color ; green bg/blue fg
; mov al, Snake_Shape;'*';0x20 ; blank char
; mov ah, 0x9
; int 0x10
; push [150]
; ret
;endp SetPoint
;===========================TESTS==============================
proc SetPoint
; Get params to stack, first parameter that you need to push
; is the shape and the second one is the location...
pop [150]
pop dx;place dh = y; dl = x
pop cx ;cl Shape, ch Color
;change the dh and dl to one number algorithem...
mov al,dh;mov al y value
mov bl,80d
mul bl
add ax,ax
add dl,dl
mov dh,0
add ax,dx
mov bx,ax
push ds ;move data seg to 0b800h
;mov cl,Snake_Shape;shape
;mov ch,Snake_Color ;color
mov ax, 0b800h
mov ds,ax
;mov bx,1
mov [bx],cx
pop ds
push [150]
ret
endp SetPoint
proc Sleep
pusha
MOV CX, 02H;0fh
MOV DX, 4240H
MOV AH, 86H
INT 15H
popa
ret
endp Sleep
proc GetPoint;Return the char in certain place get value from stack and return to stack high-Color Low-Shape
pop [150]
pop dx;place dh = y; dl = x
push [150]
mov al,dh;mov al y value
mov bl,80d
mul bl
add ax,ax
add dl,dl
mov dh,0
add ax,dx
mov bx,ax
push ds
mov ax, 0b800h
mov ds,ax
;mov bx,1
mov cx,[bx]
pop ds
pop [150]
push cx
push [150]
ret
endp GetPoint
;WORNING- May be a problem with Turns array problem and his word size [si+2]
proc CheckDirectionChange
;check if keyboard clicked
mov ah,1h ;Check if any key was pressed in the keyboard!
int 16h
jnz getKey;key was pressed!
jmp Knone;key wasent pressed!
getKey:
mov ah,0h;Get the key that was pressed!
int 16h
;compiration with al and the key that you need
cmp al,'w'
je Kup
cmp al,'a'
je Kleft
cmp al,'d'
je Kright
cmp al,'s'
je Kdown
cmp al,20h
jne Knone
pop dx
jmp MainMenu
Kup:
cmp HeadDirection,1
je Knone
mov HeadDirection,1
jmp performDataTurn
Kright:
cmp HeadDirection,2
je Knone
mov HeadDirection,2
jmp performDataTurn
Kdown:
cmp HeadDirection,3
je Knone
mov HeadDirection,3
jmp performDataTurn
Kleft:
cmp HeadDirection,4
je Knone
mov HeadDirection,4
performDataTurn:
;========Turns Alg=========
mov si,[Turns_Length]
mov al,HeadDirection
mov Turns[si],al
add si,si
mov al,Head_X
mov ah,Head_Y
mov Locations[si],ax
inc Turns_Length
;===========================
Knone:
ret
endp CheckDirectionChange
;working Snake eatten might be an error with labels location....
proc SnakeMove
;============
;mov al,Snake_Shape
; mov Blank, al
; mov Snake_Shape,' '
;
; mov bl,00
;
cmp IsSnakeEatten,1;check is snake eatten
je skip
cmp Turns_Length,0
je RemoveTail
mov si,0;Current Turn...
mov ax,Locations[si]
cmp Tail_X,al
jne RemoveTail
cmp Tail_Y,ah
jne RemoveTail
ChangeTailDirection:
;up
mov al,Turns[si]
mov TailDirection,al
RemoveTurnFromArray:
mov si,0
cmp Turns_Length,1
ja conti
mov Turns[si],00h
mov Locations[si],0000h
dec Turns_Length
jmp RemoveTail
conti:
mov cx,Turns_Length
dec cx
SortLocationsArray:
mov ax,Locations[si+2]
mov Locations[si],ax
add si,2
loop SortLocationsArray
mov si,0
mov cx,Turns_Length
dec cx
SortTurnsArray:
mov al,Turns[si+1]
mov Turns[si],al
inc si
loop SortTurnsArray
dec Turns_Length
RemoveTail:
mov cl,0
mov ch,0
push cx
mov dh,Tail_Y
mov dl,Tail_X
push dx
call SetPoint
mov IsSnakeEatten,0
cmp TailDirection,1
je MoveTailUp
cmp TailDirection,2
je MoveTailRight
cmp TailDirection,3
je MoveTailDown
cmp TailDirection,4
je MoveTailLeft
MoveTailUp:
dec Tail_Y
jmp skip
MoveTailRight:
inc Tail_X
jmp skip
MoveTailDown:
inc Tail_Y
jmp skip
MoveTailLeft:
dec Tail_X
skip:
cmp IsSnakeEatten,1
je IncSnake
jmp next
IncSnake:
mov IsSnakeEatten,0
inc Snake_Size
next:
;
; push bx
; push dx
; call SetPoint
;
; mov al,Blank
; mov Snake_Shape,al
;============
cmp HeadDirection,1
je up
cmp HeadDirection,2
je right
cmp HeadDirection,3
je down
cmp HeadDirection,4
je left
up:;UP
dec Head_Y
jmp move
right:;RIGHT
inc Head_X
jmp move
down:;DOWN
inc Head_Y
jmp move
left:;LEFT
dec Head_X
move:
;Check if snake eaten...
call IsSnakeEaten
;Check if game lost...
call CheckLose
pop ax
cmp ax,1
je GameLost
mov cl,Snake_Shape
mov ch,Snake_Color
push cx
mov dh,Head_Y
mov dl,Head_X
push dx
call SetPoint
jmp PointDone
GameLost:
call GameOver
pop [150]
push 1
push [150]
ret
PointDone:
pop [150]
push 0
push [150]
ret
endp SnakeMove
proc IsSnakeEaten
xor ax,ax
mov dh,Head_Y
mov dl,Head_X
push dx
call GetPoint
pop cx
cmp cl,S_Berry
je berry
cmp cl,S_Apple
je apple
cmp cl,S_Waffels
je waffels
jmp none
berry:
mov al,P_Berry
add Points,ax
jmp eated
apple:
mov al,P_Apple
add Points,ax
jmp eated
waffels:
mov al,P_Waffels
add Points,ax
eated:
mov IsSnakeEatten,1
call GenerateNewFood
none:
ret
endp IsSnakeEaten
proc GenerateNewFood
getY:
random Field_Y
mov T_F_Y,dl
cmp T_F_Y,0
je getY
cmp T_F_Y,24d
jae getY
getX:
random Field_X
mov T_F_X,dl
cmp T_F_X,80d
je getX
random 3d
cmp dl,1
je PlaceBerry
cmp dl,2
je PlaceApple
cmp dl,3
je PlaceWaffels
PlaceBerry:
mov ch,C_Berry;mov food color
mov cl,S_Berry;mov food shape
jmp place
PlaceApple:
mov ch,C_Apple
mov cl,S_Apple
jmp place
PlaceWaffels:
mov ch,C_Waffels
mov cl,S_Waffels
place:
mov dh,T_F_Y
mov dl,T_F_X
push cx;shape,color
push dx;x,y
call SetPoint
ret
endp
proc CheckLose
mov dh,Head_Y
mov dl,Head_X
push dx
call GetPoint
pop cx
cmp cl,20h
je Lost
cmp cl,2ah
je Lost
jmp Continue
Lost:
pop [150]
push 1
push [150]
ret
Continue:
pop [150]
push 0
push [150]
ret
endp CheckLose
proc GameOver
mov bh,0
mov ah,13h
mov al,0
mov dh,10
mov dl,10
mov bl,10
mov cx,50d
mov bp,offset MSG_GameOver1
int 10h
inc dh
mov bp,offset MSG_GameOver2
int 10h
inc dh
mov bp,offset MSG_GameOver3
int 10h
inc dh
mov bp,offset MSG_GameOver4
int 10h
mov cx,5
waitsec:
call Sleep
loop waitsec