-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tetris.asm
4399 lines (3835 loc) · 78.7 KB
/
Tetris.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 small
.STACK 64
.DATA
my_score dw 0h
score_str db "SCORE: $"
width_length dw 140h
height_length dw 0c8h
y_start_border dw 0ah
x_start_border dw 96h
y_end_border dw 0bfh
x_end_border dw 0f1h
blank db " $"
starting db "Starting...$"
end_str db "GAME OVER$"
bool_update_block db 1
current_type db 0h
current_x dw 0h
current_y dw 0h
current_color db 0
x_start dw 0h
y_start dw 0h
ntimes dw 0h
score_counter dw 0h
.CODE
main proc far
mov ax, @DATA
mov ds, ax
;set graphical mode
mov ah, 00h
mov al, 13h
int 10h
;set position for starting
mov ah, 02h
mov bh, 00h
mov dh, 05h
mov dl, 0ch
int 10h
;print
mov ah, 09h
lea dx, starting
int 21h
;wait 1 second
mov cx, 0fh
mov dx, 4240h
mov ah, 86h
int 15h
mov ah, 02h
mov bh, 00h
mov dh, 05h
mov dl, 0ch
int 10h
mov ah, 09h
lea dx, blank
int 21h
call show_score
call show_border
call show_blocks_around
time_check_label:
cmp bool_update_block, 1
jne no_new_block
call draw_new_block
jmp new_block
no_new_block:
call update_block
new_block:
jmp time_check_label
;mov ah, 4ch
;int 21h
ret
main endp
show_border proc near
mov dx, y_start_border
mov cx, x_start_border
border_label1:
mov ah, 0ch
mov al, 0ch ;light red
int 10h
inc cx
cmp cx, x_end_border
jle border_label1
cmp dx, y_end_border
je out_border1
mov dx, y_end_border
mov cx, x_start_border
jmp border_label1
out_border1:
mov dx, y_start_border
mov cx, x_start_border
border_label2:
mov ah, 0ch
mov al, 0ch ;light red
int 10h
inc dx
cmp dx, y_end_border
jle border_label2
cmp cx, x_end_border
je out_border2
mov dx, y_start_border
mov cx, x_end_border
jmp border_label2
out_border2:
ret
show_border endp
show_blocks_around proc near
mov cx, 96h
mov dx, 1h
around_label1:
mov ah, 0ch
mov al, 7h ;light gray
int 10h
inc cx
mov ax, 0f0h
cmp cx, ax
jle around_label1
inc dx
mov cx, 96h
cmp dx, 9h
jle around_label1
mov cx, 096h
mov dx, 0c0h
around_label2:
mov ah, 0ch
mov al, 7h ;light gray
int 10h
inc cx
mov ax, 0f0h
cmp cx, ax
jle around_label2
inc dx
mov cx, 96h
cmp dx, 0c7h
jl around_label2
mov cx, 96h
mov dx, 1h
line_around1:
mov ah, 0ch
mov al, 0fh ;white
int 10h
inc dx
cmp dx, 0ah
jl line_around1
add cx, 9h
mov dx, 1h
cmp cx, 0f0h
jle line_around1
mov cx, 96h
mov dx, 0c0h
line_around2:
mov ah, 0ch
mov al, 0fh ;white
int 10h
inc dx
cmp dx, 0c7h
jl line_around2
add cx, 9h
mov dx, 0c0h
cmp cx, 0f0h
jle line_around2
mov cx, 8ch
mov dx, 0ah
around_label3:
mov ah, 0ch
mov al, 7h
int 10h
inc cx
cmp cx, 96h
jl around_label3
mov cx, 8ch
inc dx
cmp dx, 0beh
jl around_label3
mov cx, 0f2h
mov dx, 0ah
around_label4:
mov ah, 0ch
mov al, 7h
int 10h
inc cx
cmp cx, 0fbh
jle around_label4
mov cx, 0f2h
inc dx
cmp dx, 0beh
jl around_label4
mov cx, 8ch
mov dx, 0ah
line_around3:
mov ah, 0ch
mov al, 0fh
int 10h
inc cx
cmp cx, 96h
jl line_around3
mov cx, 8ch
add dx, 9h
cmp dx, 0beh
jle line_around3
mov cx, 0f2h
mov dx, 0ah
line_around4:
mov ah, 0ch
mov al, 0fh
int 10h
inc cx
cmp cx, 0fbh
jle line_around4
mov cx, 0f2h
add dx, 9h
cmp dx, 0beh
jle line_around4
ret
show_blocks_around endp
draw_new_block proc near
;random block
;making random number
xor dx, dx
mov ah, 2ch
int 21h
mov ax, dx
xor dx, dx
mov cx, 5
div cx ;remainder is in dx
mov bool_update_block, 0
cmp dx, 0
je yellow_block
cmp dx, 1
je blue_block
cmp dx, 2
je pink_block
cmp dx, 3
je orange_block
cmp dx, 4
je green_block
yellow_block:
;is this the end of game?
mov cx, 0bbh
mov dx, 0bh
mov ah, 0dh
int 10h
cmp al, 0h
jne end_game
add cx, 9h
int 10h
cmp al, 0h
jne end_game
add dx, 9h
int 10h
cmp al, 0h
jne end_game
sub cx, 9h
int 10h
cmp al, 0h
jne end_game
mov current_type, 0
mov cx, 0bah ;186
inc cx
mov current_x, cx
mov dx, 0ah ;10
inc dx
mov current_y, dx
yellow_label:
mov ah, 0ch
mov al, 0eh ;yellow
int 10h
inc cx
cmp cx, 0cch
jle yellow_label
mov cx, 0bah
inc cx
inc dx
cmp dx, 1ch
jle yellow_label
jmp color_exit
blue_block:
;game end?
mov cx, 0b2h
mov dx, 0bh
mov ah, 0dh
int 10h
cmp al, 0h
jne end_game
add cx, 9h
int 10h
cmp al, 0h
jne end_game
add cx, 9h
int 10h
cmp al, 0h
jne end_game
add cx, 9h
int 10h
cmp al, 0h
jne end_game
mov current_type, 1
mov cx, 0b1h ;177
inc cx
mov current_x, cx
mov dx, 0ah ;10
inc dx
mov current_y, dx
blue_label:
mov ah, 0ch
mov al, 9h ;light blue
int 10h
inc cx
cmp cx, 0d5h ;213
jle blue_label
mov cx, 0b1h
inc cx
inc dx
cmp dx, 13h ;19
jle blue_label
jmp color_exit
pink_block:
;game end?
mov cx, 0b2h
mov dx, 0bh
mov ah, 0dh
int 10h
cmp al, 0h
jne end_game
add cx, 9h
int 10h
cmp al, 0h
jne end_game
add cx, 9h
int 10h
cmp al, 0h
jne end_game
add dx, 9h
sub cx, 9h
int 10h
cmp al, 0h
jne end_game
mov current_type, 2
mov cx, 0b1h ;177
inc cx
mov current_x, cx
mov dx, 0ah ;10
inc dx
mov current_y, dx
pink_label1:
mov ah, 0ch
mov al, 0dh ;pink
int 10h
inc cx
cmp cx, 0cch
jle pink_label1
mov cx, 0b1h
inc cx
inc dx
cmp dx, 13h
jle pink_label1
mov cx, 0bah
inc cx
mov dx, 13h
inc dx
pink_label2:
mov ah, 0ch
mov al, 0dh ;pink
int 10h
inc cx
cmp cx, 0c3h
jle pink_label2
mov cx, 0bah
inc cx
inc dx
cmp dx, 1ch
jle pink_label2
jmp color_exit
orange_block:
;game end?
mov cx, 0bbh
mov dx, 0bh
mov ah, 0dh
int 10h
cmp al, 0h
jne end_game
add dx, 9h
int 10h
cmp al, 0h
jne end_game
add dx, 9h
int 10h
cmp al, 0h
jne end_game
add cx, 9h
int 10h
cmp al, 0h
jne end_game
mov current_type, 3
mov cx, 0bah ;186
inc cx
mov current_x, cx
mov dx, 0ah ;10
inc dx
mov current_y, dx
orange_label1:
mov ah, 0ch
mov al, 6h ;orange
int 10h
inc cx
cmp cx, 0c3h
jle orange_label1
mov cx, 0bah
inc cx
inc dx
cmp dx, 25h
jle orange_label1
mov cx, 0c3h
inc cx
mov dx, 1ch
inc dx
orange_label2:
mov ah, 0ch
mov al, 6h ;orange
int 10h
inc cx
cmp cx, 0cch
jle orange_label2
mov cx, 0c3h
inc cx
inc dx
cmp dx, 25h
jle orange_label2
jmp color_exit
green_block:
mov cx, 0bbh
mov dx, 0bh
mov ah, 0dh
int 10h
cmp al, 0h
jne end_game
add dx, 9h
int 10h
cmp al, 0h
jne end_game
add cx, 9h
int 10h
cmp al, 0h
jne end_game
add dx, 9h
int 10h
cmp al, 0h
jne end_game
mov current_type, 4
mov cx, 0bah
inc cx
mov current_x, cx
mov dx, 0ah
inc dx
mov current_y, dx
green_label1:
mov ah, 0ch
mov al, 0ah ;green
int 10h
inc cx
cmp cx, 0c3h
jle green_label1
mov cx, 0bah
inc cx
inc dx
cmp dx, 1ch
jle green_label1
mov cx, 0c3h
inc cx
mov dx, 13h
inc dx
green_label2:
mov ah, 0ch
mov al, 0ah ;green
int 10h
inc cx
cmp cx, 0cch
jle green_label2
mov cx, 0c3h
inc cx
inc dx
cmp dx, 25h
jle green_label2
jmp color_exit
end_game:
call game_over
color_exit:
ret
draw_new_block endp
game_over proc
mov ah, 02h
mov bh, 00h
mov dh, 08h
mov dl, 04h
int 10h
mov ah, 09h
lea dx, end_str
int 21h
mov ah, 4ch
int 21h
ret
game_over endp
update_block proc near
;if key has been pressed
mov ah, 1h
int 16h
jz exit_update
mov ah, 0h
int 16h
;s key
cmp al, 73h
je call_down_move
cmp al, 53h
je call_down_move
;a key
cmp al, 61h
je call_left_move
cmp al, 41h
je call_left_move
;d key
cmp al, 64h
je call_right_move
cmp al, 44h
je call_right_move
;w key
cmp al, 77h
je call_rotate_move
cmp al, 57h
je call_rotate_move
jmp exit_update
call_rotate_move:
call rotate_move
jmp exit_update
call_right_move:
call right_move
jmp exit_update
call_left_move:
call left_move
jmp exit_update
call_down_move:
call down_move
jmp exit_update
exit_update:
ret
update_block endp
rotate_move proc
;yellow has no rotate
cmp current_type, 0
je exit_rotate_move
cmp current_type, 1
je rotate_move1
cmp current_type, 5
je rotate_move5
cmp current_type, 2
je rotate_move2
cmp current_type, 6
je rotate_move6
cmp current_type, 7
je rotate_move7
cmp current_type, 8
je rotate_move8
cmp current_type, 3
je rotate_move3
cmp current_type, 9
je rotate_move9
cmp current_type, 0ah
je rotate_move10
cmp current_type, 0bh
je rotate_move11
cmp current_type, 4
je rotate_move4
cmp current_type, 0ch
je rotate_move12
rotate_move1:
;can rotate?
mov cx, current_x
mov dx, current_y
add cx, 9h
dec dx
mov ah, 0dh
int 10h
cmp al, 0h
jne exit_rotate_move
add dx, 0ah
int 10h
cmp al, 0h
jne exit_rotate_move
add dx, 9h
int 10h
cmp al, 0h
jne exit_rotate_move
mov current_color, 0h
mov cx, current_x
mov x_start, cx
mov dx, current_y
mov y_start, dx
call draw_clear_block
mov cx, current_x
add cx, 12h
mov x_start, cx
mov dx, current_y
mov y_start, dx
call draw_clear_block
mov cx, current_x
add cx, 1bh
mov x_start, cx
mov dx, current_y
mov y_start, dx
call draw_clear_block
mov current_color, 9h
mov cx, current_x
add cx, 9h
mov x_start, cx
mov dx, current_y
sub dx, 9h
mov y_start, dx
call draw_clear_block
mov cx, current_x
add cx, 9h
mov x_start, cx
mov dx, current_y
add dx, 9h
mov y_start, dx
call draw_clear_block
mov cx, current_x
add cx, 9h
mov x_start, cx
mov dx, current_y
add dx, 12h
mov y_start, dx
call draw_clear_block
mov current_type, 5
mov cx, current_x
add cx, 9h
mov current_x, cx
mov dx, current_y
sub dx, 9h
mov current_y, dx
;end?
mov cx, current_x
mov dx, current_y
add dx, 24h
mov ah, 0dh
int 10h
cmp al, 0h
jne end_any_move
jmp exit_rotate_move
rotate_move5:
;can rotate?
mov cx, current_x
mov dx, current_y
add cx, 9h
sub dx, 12h
mov ah, 0dh
int 10h
cmp al, 0h
jne exit_rotate_move
add cx, 9h
int 10h
cmp al, 0h
jne exit_rotate_move
sub cx, 1bh
int 10h
cmp al, 0h
jne exit_rotate_move
mov current_color, 0h
mov cx, current_x
mov x_start, cx
mov dx, current_y
mov y_start, dx
call draw_clear_block
mov cx, current_x
mov x_start, cx
mov dx, current_y
add dx, 9h
mov y_start, dx
call draw_clear_block
mov cx, current_x
mov x_start, cx
mov dx, current_y
add dx, 1bh
mov y_start, dx
call draw_clear_block
mov current_color, 9h
mov cx, current_x
add cx, 9h
mov x_start, cx
mov dx, current_y
add dx, 12h
mov y_start, dx
call draw_clear_block
mov cx, current_x
add cx, 12h
mov x_start, cx
mov dx, current_y
add dx, 12h
mov y_start, dx
call draw_clear_block
mov cx, current_x
sub cx, 9h
mov x_start, cx
mov dx, current_y
add dx, 12h
mov y_start, dx
call draw_clear_block
mov current_type, 1
mov cx, current_x
sub cx, 9h
mov current_x, cx
mov dx, current_y
add dx, 12h
mov current_y, dx
;end?
mov cx, current_x
mov dx, current_y
add dx, 9h
mov ah, 0dh
int 10h
cmp al, 0h
jne end_any_move
add cx, 9h
int 10h
cmp al, 0h
jne end_any_move
add cx, 9h
int 10h
cmp al, 0h
jne end_any_move
add cx, 9h
int 10h
cmp al, 0h
jne end_any_move
jmp exit_rotate_move
rotate_move2:
;can rotate?
mov cx, current_x
mov dx, current_y
add cx, 9h
dec dx
mov ah, 0dh
int 10h
cmp al, 0h
jne exit_rotate_move
mov current_color, 0h
mov cx, current_x
add cx, 12h
mov x_start, cx
mov dx, current_y
mov y_start, dx
call draw_clear_block
mov current_color, 0dh
mov cx, current_x
add cx, 9h
mov x_start, cx
mov dx, current_y
sub dx, 9h
mov y_start, dx
call draw_clear_block
mov current_type, 6
mov cx, current_x
add cx, 9h
mov current_x, cx
mov dx, current_y
sub dx, 9h
mov current_y, dx
;end?
mov cx, current_x
mov dx, current_y
add dx, 12h
sub cx, 9h
mov ah, 0dh
int 10h
cmp al, 0h
jne end_any_move
add cx, 9h
add dx, 9h
int 10h
cmp al, 0h
jne end_any_move
jmp exit_rotate_move
rotate_move6:
;can rotate?
mov cx, current_x
mov dx, current_y
add cx, 9h
add dx, 9h
mov ah, 0dh
int 10h
cmp al, 0h
jne exit_rotate_move
mov current_color, 0h
mov cx, current_x
mov x_start, cx
mov dx, current_y
add dx, 12h
mov y_start, dx
call draw_clear_block
mov current_color, 0dh
mov cx, current_x
add cx, 9h
mov x_start, cx
mov dx, current_y
add dx, 9h
mov y_start, dx
call draw_clear_block
mov current_type, 7
;current_x & current_y won't change
;end?
mov cx, current_x
mov dx, current_y
add dx, 12h
mov ah, 0dh
int 10h
cmp al, 0h
jne end_any_move
add cx, 9h
int 10h
cmp al, 0h
jne end_any_move
sub cx, 12h
int 10h
cmp al, 0h
jne end_any_move
jmp exit_rotate_move
rotate_move7:
;can rotate?
mov cx, current_x
mov dx, current_y
add dx, 12h
mov ah, 0dh
int 10h
cmp al, 0h
jne exit_rotate_move
mov current_color, 0h
mov cx, current_x
sub cx, 9h
mov x_start, cx
mov dx, current_y
add dx, 9h
mov y_start, dx
call draw_clear_block
mov current_color, 0dh
mov cx, current_x
mov x_start, cx
mov dx, current_y
add dx, 12h
mov y_start, dx
call draw_clear_block
mov current_type, 8
;current_x & current_y won't change
;end?
mov cx, current_x