-
Notifications
You must be signed in to change notification settings - Fork 0
/
2048.asm
2500 lines (2186 loc) · 39.7 KB
/
2048.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
;-------------------------------2048 BOARD GAME ON 4*4 GRID
;-------------------------------FINAL PROJECT BY MUHAMMAD ABDULLAH ATIF
;-------------------------------ROLL NO # L21-6225
;-------------------------------SECTION AND BATCH BSDS-3A2-21
;-------------------------------HOPE YOU LIKE IT AND WIN :)
[org 0x0100]
;-------------------------------
;-------------------------------BASIC NEEDS (promts and messages)
;-------------------------------
jmp start
welcomemsg1: db " COAL FINAL PROJECT "
welcomemsg2: db " WELCOME TO 2048 BOARD GAME "
welcomemsg3: db " GAME WILL START IN FEW SECONDS "
welcomemsg4: db " 2048 BOARD GAME "
welcomemsg5: db " SCORE "
welcomemsg6: db " L21-6225 "
welcomemsg7: db " By MUHAMMAD ABDULLAH "
instructionmsg1: db "Instructions :"
instructionmsg2: db "1) Up - ^"
instructionmsg3: db "2) Down - v"
instructionmsg4: db "3) Left - <"
instructionmsg5: db "4) Right - >"
instructionmsg6: db "5) End -ESC"
endingmsg1: db " ||GAME OVER|| "
endingmsg2: db " ||YOUR SCORE IS GIVEN BELOW|| "
endingmsg3: db " ||SCORE|| "
random2: dw 2
endingscore: dw 0
endingcheck: dw 2048
random0123: db 0
oldisr: dd 0
score: dw 0
arr1: dw 0,0,0,0
arr2: dw 0,0,0,0
arr3: dw 0,0,0,0
arr4: dw 0,0,0,0
zerocheck: dw 0
;-------------------------------
;-------------------------------CLEAR SCREEN (Clearing screen)
;-------------------------------
clearscreen:
mov ax,0xb800
mov es,ax
mov di,0
clr:
mov word[es:di],0x0720
add di,2
cmp di,4000
jne clr
ret
;-------------------------------
;-------------------------------DELAY FUNCTION GRID LINE PRINT (delaying printing of lines in grid)
;-------------------------------
delay:
mov bp, 2
mov si, 2
d:
dec bp
jnz d
dec si
cmp si,0
jnz d
ret
;-------------------------------
;-------------------------------DELAY FUNCTION2 GRID CROSSES PRINT (delaying printing of crosses in grid)
;-------------------------------
delay1:
mov bp,10
mov si,10
d_1:
dec bp
jnz d_1
dec si
cmp si,0
jnz d_1
ret
;-------------------------------
;-------------------------------DELAY FUNCTION3 WELCOME SCREEN COUNTER (delaying counter printing)
;-------------------------------
delay2:
mov bp,20
mov si,20
d_2:
dec bp
jnz d_2
dec si
cmp si,0
jnz d_2
ret
;-------------------------------
;-------------------------------DELAY FUNCTION4 BIOS
;-------------------------------
delayprint:
xor dx,dx
mov cx,1
mov ah,0x86
int 0x0015
mov ah,0x0c
int 0x0021
ret
;-------------------------------
;-------------------------------WELCOME SCREEN (page 1 /welcomemsg 1,2,3 and background)
;-------------------------------
welcomescreen:
call clearscreen ;<><>
mov ax,0xb800
mov es,ax
mov di,0
wel:
mov word[es:di],0x0F5F
add di,2
cmp di,4000
jne wel
welmessage1:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x4F
mov dx,0x041F
mov cx,20
push cs
pop es
mov bp,welcomemsg1
int 0x10
welmessage2:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x2F
mov dx,0x081B
mov cx,28
push cs
pop es
mov bp,welcomemsg2
int 0x10
welmessage3:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x1F
mov dx,0x0C19
mov cx,32
push cs
pop es
mov bp,welcomemsg3
int 0x10
timer:
call delay
mov ax,0xb800
mov es,ax
mov di,2638
mov dh,0x0F
mov dl,0x35
mov bx,6
time:
mov word[es:di],dx
call delay2
sub dl,0x01
sub bx,1
cmp bx,0
jnz time
call clearscreen ;<><>
ret
;-------------------------------
;-------------------------------END SCREEN (page 3 /endingmsg 1,2,3 and background/final score)
;-------------------------------
endingscreen:
call clearscreen ;<><>
mov ax,0xb800
mov es,ax
mov di,0
ends:
mov word[es:di],0x0E5F
add di,2
cmp di,4000
jne ends
mov di,0
ends1:
mov word[es:di],0x0E7C
add di,2
cmp di,318
jne ends1
mov di,318
ends2:
mov word[es:di],0x0E7C
add di,160
cmp di,3998
jne ends2
mov di,3680
ends3:
mov word[es:di],0x0E7C
add di,2
cmp di,4000
jne ends3
mov di,3840
ends4:
mov word[es:di],0x0E7C
sub di,160
cmp di,0
jne ends4
endmessage1:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0xCF
mov dx,0x0A20
mov cx,15
push cs
pop es
mov bp,endingmsg1
int 0x10
endmessage2:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0xAF
mov dx,0x0C18
mov cx,31
push cs
pop es
mov bp,endingmsg2
int 0x10
endmessage3:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x9F
mov dx,0x0E22
mov cx,11
push cs
pop es
mov bp,endingmsg3
int 0x10
endscore:
mov ax,2634
push ax
push word[score]
call printnum ;<<>>
ret
;-------------------------------
;-------------------------------MAIN BOARD INITIALS (page 2/welcomemsg 4,5,6,7/keys instructions,top/bottom backgrounds,border print/actual grid print/printscore)
;-------------------------------
mainboard:
mov ax,0xb800
mov es,ax
mov di,0
main:
mov word[es:di],0x4F2E
add di,2
cmp di,640
jne main
mov di,3360
main1:
mov word[es:di],0x4F2E
add di,2
cmp di,3840
jne main1
welmessage4:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x0E
mov dx,0x021D
mov cx,17
push cs
pop es
mov bp,welcomemsg4
int 0x10
welmessage5:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x0E
mov dx,0x023F
mov cx,7
push cs
pop es
mov bp,welcomemsg5
int 0x10
welmessage6:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x0E
mov dx,0x0205
mov cx,10
push cs
pop es
mov bp,welcomemsg6
int 0x10
welmessage7:
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x0E
mov dx,0x161B
mov cx,22
push cs
pop es
mov bp,welcomemsg7
int 0x10
call printgrid ;<><>
call printborder ;<><>
call delay1
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x4F
mov dx,0x0804
mov cx,14
push cs
pop es
mov bp,instructionmsg1
int 0x10
insmsg2:
call delay
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x1E
mov dx,0x0A04
mov cx,14
push cs
pop es
mov bp,instructionmsg2
int 0x10
insmsg3:
call delay
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x2E
mov dx,0x0C04
mov cx,14
push cs
pop es
mov bp,instructionmsg3
int 0x10
insmsg4:
call delay
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x5E
mov dx,0x0E04
mov cx,14
push cs
pop es
mov bp,instructionmsg4
int 0x10
insmsg5:
call delay
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x3E
mov dx,0x1004
mov cx,14
push cs
pop es
mov bp,instructionmsg5
int 0x10
insmsg6:
call delay
mov ah,0x13
mov al,0
mov bh,0
mov bl,0x6E
mov dx,0x1204
mov cx,14
push cs
pop es
mov bp,instructionmsg6
int 0x10
call delay1 ;<><>
call printgridarray ;<><> ;<><>
call generate2 ;<><>
call printscore ;<><>
ret
;-------------------------------
;-------------------------------PRINT BORDER (printing border of grid)
;-------------------------------
printborder:
mov ax,0xb800
mov es,ax
mov di,844
border1:
mov word[es:di],0x5ECD
add di,2
cmp di,908
jne border1
mov di,1066
border2:
mov word[es:di],0x5ECE
add di,160
cmp di,3146
jne border2
mov di,3146
border3:
mov word[es:di],0x5ECD
sub di,2
cmp di,3082
jne border3
mov di,2924
border4:
mov word[es:di],0x5ECE
sub di,160
cmp di,844
jne border4
ret
;-------------------------------
;-------------------------------PRINT GRID (actual grid printing)
;-------------------------------
printgrid:
mov ax,0xb800
mov es,ax
mov di,1006
print1:
mov word[es:di],0x31DF
call delay ;<><>
add di,2
cmp di,1066
jne print1
mov di,1224
print2:
mov word[es:di],0x31DC
call delay ;<><>
add di,160
cmp di,3144
jne print2
mov di,1222
print3:
mov word[es:di],0x31DC
add di,160
cmp di,3142
jne print3
mov di,2984
print4:
mov word[es:di],0x31DC
call delay ;<><>
sub di,2
cmp di,2926
jne print4
mov di,2926
print5:
mov word[es:di],0x31DC
call delay ;<><>
sub di,160
cmp di,1006
jne print5
mov di,2928
print6:
mov word[es:di],0x31DC
sub di,160
cmp di,1008
jne print6
call delay1 ;<><>
mov di,1180
print7:
mov word[es:di],0x31DC
add di,160
cmp di,2940
jne print7
mov di,1182
print8:
mov word[es:di],0x31DC
add di,160
cmp di,2942
jne print8
mov di,1486
print9:
mov word[es:di],0x31DC
add di,2
cmp di,1544
jne print9
call delay1 ;<><>
mov di,1194
print10:
mov word[es:di],0x31DC
add di,160
cmp di,2954
jne print10
mov di,1196
print11:
mov word[es:di],0x31DC
add di,160
cmp di,2956
jne print11
mov di,1966
print12:
mov word[es:di],0x31DC
add di,2
cmp di,2024
jne print12
call delay1 ;<><>
mov di,1208
print13:
mov word[es:di],0x31DC
add di,160
cmp di,2968
jne print13
mov di,1210
print14:
mov word[es:di],0x31DC
add di,160
cmp di,2970
jne print14
mov di,2446
print15:
mov word[es:di],0x31DC
add di,2
cmp di,2504
jne print15
call delay1 ;<><>
ret
;-------------------------------
;-------------------------------GET RANDOM 2 (getting randomly 2)
;-------------------------------
getrandom2:
MOV AH, 00h ; interrupts to get system time
INT 1AH ; CX:DX now hold number of clock ticks since midnight
mov dx,2
mov ax, dx
xor dx, dx
mov cx, 3;
div cx ; here dx contains the remainder of the division - from 0 to 9
add dl, '0' ; to ascii from '0' to '9'
mov word[random2],dx;
ret
;-------------------------------
;-------------------------------GET RANDOM 0123 (getting randomly 0123)
;-------------------------------
getrandom0123:
MOV AH, 00h ; interrupts to get system time
INT 1AH ; CX:DX now hold number of clock ticks since midnight
mov ax, dx
xor dx, dx
mov cx, 8
div cx ; here dx contains the remainder of the division - from 0 to 9
add dl, '0' ; to ascii from '0' to '9'
mov word[random0123],dx;
ret
;-------------------------------
;-------------------------------PRINT SCORE (printing score on page 2)
;-------------------------------
printscore:
mov ax,0xb800
mov es,ax
mov ax,462
push ax
push word[score]
call printnum ;<><>
ret
;-------------------------------
;-------------------------------PRINT RANDOM 2 (printing 2 randomly out of any 16 locations)
;-------------------------------
printgridarray:
mov ax,0xb800
mov es,ax
mov ax,1330
push ax
push word[arr1]
call printnum ;<><>
mov ax,1344
push ax
push word[arr1+2]
call printnum ;<><>
mov ax,1358
push ax
push word[arr1+4]
call printnum ;<><>
mov ax,1372
push ax
push word[arr1+6]
call printnum ;<><>
mov ax,1810
push ax
push word[arr2]
call printnum ;<><>
mov ax,1824
push ax
push word[arr2+2]
call printnum ;<><>
mov ax,1838
push ax
push word[arr2+4]
call printnum ;<><>
mov ax,1852
push ax
push word[arr2+6]
call printnum ;<><>
mov ax,2290
push ax
push word[arr3]
call printnum ;<><>
mov ax,2304
push ax
push word[arr3+2]
call printnum ;<><>
mov ax,2318
push ax
push word[arr3+4]
call printnum ;<><>
mov ax,2332
push ax
push word[arr3+6]
call printnum ;<><>
mov ax,2770
push ax
push word[arr4]
call printnum ;<><>
mov ax,2784
push ax
push word[arr4+2]
call printnum ;<><>
mov ax,2798
push ax
push word[arr4+4]
call printnum ;<><>
mov ax,2812
push ax
push word[arr4+6]
call printnum ;<><>
ret
;-------------------------------
;-------------------------------RIGHT KEY FUNCTION
;-------------------------------
rightfunction:
rarray_1:
mov ax,[arr1+4]
mov bx,[arr1+6]
cmp ax,bx
jz r1
rback2:
mov ax,[arr1+2]
mov bx,[arr1+4]
cmp ax,bx
jz r2
rback3:
mov ax,[arr1]
mov bx,[arr1+2]
cmp ax,bx
jz r3
rback4:
mov ax,[arr1]
mov bx,[arr1+4]
cmp ax,bx
jz r4
rback5:
mov ax,[arr1]
mov bx,[arr1+6]
cmp ax,bx
jz r5
rback6:
mov ax,[arr1+2]
mov bx,[arr1+6]
cmp ax,bx
jz r6
jmp rarray_2
r1:
add ax,bx
mov word[arr1+6],ax
mov word[arr1+4],0
call printgridarray ;<><>
add word[score],5
jmp rback2
r2:
add ax,bx
mov word[arr1+4],ax
mov word[arr1+2],0
call printgridarray ;<><>
add word[score],5
jmp rback3
r3:
add ax,bx
mov word[arr1+2],ax
mov word[arr1],0
call printgridarray ;<><>
add word[score],5
jmp rback4
r4:
add ax,bx
mov word[arr1+4],ax
mov word[arr1],0
call printgridarray ;<><>
add word[score],5
jmp rback5
r5:
add ax,bx
mov word[arr1+6],ax
mov word[arr1],0
call printgridarray ;<><>
add word[score],5
jmp rback6
r6:
add ax,bx
mov word[arr1+6],ax
mov word[arr1+2],0
call printgridarray ;<><>
add word[score],5
rarray_2:
mov ax,[arr2+4]
mov bx,[arr2+6]
cmp ax,bx
jz rr1
rrback2:
mov ax,[arr2+2]
mov bx,[arr2+4]
cmp ax,bx
jz rr2
rrback3:
mov ax,[arr2]
mov bx,[arr2+2]
cmp ax,bx
jz rr3
rrback4:
mov ax,[arr2]
mov bx,[arr2+4]
cmp ax,bx
jz rr4
rrback5:
mov ax,[arr2]
mov bx,[arr2+6]
cmp ax,bx
jz rr5
rrback6:
mov ax,[arr2+2]
mov bx,[arr2+6]
cmp ax,bx
jz rr6
jmp rarray_3
rr1:
add ax,bx
mov word[arr2+6],ax
mov word[arr2+4],0
call printgridarray ;<><>
add word[score],5
jmp rrback2
rr2:
add ax,bx
mov word[arr2+4],ax
mov word[arr2+2],0
call printgridarray ;<><>
add word[score],5
jmp rrback3
rr3:
add ax,bx
mov word[arr2+2],ax
mov word[arr2],0
call printgridarray ;<><>
add word[score],5
jmp rrback4
rr4:
add ax,bx
mov word[arr2+4],ax
mov word[arr2],0
call printgridarray ;<><>
add word[score],5
jmp rrback5
rr5:
add ax,bx
mov word[arr2+6],ax
mov word[arr2],0
call printgridarray ;<><>
add word[score],5
jmp rrback6
rr6:
add ax,bx
mov word[arr2+6],ax
mov word[arr2+2],0
call printgridarray ;<><>
add word[score],5
rarray_3:
mov ax,[arr3+4]
mov bx,[arr3+6]
cmp ax,bx
jz rrr1
rrrback2:
mov ax,[arr3+2]
mov bx,[arr3+4]
cmp ax,bx
jz rrr2
rrrback3:
mov ax,[arr3]
mov bx,[arr3+2]
cmp ax,bx
jz rrr3
rrrback4:
mov ax,[arr3]
mov bx,[arr3+4]
cmp ax,bx
jz rrr4
rrrback5:
mov ax,[arr3]
mov bx,[arr3+6]
cmp ax,bx
jz rrr5
rrrback6:
mov ax,[arr3+2]
mov bx,[arr3+6]
cmp ax,bx
jz rrr6
jmp rarray_4
rrr1:
add ax,bx
mov word[arr3+6],ax
mov word[arr3+4],0
call printgridarray ;<><>
add word[score],5
jmp rrrback2
rrr2:
add ax,bx
mov word[arr3+4],ax
mov word[arr3+2],0
call printgridarray ;<><>
add word[score],5
jmp rrrback3
rrr3:
add ax,bx
mov word[arr3+2],ax
mov word[arr3],0
call printgridarray ;<><>
add word[score],5
jmp rrrback4
rrr4:
add ax,bx
mov word[arr3+4],ax
mov word[arr3],0
call printgridarray ;<><>
add word[score],5
jmp rrrback5
rrr5:
add ax,bx
mov word[arr3+6],ax
mov word[arr3],0
call printgridarray ;<><>
add word[score],5
jmp rrrback6
rrr6:
add ax,bx
mov word[arr3+6],ax
mov word[arr3+2],0
call printgridarray ;<><>
add word[score],5
rarray_4:
mov ax,[arr4+4]
mov bx,[arr4+6]
cmp ax,bx
jz rrrr1
rrrrback2:
mov ax,[arr4+2]
mov bx,[arr4+4]
cmp ax,bx
jz rrrr2
rrrrback3:
mov ax,[arr4]
mov bx,[arr4+2]
cmp ax,bx
jz rrrr3
rrrrback4:
mov ax,[arr4]
mov bx,[arr4+4]
cmp ax,bx
jz rrrr4
rrrrback5:
mov ax,[arr4]
mov bx,[arr4+6]
cmp ax,bx
jz rrrr5
rrrrback6:
mov ax,[arr4+2]
mov bx,[arr4+6]
cmp ax,bx
jz rrrr6
jmp rend
rrrr1:
add ax,bx
mov word[arr4+6],ax
mov word[arr4+4],0
call printgridarray ;<><>
add word[score],5
jmp rrrrback2
rrrr2:
add ax,bx
mov word[arr4+4],ax
mov word[arr4+2],0
call printgridarray ;<><>
add word[score],5
jmp rrrrback3
rrrr3:
add ax,bx