-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_v1.asm
829 lines (699 loc) · 17.2 KB
/
snake_v1.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
IDEAL
MODEL small
STACK 100h
DATASEG
;__________________Data_________________________
; Flags:
disqualified db 0 ; 0 if you deid ==> True
; Varialbes
; snake
color dw 02 ; 1
transform_shape dw 1 ; 3 ^,v,>,<
last_movement dw 0 ; 5
invalid_input dw 0 ; 7
snake_len dw 1 ; 9
new_pos dw ((12*80)+40)*2 ; 11 B
; Apple
apple_pose dw 0 ; 13 D
; Snake list
list_of_positions dw ((12*80)+40)*2 ; 15 F
dw 1999 dup (0)
;list
color_list dw 01
;_________________________________________________
CODESEG
;________________________________________Zone-4 low level functions______________________________________
;--------------------------form: create_apple----------------------
;input: None - (push dx)
;output: random place (si)
proc random_generate_0_3998
push bp
mov bp, sp
push es ; mess with es
push ax ; Mathematical operations
push si ; Multiplier
push dx ; holds the dosbox clock, and the Module = answer
push cx ; black box
;------------------
mov ah, 0 ; takes the value in dosbox clock and put it in dx and cx
int 1ah
;------------------
mov ax, dx ; ax = clock time
; add xor with random data for full random:
mov bx, cx
xor ax, [bx]
;------------------
mov si, 2
mul si ; ax = clock number even
;------------------
;------------------
mov si, 4000
div si ; dx = module of 4,000.
;------------------
mov [bp + 4], dx ; dx = even number between 0 to 3998
pop cx
pop dx
pop si
pop ax
pop es
pop bp
ret
endp random_generate_0_3998
;-------------------------------------------
;--------------from: initial----------------
proc clear_screen
push di ; hold clear position
push cx ; loop counter
push ax ; draw properties
mov di, 0
mov cx, 3fffh ; half the positions on the screen
;--------------------------------
loopp:
;---------------
mov al, ' '
mov ah, 0
mov[es:di], ax
;---------------
add di, 2 ; every poxle is to palces, thus +2.
loop loopp
;--------------------------------
pop ax
pop cx
pop di
ret
endp clear_screen
;-------------------------------------------
;________________________________________________________________________________________________________
;________________________________________Zone-3 mid level functions______________________________________
;________________________________________from: graphical_update__________________________________________
;-------------------create apple-------------------
; input - pointer apple_pose (+4)
; output - none
proc creat_apple
push bp
mov bp, sp
push bx ; the atribute in the random place
push si ; random palce in the textul screen
reganerate:
;---------------
push bx
call random_generate_0_3998 ;
pop si
;---------------
;---------------
mov bx, [es:si] ; make dx to have the atribute of the si cell
cmp bl, " "
jne reganerate ; if the cell is full, Than rganerate random number
;---------------
;------------------------------
mov bl, 162 ; the apple simbol
mov bh, 04
mov[es:si], bx ; make the apple position si and draw it.
;----------------------------
mov bx, [bp + 4]
mov [word ptr bx], si ; make the apple position si
pop si
pop bx
pop bp
ret 2
endp creat_apple
;-------------------------------------------------------------
;----------------------shift-----------------------
; input: the_new_place (+8), offset list_of_positions (+6), [snake_length] (+4)
; output: None
proc shift
push bp
mov bp, sp
push cx ; number of loops we go to the list
push bx ; index of positions
push ax ; transference register
;-----------------
mov bx, [bp + 4] ; snake_length
mov cx, [bp + 4]
;------------------
shl bx, 1
add bx, [bp + 6] ; bx = last list index
cmp cx, 0
je not_shifted
;------------------
go_backwords: ; the loop goes from the tail to the head and update the positions
sub bx, 2
mov ax, [bx]
mov [bx + 2], ax
loop go_backwords
;------------------
mov ax, [bp + 8] ; add the new place in the first place in the list
mov [bx], ax
not_shifted:
pop ax
pop bx ; black box
pop cx
pop bp
ret 6
endp shift
;---------------------------------------------------
;-------------------------redraw_snake----------------------------
; input: snake_len(+8), p list_of_positions(+6), p color_list(+4)
proc redraw_snake
push bp
mov bp, sp
push bx
push cx
push ax
push di
push si
;---------------
mov cx, [bp + 8] ; cx = snake_len
dec cx
mov bx, [bp + 6] ; bx = p list_of_positions
mov di, [bp + 4] ; di = p color_list
;---------------
;-------draw head-------
mov al, 09
mov ah, [byte ptr transform_shape]
mov si, [bx]
mov [es:si], ax
add bx, 2
;------------------------
cmp [snake_len], 1
je no_body
;____________________________________
draw_body:
mov al, 09 ;[byte ptr transform_shape];09;
mov ah, [byte ptr di] ; acording to the color_list
mov si, [bx]
mov [es:si], ax
add bx, 2
add di, 2
loop draw_body
;____________________________________
no_body:
;-----delet pixle---------
mov al, ' '
mov ah, 0
mov si, [bx]
mov [es:si], ax
;------------------------
pop si
pop di
pop ax
pop cx
pop bx
pop bp
ret 6
endp redraw_snake
;---------------------------------------------------------------------------------------
;_______________________________________________________________________________________________________
;_____________________________________from: discualfied_update__________________________________________
;----------------
proc touch_edges
push bp
mov bp, sp
push ax
push bx
push cx
;------------------------
mov ax, [bp + 6] ; first list of positions value
mov bx, [bp + 4] ; p disqualified
mov cl, 160
;------------------------
; Up
cmp ax, 158
jl touch
; Down
cmp ax, (24*80)*2
ja touch
; Left
div cl
cmp ah, 0
je touch
;Right
cmp ah, 158
je touch
jmp not_touch
touch:
mov [byte ptr bx], 1
not_touch:
pop cx
pop bx
pop ax
pop bp
ret 4
endp touch_edges
;----------------
; input: list_of_positions - (+8)(bx), [snake_len](+6)
; , disqualified (+4)
; output: None
;--------------Ate Himself-------------
proc ate_himself
push bp
mov bp, sp
push bx
push cx ; loop counter
push di
push si ; pointer to the body blocks values
push dx ; holds the snake head position
;------------------------
mov bx, [bp + 8] ; bx = poniter list_of_positions
mov cx, [bp + 6] ; cx = [snake_len]
mov di, [bp + 4] ; di = pointer disqualified
;------------------------
mov dx, [bx] ; dx = head position
mov si, bx ; si = body pointer
add si, 2 ; pointer body
shl cx, 1
add cx, bx ; cx = tail pointer
;____________________________________
check_list:
cmp dx, [si] ;checks if the head is the same coordinates as part body
je died
add si, 2 ; go to the next coordinates
cmp si, cx
jl check_list
;____________________________________
;---------------------------
jmp not_died
died:
mov [byte ptr di], 1
not_died:
;---------------------------
pop dx
pop si
pop di
pop cx
pop bx
pop bp
ret 6
endp ate_himself
;------------------------------
;----------------delay----------------
; input: None
; output: None
proc delay
push cx
mov cx, 0ffffh
;--------------------------------
out_loop:
push cx
mov cx, 100
;----------------
inloop:
loop inloop
;----------------
pop cx
loop out_loop
;--------------------------------
pop cx ; black box
ret ; retrun the ip in order to
endp delay
;--------------------------------------------------
;________________________________________________________________________________________________________
;________________________________________Zone-2 The Three Main Function__________________________________
;_________________F3 = grafical_update___________________
; input: p apple_pose(+14), color_list(+12), p_transform_shape(+10), p_new_pos(+8),
; p_list_of_positions(+6), p_snake_len(+4)
proc graphical_update
push bp
mov bp, sp
push bx
push ax
;----------------
mov bx, [bp + 8]
push [word ptr bx] ; new_pos
;----------------
push [bp + 6] ; p list_of_positions
;----------------
mov bx, [bp + 4]
push [word ptr bx] ; snake len
call shift
;_____________eat apple________________
mov bx, [bp +6]
mov ax, [word ptr bx] ; ax = list_of_positions first value
mov bx, [bp + 14] ; pointer apple_pose
cmp ax, [word ptr bx]
jne not_aten
mov bx, [bp + 4]
add [word ptr bx], 1 ; increase snake_len
;---------------------
mov bx, [bp + 10] ; v transform
push [bx]
push [bp + 12] ; offset color_list
mov bx, [bp + 4]
push [bx] ; snake_len
call shift
;---------------------
mov bx, [bp + 4]
push [bp + 14]
call creat_apple
;---------------------
not_aten:
;---------------------
mov bx, [bp + 4]
push [bx] ; snake_len
push [bp + 6]
push [bp + 12]
call redraw_snake
;---------------------
pop ax
pop bx
pop bp
ret 12
endp graphical_update
;________________________________________________________
;_______________F2 = disqualified_update_________________
proc disqualified_update
push bp
mov bp, sp
push bx
mov bx, [bp + 8] ; p new position
;---------------------
push [word ptr bx] ; new pos value
push [bp + 4] ; p disqualified
call touch_edges
;---------------------
mov bx, [bp + 6] ; p snake_len
push [bp + 8] ; p list_of_positions
push [word ptr bx] ; [snake_len]
push [bp + 4] ; p disqualified
call ate_himself
;---------------------
call delay
pop bx
pop bp
ret 6
endp disqualified_update
;______________________________________________________
;_______________F1 = <move name>_update_________________
; input:
; P_TRANSFORM_SHAPE 10
; P_LAST_MOVEMENT 8
; P_INVAILD_INPUT 6
; P_NEW_POS 4
;--------------------------------------------
proc up_update
push bp
mov bp, sp
push bx
mov bx, [bp + 10]
; add [word ptr bx], 1 ; head color
mov bx, [bp + 8]
mov [word ptr bx], 'w' ; last movement
mov bx, [bp + 6]
mov [word ptr bx], 's' ; invalid input
mov bx, [bp + 4]
sub [word ptr bx], 160 ; snake move
pop bx
pop bp
ret 8
endp up_update
;--------------------------------------------
;--------------------------------------------
proc down_update
push bp
mov bp, sp
push bx
mov bx, [bp + 10]
; add [word ptr bx], 1 ; head color
mov bx, [bp + 8]
mov [word ptr bx], 's' ; last movement
mov bx, [bp + 6]
mov [word ptr bx], 'w' ; invalid input
mov bx, [bp + 4]
add [word ptr bx], 160 ; snake move
pop bx
pop bp
ret 8
endp down_update
;--------------------------------------------
;--------------------------------------------
proc left_update
push bp
mov bp, sp
push bx
mov bx, [bp + 10]
; add [word ptr bx], 1 ; head color
mov bx, [bp + 8]
mov [word ptr bx], 'a' ; last movement
mov bx, [bp + 6]
mov [word ptr bx], 'd' ; invalid input
mov bx, [bp + 4]
sub [word ptr bx], 2 ; snake move
pop bx
pop bp
ret 8
endp left_update
;--------------------------------------------
;--------------------------------------------
proc right_update
push bp
mov bp, sp
push bx
mov bx, [bp + 10]
; add [word ptr bx], 1 ; head color
mov bx, [bp + 8]
mov [word ptr bx], 'd' ; last movement
mov bx, [bp + 6]
mov [word ptr bx], 'a' ; invalid input
mov bx, [bp + 4]
add [word ptr bx], 2 ; snake move
pop bx
pop bp
ret 8
endp right_update
;_______________________________________________________
;_______________________________________________________________________________________
;______________________________________Root Zone-1________________________________________
; input: data pointers: for more details look for define
; output: None
;--------------------------------------
; define input:
P_INVALID_INPUT equ [bp + 4]
P_TRANSFORM_SHAPE equ [bp + 6]
P_LIST_OF_POSITIONS equ [bp + 8]
P_LAST_MOVEMENT equ [bp + 10]
P_NEW_POS equ [bp + 12]
P_SNAKE_LEN equ [bp + 14]
P_APPLE_POSE equ [bp + 16]
P_DISQUALIFIED equ [bp + 18]
P_COLOR_LIST equ [bp + 20]
;-------------------------------------------
proc up
push bp
mov bp, sp
cmp al, [byte ptr last_movement]
je change_head_color
add [transform_shape], 1
change_head_color:
;---------------------
push P_TRANSFORM_SHAPE
push P_LAST_MOVEMENT
push P_INVALID_INPUT
push P_NEW_POS
call up_update
;----------------------
push P_APPLE_POSE
push P_COLOR_LIST
push P_TRANSFORM_SHAPE
push P_NEW_POS
push P_LIST_OF_POSITIONS
push P_SNAKE_LEN
call graphical_update
;---------------------
;---------------------
push P_LIST_OF_POSITIONS
push P_SNAKE_LEN
push P_DISQUALIFIED
call disqualified_update
;----------------------
pop bp
ret 18
endp up
;-------------------------------------------
;-------------------------------------------
proc down
push bp
mov bp, sp
cmp al, [byte ptr last_movement]
je change_head_color1
add [transform_shape], 1
change_head_color1:
;---------------------
push P_TRANSFORM_SHAPE
push P_LAST_MOVEMENT
push P_INVALID_INPUT
push P_NEW_POS
call down_update
;----------------------
push P_APPLE_POSE
push P_COLOR_LIST
push P_TRANSFORM_SHAPE
push P_NEW_POS
push P_LIST_OF_POSITIONS
push P_SNAKE_LEN
call graphical_update
;---------------------
;---------------------
push P_LIST_OF_POSITIONS
push P_SNAKE_LEN
push P_DISQUALIFIED
call disqualified_update
;----------------------
pop bp
ret 18
endp down
;-------------------------------------------
proc left
push bp
mov bp, sp
cmp al, [byte ptr last_movement]
je change_head_color2
add [transform_shape], 1
change_head_color2:
;---------------------
push P_TRANSFORM_SHAPE
push P_LAST_MOVEMENT
push P_INVALID_INPUT
push P_NEW_POS
call left_update
;----------------------
push P_APPLE_POSE
push P_COLOR_LIST
push P_TRANSFORM_SHAPE
push P_NEW_POS
push P_LIST_OF_POSITIONS
push P_SNAKE_LEN
call graphical_update
;---------------------
;---------------------
push P_LIST_OF_POSITIONS
push P_SNAKE_LEN
push P_DISQUALIFIED
call disqualified_update
;----------------------
pop bp
ret 18
endp left
;-------------------------------------------
;-------------------------------------------
proc right
push bp
mov bp, sp
cmp al, [byte ptr last_movement]
je change_head_color3
add [transform_shape], 1
change_head_color3:
;---------------------
push P_TRANSFORM_SHAPE
push P_LAST_MOVEMENT
push P_INVALID_INPUT
push P_NEW_POS
call right_update
;----------------------
push P_APPLE_POSE
push P_COLOR_LIST
push P_TRANSFORM_SHAPE
push P_NEW_POS
push P_LIST_OF_POSITIONS
push P_SNAKE_LEN
call graphical_update
;---------------------
;---------------------
push P_LIST_OF_POSITIONS
push P_SNAKE_LEN
push P_DISQUALIFIED
call disqualified_update
;----------------------
pop bp
ret 18
endp right
;-------------------------------------------
;---------------------start functions----------------------
; input: list_of_positions(+6), apple_position(+4)
; output: None
proc initial
push bp
mov bp, sp
push ax
push di
mov ax, 0b800h
mov es, ax
call clear_screen
mov di, [bp + 6]
mov al, 09
mov ah, 2
mov[es:di], ax
push [bp + 4] ; apple position
call creat_apple
pop di
pop ax
pop bp
ret 4
endp initial
;---------------------------------------------------------
;_____________________________________________________________________________________________
;___________________________________________________MAIN___________________________________________________
; --------------------------
start:
mov ax, @data
mov ds, ax
; ---------------------------
push [list_of_positions]
push offset apple_pose
call initial
;----------------------------
main_game_loop:
push offset color_list
push offset disqualified
push offset apple_pose
push offset snake_len
push offset new_pos
push offset last_movement
push offset list_of_positions
push offset transform_shape
push offset invalid_input
input:
mov ah, 1 ; if there is not a new input it will comtinue with the privews press the auto one
int 16h
je auto_movement
mov ah, 0 ; put in the al the buffer value
int 16h
auto_movement:
cmp al, [byte ptr invalid_input]
je moved
cmp al, 'w'
je up_m
cmp al, 's'
je down_m
cmp al, 'a'
je left_m
cmp al, 'd'
je right_m
cmp al, 'q'
je exit
cmp [last_movement], 0
jne moved ; snake moved before
jmp main_game_loop
moved:
mov al, [byte ptr last_movement]
jmp input
up_m:
call up
jmp exit_check
down_m:
call down
jmp exit_check
left_m:
call left
jmp exit_check
right_m:
call right
jmp exit_check
exit_check:
cmp [disqualified], 1
je exit
jmp main_game_loop
;__________________________________________________________________________________________________________
exit:
mov ax, 4c00h
int 21h
END start