-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.asm
1111 lines (951 loc) · 20.8 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
.model tiny
.code
.386
org 80h
cmd_len db ?
cmd_args db ?
org 100h
main:
call parse_selfintersecting
call parse_help
cmp help_usage, 0
jne print_help
call init_state
; set new handler
mov ax, 3509h
int 21h
mov word ptr old_handler, bx
mov word ptr old_handler + 2, es
mov ax, 2509h
mov dx, offset int_handler
int 21h
mov ah, 0h ; установка режима
mov al, screen_mode
mov bl, 1
int 10h
_loop:
cmp snake_length, 0
je _loop_end
call handle_keydown
cmp al, 1
je _loop_end
cmp is_pause, 0ffh
je _loop
call round
cmp crashed, 0
jne _loop_end
call draw_field
call pause
jmp _loop
_loop_end:
mov ax, 2509h
mov dx, word ptr old_handler + 2
mov ds, dx
mov dx, word ptr cs:old_handler
int 21h
call draw_end
call wait_press_key
ret
print_help:
lea dx, help_msg
push ax
mov ah, 9
int 21h
pop ax
ret
; return al - code (1 if quit)
handle_keydown:
xor ah, ah
mov al, key
cmp old_key, al
je _handle_keydown_ret
_handle_keydown_case:
cmp al, 1
je _quit_game
cmp al, 75
je _handle_keydown_case_75
cmp al, 80
je _handle_keydown_case_80
cmp al, 77
je _handle_keydown_case_77
cmp al, 72
je _handle_keydown_case_72
cmp al, 59
je _handle_keydown_case_59
jmp _handle_keydown_case_end
_handle_keydown_case_80: ; down
cmp direction, 2
je _handle_keydown_case_end
mov direction, 0
jmp _handle_keydown_case_end
_handle_keydown_case_77: ; right
cmp direction, 3
je _handle_keydown_case_end
mov direction, 1
jmp _handle_keydown_case_end
_handle_keydown_case_72: ; up
cmp direction, 0
je _handle_keydown_case_end
mov direction, 2
jmp _handle_keydown_case_end
_handle_keydown_case_75: ; left
cmp direction, 1
je _handle_keydown_case_end
mov direction, 3
jmp _handle_keydown_case_end
_handle_keydown_case_59: ; F1
not is_pause
;call change_page
call draw_help
jmp _handle_keydown_case_end
_handle_keydown_case_end:
_handle_keydown_ret:
mov key, 255
ret
_quit_game:
mov al, 1
ret
draw_field:
pusha
mov cx, field_size
dec cx
_draw_field_loop:
; set cursor
mov ax, cx
mov bh, 80
div bh
mov dh, al ; row
mov dl, ah ; column
mov bh, 0
mov ah, 2
int 10h
; draw cell
lea bx, field
add bx, cx
mov al, byte ptr [bx]
mov ah, al
lea bx, symbol_table
xlat
xchg ah, al ; в ah запомнили сиивол для печати, в al снова символ из field
push ax
lea bx, attribute_table
xlat
mov bl, al ; запомнили аттрибут для печати
pop ax
mov bh, 0
push cx
mov cx, 1
mov al, ah
mov ah, 09
int 10h
pop cx
loop _draw_field_loop
popa
ret
draw_help:
pusha
mov ah, 0h
mov al, screen_mode
mov bl, 1
int 10h
lea bp, _control_by_arrows_string
mov ah, 13h
mov al, 1
mov cx, 17
mov bl, 00001010b
mov bh, 0
push cs
pop es
mov dh, 1
mov dl, 1
int 10h
lea bp, _game_speed_control_string
mov cx, 19
mov bl, 00001010b
mov dh, 2
int 10h
lea bp, _teleport_wall_string
mov cx, 13
mov bl, 00011010b
mov dh, 3
int 10h
lea bp, _mirror_wall_string
mov cx, 11
mov bl, 01111010b
mov dh, 4
int 10h
lea bp, _stone_wall_string
mov cx, 10
mov bl, 01001010b
mov dh, 5
int 10h
lea bp, _apple_string
mov cx, 5
mov bl, 01011010b
mov dh, 6
int 10h
lea bp, _stop_signal_string
mov cx, 11
mov bl, 01001010b
mov dh, 7
int 10h
lea bp, _burger_string
mov cx, 6
mov bl, 00111010b
mov dh, 8
int 10h
popa
ret
draw_end:
mov ah, 0h
mov al, 3
mov bl, 1
int 10h
; write length
lea bp, _snake_length_string
mov ah, 13h
mov al, 0
mov cx, 13
mov bl, 00001010b
mov bh, 0
push cs
pop es
mov dh, 10
mov dl, 1
int 10h
mov ah, 02
mov bh, 0
mov dh, 10
mov dl, 15
int 10h
xor ah, ah
mov al, cs:snake_length
mov cl, 10
div cl
add al, 48
mov ah, 9
mov bh, 0
mov bl, 00001010b
mov cx, 1
int 10h
mov ah, 02
mov bh, 0
mov dh, 10
mov dl, 16
int 10h
xor ah, ah
mov al, cs:snake_length
mov cl, 10
div cl
mov al, ah
add al, 48
mov ah, 9
mov bh, 0
mov bl, 00001010b
mov cx, 1
int 10h
; write score
lea bp, _score_string
mov ah, 13h
mov al, 0
mov cx, 6
mov bl, 00001010b
mov bh, 0
push cs
pop es
mov dh, 11
mov dl, 1
int 10h
mov ah, 02
mov bh, 0
mov dh, 11
mov dl, 8
int 10h
xor ah, ah
mov al, cs:score
mov cl, 10
div cl
add al, 48
mov ah, 9
mov bh, 0
mov bl, 00001010b
mov cx, 1
int 10h
mov ah, 02
mov bh, 0
mov dh, 11
mov dl, 9
int 10h
xor ah, ah
mov al, cs:score
mov cl, 10
div cl
mov al, ah
add al, 48
mov ah, 9
mov bh, 0
mov bl, 00001010b
mov cx, 1
int 10h
; write burgers
lea bp, _burger_count_string
mov ah, 13h
mov al, 0
mov cx, 8
mov bl, 00001010b
mov bh, 0
push cs
pop es
mov dh, 12
mov dl, 1
int 10h
mov ah, 02
mov bh, 0
mov dh, 12
mov dl, 10
int 10h
xor ah, ah
mov al, cs:burger_count
mov cl, 10
div cl
add al, 48
mov ah, 9
mov bh, 0
mov bl, 00001010b
mov cx, 1
int 10h
mov ah, 02
mov bh, 0
mov dh, 12
mov dl, 11
int 10h
xor ah, ah
mov al, cs:burger_count
mov cl, 10
div cl
mov al, ah
add al, 48
mov ah, 9
mov bh, 0
mov bl, 00001010b
mov cx, 1
int 10h
; write the end
lea bp, _the_end_string
mov ah, 13h
mov al, 0
mov cx, 7
mov bl, 00001010b
mov bh, 0
push cs
pop es
mov dh, 14
mov dl, 37
int 10h
mov ah, 02
mov bh, 0
mov dh, 250
mov dl, 250
int 10h
ret
change_page:
pusha
mov ah, 0fh
int 10h
_change_page_case:
cmp bh, 0
je _change_page_case_zero
cmp bh, 1
je _change_page_case_one
jmp _change_page_case_end
_change_page_case_zero:
mov al, 1
jmp _change_page_case_end
_change_page_case_one:
mov al, 0
jmp _change_page_case_end
_change_page_case_end:
mov ah, 05h
int 10h
popa
ret
round:
pusha
call move_snake
_round_case:
cmp cl, 6
je _apple_case
cmp cl, 4
je _teleport_case
cmp cl, 3
je _stone_case
cmp cl, 5
je _mirror_case
cmp cl, 7
je _burger_case
cmp cl, 8
je _stop_signal_case
cmp cl, 1
je _snake_case
jmp _round_case_end
_apple_case:
; удлинняем змейку
mov cl, 1 ; snake
call set_field_cell
mov cl, snake_length
call set_snake_cell
inc cl
mov snake_length, cl
call generate_apple
call generate_stop_signal
mov al, score
inc al
mov score, al
jmp _round_case_end
_teleport_case:
lea bx, snake
mov ax, word ptr [bx] ; ax - head cords
push ax
call get_teleport_cords ; ax - teleport cords
mov word ptr [bx], ax ; equals set_snake_cell with cl = 0
mov cl, 1 ; snake body
call set_field_cell
pop ax
mov cl, 4 ; teleport
call set_field_cell ; восстановили "съеденную стену"
jmp _round_case_end
_stone_case:
not crashed
jmp _round_case_end
_mirror_case:
mov cl, snake_length
call set_snake_cell
mov cl, 1 ; snake
call set_field_cell
lea bx, snake
mov ax, [bx]
push ax
xor ch, ch
mov cl, snake_length
lea si, snake
lea di, snake
add di, cx
add di, cx
_mirror_case_loop:
cmp si, di
jge _mirror_case_loop_end
mov ax, [si]
mov cx, [di]
mov [di], ax
mov [si], cx
inc si
inc si
dec di
dec di
jmp _mirror_case_loop
_mirror_case_loop_end:
pop ax
mov cl, 5 ; mirror
call set_field_cell
mov cl, snake_length
mov ax, 0
call set_snake_cell
xor ah, ah
mov al, direction
add al, 2
mov cl, 4
div cl
mov direction, ah
jmp _round_case_end
_burger_case:
lea bx, snake
xor cx, cx
mov cl, snake_length
add bx, cx
add bx, cx
sub bx, 2
mov ax, [bx]
mov cl, 0
call set_field_cell
mov cl, snake_length
dec cl
mov snake_length, cl
xor ax, ax
call set_snake_cell
call generate_burger
mov al, burger_count
inc al
mov burger_count, al
jmp _round_case_end
_stop_signal_case:
not crashed
jmp _round_case_end
_snake_case:
cmp crash_if_intersect, 0
je _round_case_end
not crashed
jmp _round_case_end
_round_case_end:
_round_ret:
popa
ret
; translate ax to teleport (OX axe)
get_teleport_cords:
get_teleport_cords_case:
cmp ah, 0
je get_teleport_cords_case_left
cmp ah, 79
je get_teleport_cords_case_right
jmp get_teleport_cords_case_end
get_teleport_cords_case_left:
mov ah, 78
jmp get_teleport_cords_case_end
get_teleport_cords_case_right:
mov ah, 1
jmp get_teleport_cords_case_end
get_teleport_cords_case_end:
ret
; ax - tail cords
; cl - object ate
; return:
move_snake:
lea bx, snake
mov ax, word ptr [bx]
mov dx, ax
mov cl, direction
_move_snake_case:
cmp cl, 0
je _inc_y
cmp cl, 1
je _inc_x
cmp cl, 2
je _dec_y
cmp cl, 3
je _dec_x
jmp _move_snake_case_end
_inc_x:
inc ah
jmp _move_snake_case_end
_dec_x:
dec ah
jmp _move_snake_case_end
_inc_y:
inc al
jmp _move_snake_case_end
_dec_y:
dec al
jmp _move_snake_case_end
_move_snake_case_end:
; берем объект по координатам ax, чтобы вернуть его из функции
call get_field_cell
xor ch, ch
push cx
;
mov cl, 1
call set_field_cell
push ax
xor cx, cx
mov cl, snake_length
lea bx, snake
add bx, cx
add bx, cx ; bx - адрес первой пустой клетки
_move_snake_loop:
sub bx, 2 ; word len
mov ax, [bx] ; snake[i - 1]
add bx, 2
mov [bx], ax ; snake[i] = snake[i - 1]
sub bx, 2
loop _move_snake_loop
pop ax
mov cl, 0
call set_snake_cell
; del tail
xor cx, cx
mov cl, snake_length
lea bx, snake
add bx, cx
add bx, cx ; bx - адрес посленней клетки (лишней)
mov ax, [bx] ; tail cords
push ax
mov cl, 0
call set_field_cell
xor ax, ax
mov cl, snake_length
call set_snake_cell
_move_snake_ret:
pop ax ; адрес хвоста
pop cx ; cl - "съеденный" объект
ret
generate_apple:
pusha
_generate_apple_again:
call get_random
push ax
; 80x25 - screen size in 3td mode
mov al, ah
xor ah, ah
mov cl, 78
div cl
mov dh, ah
inc dh
pop ax
xor ah, ah
mov cl, 23
div cl
mov dl, ah
inc dl
xchg ax, dx
call get_field_cell
cmp cl, 1 ; snake
je _generate_apple_again
mov cl, 6 ; apple
call set_field_cell
popa
ret
generate_burger:
pusha
_generate_burger_again:
call get_random
push ax
; 80x25 - screen size in 3td mode
mov al, ah
xor ah, ah
mov cl, 78
div cl
mov dh, ah
inc dh
pop ax
xor ah, ah
mov cl, 23
div cl
mov dl, ah
inc dl
xchg ax, dx
call get_field_cell
cmp cl, 1 ; snake
je _generate_burger_again
mov cl, 7 ; burger
call set_field_cell
_generate_burger_ret:
popa
ret
generate_stop_signal:
pusha
_generate_stop_signal_again:
call get_random
push ax
; 80x25 - screen size in 3td mode
mov al, ah
xor ah, ah
mov cl, 78
div cl
mov dh, ah
inc dh
pop ax
xor ah, ah
mov cl, 23
div cl
mov dl, ah
inc dl
xchg ax, dx
call get_field_cell
cmp cl, 1 ; snake
je _generate_stop_signal_again
push ax
mov ax, stop_signal
mov cl, 0
call set_field_cell
pop ax
mov cl, 8 ; stop signal
call set_field_cell
mov stop_signal, ax
popa
ret
get_random: ; eax - result (0-9999)
push edx
db 0Fh, 31h ; rdtsc (read time stamp counter)
pop edx
ret
; ax - cords, cl - byte
; ah - x, al - y
set_field_cell:
pusha
push cx
mov dx, ax
lea bx, field
mov cl, 80 ; line len
mul cl
add bx, ax
xor ax, ax
mov al, dh
add bx, ax
pop cx
mov byte ptr [bx], cl
popa
ret
; ax - coords
; return cl - value
get_field_cell:
push ax
push bx
push dx
mov dx, ax
lea bx, field
mov cl, 80 ; line len
mul cl
add bx, ax
xor ax, ax
mov al, dh
add bx, ax
mov cl, byte ptr [bx]
pop dx
pop bx
pop ax
ret
; ax - cords, cl - cell cord
set_snake_cell:
pusha
xor ch, ch
lea bx, snake
add bx, cx
add bx, cx
mov word ptr [bx], ax
popa
ret
pause:
mov cx, 0010h ; 1 sec
sub cl, game_speed
mov dx, 0000h
mov ah, 86h
int 15h
ret
init_state:
pusha
mov ax, 0707h
mov cl, 1
call set_field_cell
mov cl, 0
call set_snake_cell
mov ax, 0706h
mov cl, 1
call set_field_cell
mov cl, 1
call set_snake_cell
mov ax, 0705h
mov cl, 1
call set_field_cell
mov cl, 2
call set_snake_cell
mov ax, 0202h
mov cl, 6
call set_field_cell
mov ax, 00
xor ch, ch
mov cl, 23
_init_state_loop:
mov al, cl
push cx
mov ah, 0
mov cl, 4 ; teleport wall
call set_field_cell
mov ah, 79
call set_field_cell
pop cx
loop _init_state_loop
mov ax, 0023h
xor ch, ch
mov cl, 78
_init_state_loop2:
mov ah, cl
mov al, 24
push cx
mov cl, 3 ; stone
call set_field_cell
mov al, 0
mov cl, 5 ; mirror
call set_field_cell
pop cx
loop _init_state_loop2
call generate_burger
popa
ret
int_handler proc far
pushf
call cs:old_handler
pusha
cli
xor bx, bx
in al, 60h ; new code
xor ah, ah
cmp ax, 128
jge _iret
_int_handler_case:
cmp al, 12
je _int_handler_case_12
cmp al, 13
je _int_handler_case_13
cmp al, 57
je _int_handler_case_57
jmp _int_handler_case_end
_int_handler_case_12: ; minus
mov ah, cs:game_speed
cmp ah, 0
je _int_handler_case_end
dec ah
mov cs:game_speed, ah
jmp _int_handler_case_end
_int_handler_case_13: ; plus
mov ah, cs:game_speed
cmp ah, 0fh
je _int_handler_case_end
inc ah
mov cs:game_speed, ah
jmp _int_handler_case_end
_int_handler_case_57: ; space
not cs:is_pause
jmp _int_handler_case_end
_int_handler_case_end:
cmp al, cs:key
je _iret
mov ah, cs:key
mov cs:old_key, ah
mov cs:key, al
_iret:
popa
sti
iret
int_handler endp
; ax - number
print_number:
pusha
mov cx, 0
cycle:
mov dx, 0
mov bx, 10
div bx
mov bx, ax
add dl, 30h
push dx
mov ax, bx
inc cx
cmp ax, 0
je print_number_ret
jmp cycle
print_number_ret:
pop dx
mov ah, 02h
int 21h
loop print_number_ret
popa
ret
wait_press_key:
pusha
_wait_press_key_loop:
mov ah, 0bh
int 21h
cmp al, 0
je _wait_press_key_loop
popa
ret
get_arg_by_key: ; ah - key (char), return: bl - len, bh - position
xor al, al ; позиция - начало аргумента
xor cx, cx
lea bx, cmd_args + 1 ;space
mov cl, cmd_len
dec cl
_loop3: