-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.asm
907 lines (782 loc) · 17.9 KB
/
blackjack.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
.model small
.stack 100h
.data
mult DW 25173
incr DW 13849
SEED DW ?
welcome DB 'Welcome to Blackjack$'
DHandMsg DB 'Dealer Hand$'
PHandMSg DB 'Your Hand$'
PTotalMSg DB 'Your Total:$'
DTotalMSg DB 'Dealer Total:$'
msgPbust db "Bust! You lose!$"
msgPwin db "Blackjack! You win!$"
msgAskHitOrStay db 'Do you want to hit or stay? (h/s): $'
msgInvalidInput db 'Invalid input. Please enter "h" for hit or "s" for stay: $'
msgPlayerHit db 'You chose to hit.$'
msgPlayerStay db 'You chose to stay.$'
msgPlayerWin DB 'YOU WON!$'
msgDealerWin DB 'DELAER WON. YOU LOST!$'
;msgPcontinue db "Your score is less than 21. Continue playing.$", 0
msgClear db " $"
round DB 0
dealerHand db 10 dup (0)
playerHand db 10 dup (0)
dealerSize db 0
playerSize db 0
dealerRow db 3
dealerCol db 1
playerRow db 16
playerCol db 1
currDCard dW 0
currPCard dW 0
deck db 65,50,51,52,53,54,55,56,57,74,81,75,42
pScore db ?
dScore db ?
row db ?
column db ?
character db ?
.code
printStr MACRO row, column
saveRegs
;calculate length of the string
MOV CX, 00 ;length will be stroed in cx
MOV SI, BP
CALL strLen
;print the string
MOV AX, 1301H
MOV BH, 0
MOV BL, 02H ; color
DEC CX ;the calculated length includes the $ symbol. So decrement length by 1
MOV DH, row
MOV DL, column
INT 10H
restoreRegs
endm
saveRegs MACRO
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH SI
endm
restoreRegs MACRO
POP SI
POP DX
POP CX
POP BX
POP AX
endm
setCursor MACRO row, column
saveRegs
MOV AH, 02H
MOV DH, row
MOV DL, column
INT 10H
restoreRegs
endm
dispCard MACRO cardNum
saveRegs
MOV AH, 0
LEA DX, deck
MOV AL, cardNum
DEC AL
ADD DX, AX
MOV SI, DX
MOV AL, [SI]
MOV AH, 09H
MOV BX, 2
MOV CX, 1
INT 10H
restoreRegs
endm
;---------------------------------------------------------------------------------------
start:
MOV AX, @data
MOV DS, AX
MOV ES, AX
PUSH BP
MOV bp,sp
LEA BP, welcome
printStr 0,25
LEA BP, DHandMsg
printStr 2,1
LEA BP, PHandMsg
printStr 14,1
LEA BP, PTotalMsg
printStr 14,15
;Initialise the game
CALL createSeed
CALL dealDealer
CALL dealPlayer
CALL dealDealer
CALL dealPlayer
CALL dispDHand
CALL dispPHand
CALL checkP21
CALL askHitOrStay
MOV AX, 4C00H
INT 21H
;---------------------------------------------------------------------------------------
dealDealer PROC
;dealerHand
genRand 12, 1
PUSH DX
MOV AL, dealerSize
MOV AH, 0
LEA DX, dealerHand
ADD DX,AX
MOV SI, DX
POP DX
MOV [SI], DL
INC dealerSize
RET
dealDealer ENDP
dealPlayer PROC
;playerHand
genRand 12, 1
PUSH DX
MOV AL, playerSize
MOV AH, 0
LEA DX, playerHand
ADD DX, AX
MOV SI, DX
POP DX
MOV [SI], DL
INC playerSize
RET
dealPlayer ENDP
dispCardNew MACRO r, char, currCard
saveRegs
MOV AH, 0
LEA DX, deck
DEC AL
ADD DX, AX
MOV SI, DX
MOV AL, [SI]
mov character, al
xor dx, dx
mov ax, currCard
mov bx, 11
;int 3h
mul bx
mov row, r
mov column, al
call printCard
restoreRegs
ENDM
dispDHand PROC
dispNextDcard:
MOV BL, dealerSize
LEA DX, dealerHand
ADD DX, currDCard
MOV SI, DX
SUB BX, currDCard
dh1:
CMP BL, 0
JE exdispDHand
MOV AL, [SI]
setCursor dealerRow, dealerCol
cmp currDCard, 1,
je dispMask
dispCardNew 3, AL,currDCard
updateDvals:
INC currDCard
INC dealerCol
INC SI
DEC BL
JMP dh1
dispMask:
MOV AL, 13
dispCardNew 3, AL, 1
jmp updateDvals
exdispDHand:
RET
dispDHand ENDP
printCard PROC
push ax
push bx
push cx
push dx
;top border
mov ah, 02h
mov dh, row
mov dl, column
inc dl
int 10h
mov ah, 09h
mov al, '-'
mov bl, 02
mov cx, 8
int 10h
;left border
mov cx, 8
dec dl
print_left_border:
inc dh
mov ah, 02h
int 10h
push cx
mov ah, 09h
mov al, '|'
mov bl, 02
mov cx, 1
int 10h
pop cx
loop print_left_border
;bottom border
inc dh
inc dl
mov ah, 02h
int 10h
mov ah, 09h
mov al, '-'
mov bl, 02
mov cx, 8
int 10h
;left border
mov cx, 8
add dl, 8
print_right_border:
dec dh
mov ah, 02h
int 10h
push cx
mov ah, 09h
mov al, '|'
mov bl, 02
mov cx, 1
int 10h
pop cx
loop print_right_border
mov dh, row
mov dl, column
add dh, 1
add dl, 1
mov ah, 02
int 10h
mov ah, 09h
mov al, character
mov bl, 2
mov cx, 1
int 10h
add dh, 7
add dl, 7
mov ah, 02
int 10h
mov ah, 09h
mov al, character
mov bl, 2
mov cx, 1
int 10h
cmp al, '*'
je leavePrintCard
push dx
genRand 4, 1
cmp dx, 1
je heart
cmp dx, 2
je spade
cmp dx, 3
je diamond
jmp club
heart:
pop dx
drawHeart dh, dl
jmp leavePrintCard
spade:
pop dx
drawSpade dh, dl
jmp leavePrintCard
diamond:
pop dx
drawDiamond dh, dl
jmp leavePrintCard
club:
pop dx
drawClub dh, dl
leavePrintCard:
pop dx
pop cx
pop bx
pop ax
ret
endp
printChar MACRO r, col, char
mov dh, r
mov dl, col
mov ah, 02
int 10h
mov ah, 09h
mov al, char
mov bl, 2
mov cx, 1
int 10h
ENDm
drawHeart MACRO row, col
mov dh, row
mov dl, col
sub dh, 4
sub dl, 6
printChar dh, dl, 40
dec dh
inc dl
printChar dh, dl, '_'
add dh, 2
printChar dh, dl, 92
inc dl
inc dh
printChar dh, dl, 92
inc dl
printChar dh, dl, 47
inc dl
dec dh
printChar dh, dl, 47
sub dh, 2
printChar dh, dl, '_'
inc dl
inc dh
printChar dh, dl, 41
sub dl, 3
printChar dh, dl, 92
inc dl
printChar dh, dl, 47
endm
drawDiamond MACRO row, col
mov dh, row
mov dl, col
sub dh, 4
sub dl, 5
printChar dh, dl, 47
inc dl
dec dh
printChar dh, dl, 47
inc dl
printChar dh, dl, 92
inc dl
add dh, 1
printChar dh, dl, 92
inc dh
printChar dh, dl, 47
dec dl
inc dh
printChar dh, dl, 47
dec dl
printChar dh, dl, 92
dec dl
dec dh
printChar dh, dl, 92
endm
drawSpade MACRO row, col
mov dh, row
mov dl, col
sub dh, 3
sub dl, 6
printChar dh, dl, 40
inc dl
dec dh
printChar dh, dl, 47
inc dl
dec dh
printChar dh, dl, 127
inc dl
inc dh
printChar dh, dl, 92
inc dl
inc dh
printChar dh, dl, 41
dec dl
printChar dh, dl, '_'
dec dl
printChar dh, dl, ','
dec dl
printChar dh, dl, '_'
inc dh
inc dl
printChar dh, dl, 'I'
endm
drawClub MACRO row, col
mov dh, row
mov dl, col
sub dh, 3
sub dl, 6
printChar dh, dl, 40
inc dl
dec dh
printChar dh, dl, 40
inc dl
dec dh
printChar dh, dl, '_'
inc dl
inc dh
printChar dh, dl, 41
inc dl
inc dh
printChar dh, dl, 41
dec dl
printChar dh, dl, '_'
dec dl
printChar dh, dl, 'X'
dec dl
printChar dh, dl, '_'
inc dh
inc dl
printChar dh, dl, 'Y'
endm
dispPHand PROC
MOV BL, playerSize
LEA DX, playerHand
ADD DX, currPCard
MOV SI, DX
SUB BX, currPCard
dh2:
CMP BL, 0
JE exdispPHand
MOV AL, [SI]
setCursor playerRow, playerCol
dispCardNew 15,AL, currPCard
INC currPCard
INC playerCol
INC SI
DEC BL
JMP dh2
exdispPHand:
RET
dispPHand ENDP
checkP21 PROC
saveRegs
LEA BP, msgClear
printstr 25,10
printstr 26,10
MOV BX, 00 ;TOTAL VALUE
MOV CX, 00 ;TOTAL NUMBER OF ACES
LEA DX, playerHand
MOV SI, DX
ch1:
MOV AX, [SI]
XOR AH, AH
INC SI
CMP AX, 0
JE calcP
CMP AX, 1 ;check if ace. Value of ACE's will be calculated at the end
JE ACE1
CMP AX, 10 ;check if face card
JAE FACE1 ;add 10 if it is
ADD BL, AL ;add value of non-ace card to the total
JMP ch1
FACE1:
ADD BL, 10
JMP CH1
ACE1:
INC CL
JMP ch1
addAce1:
ADD BL, 11
DEC CL
calcP:
CMP CL, 0 ;check if there are any aces. if not then leave
JE exChp21
CMP BL, 10 ;check if current total is <= 10
JLE addAce1 ;if it is then add 11 for that ACE's values
ch2:
ADD BL, 1 ;otherwise add 1 for it's value
LOOP ch2
exChp21:
MOV pScore, BL
CALL updatePScore
CALL checkPScore
restoreRegs
RET
checkP21 ENDP
updatePScore PROC
saveRegs
XOR CX, CX
MOV AL, pScore
; Convert the hex value to decimal and store the digits in the stack
MOV AH, 0 ; Initialize the quotient to 0
MOV CX, 10 ; Initialize the divisor to 10
DIV_LOOP:
XOR DX, DX ; Clear the DX register
DIV CX ; Divide the quotient by the divisor
ADD DL, '0' ; Convert the remainder to ASCII
PUSH DX ; Save the digit on the stack
CMP AL, 0 ; Check if the quotient is 0
JNE DIV_LOOP ; If not, continue dividing
CMP pScore, 9
JA PRINT
MOV AX, 30H
PUSH AX
PRINT:
setCursor 14, 28
; Pop each digit from the stack and print it to the screen
POP AX
MOV AH, 09H ; Set up the function code for printing a character
MOV CX, 1
MOV BL, 2
INT 10H ; Call the BIOS to print the character
setCursor 14, 29
; Pop each digit from the stack and print it to the screen
POP AX
MOV AH, 09H ; Set up the function code for printing a character
MOV CX, 1
MOV BL, 2
INT 10H ; Call the BIOS to print the character
restoreRegs
RET
updatePScore ENDP
checkPScore PROC
saveRegs
CMP pScore, 21
JA pBust
; Check if Pscore is equal to 21
CMP pScore, 21
JE pWin
; If Pscore is less than 21, continue the game
JMP pContinue
pBust:
; Player has bust and lost the game
LEA BP, msgPbust
printstr 25, 10
CALL endGame
pWin:
LEA BP, msgPwin
printstr 25, 10
CALL endGame
pContinue:
restoreRegs
RET
checkPScore ENDP
askHitOrStay PROC
saveRegs
; Display message to ask player to hit or stay
LEA BP, msgAskHitOrStay
printstr 25, 10
; Wait for player input (h for hit, s for stay)
CALL KEYPRESS
; Check if player chose to hit or stay
cmp al, 'h'
je hit
cmp al, 's'
je stay
; Invalid input, ask again
LEA BP, msgInvalidInput
printstr 25, 10
JMP askHitOrStay
hit:
; Player chose to hit
LEA BP, msgPlayerHit
printstr 26, 10
JMP hitP
restoreRegs
RET
stay:
; Player chose to stay
LEA BP, msgPlayerStay
printstr 26, 10
JMP stayP
restoreRegs
RET
askHitOrStay ENDP
hitP PROC
CALL dealPlayer
CALL dispPHand
CALL checkP21
CALL askHitOrStay
RET
hitP ENDP
STAYP PROC
DEC dealerCol
setCursor dealerRow, dealerCol
INC dealerCol
MOV BL, 1
LEA DX, dealerHand
ADD DX, 1
MOV SI, DX
MOV AL, [SI]
dispCardNew 3, AL, 1
LEA BP, msgClear
printstr 25,10
printstr 26,10
LEA BP, DTotalMsg
PRINTSTR 14, 35
d17Check:
CALL CalcDTotal
CALL showDScore
; Check if dealer's total is less than 17
CMP dScore, 17
JL DEALP
; Check if dealer has bust
CMP dScore, 22
JAE PLAYER_WIN
MOV AH, dScore
MOV AL, pScore
; Check if dealer has higher hand than player
CMP AH, AL
JG DEALER_WIN
; If none of the above conditions are true, player wins
JMP PLAYER_WIN
DEALP:
CALL dealDealer
CALL dispDHand
JMP d17Check
PLAYER_WIN:
LEA BP, msgPlayerWin
PRINTSTR 25, 10
JMP END_GAME
DEALER_WIN:
LEA BP, msgDealerWin
PRINTSTR 25, 0
JMP END_GAME
END_GAME:
MOV AX, 4C00H
INT 21H
STAYP ENDP
CalcDTotal PROC
saveRegs
MOV BX, 0 ;TOTAL VALUE
MOV CX, 0 ;TOTAL NUMBER OF ACES
LEA DX, dealerHand
MOV SI, DX
ch3:
MOV AX, [SI]
XOR AH, AH
INC SI
CMP AX, 0
JE calcD
CMP AX, 1 ;check if ace. Value of ACE's will be calculated at the end
JE ACE2
CMP AX, 10 ;check if face card
JAE FACE2 ;add 10 if it is
ADD BL, AL ;add value of non-ace card to the total
JMP ch3
FACE2:
ADD BL, 10
JMP CH3
ACE2:
INC CX
JMP ch3
addAce2:
ADD BL, 11
DEC CX
calcD:
CMP CX, 0 ;check if there are any aces. if not then leave
JE exChd21
CMP BL, 10 ;check if current total is <= 10
JLE addAce2 ;if it is then add 11 for that ACE's values
ch4:
ADD BL, 1 ;otherwise add 1 for it's value
LOOP ch4
exChd21:
MOV dScore, BL
;CALL checkDScore
restoreRegs
RET
CalcDTotal ENDP
showDScore PROC
saveRegs
XOR CX, CX
MOV AL, dScore
; Convert the hex value to decimal and store the digits in the stack
MOV AH, 0 ; Initialize the quotient to 0
MOV CX, 10 ; Initialize the divisor to 10
DIV_LOOP2:
XOR DX, DX ; Clear the DX register
DIV CX ; Divide the quotient by the divisor
ADD DL, '0' ; Convert the remainder to ASCII
PUSH DX ; Save the digit on the stack
CMP AL, 0 ; Check if the quotient is 0
JNE DIV_LOOP2 ; If not, continue dividing
CMP dScore, 9
JA PRINT2
MOV AX, 30H
PUSH AX
PRINT2:
setCursor 14, 50
; Pop each digit from the stack and print it to the screen
POP AX
MOV AH, 09H ; Set up the function code for printing a character
MOV CX, 1
MOV BL, 2
INT 10H ; Call the BIOS to print the character
setCursor 14, 51
; Pop each digit from the stack and print it to the screen
POP AX
MOV AH, 09H ; Set up the function code for printing a character
MOV CX, 1
MOV BL, 2
INT 10H ; Call the BIOS to print the character
restoreRegs
RET
showDScore ENDP
endGame PROC
MOV AX, 4C00H
INT 21H
endGame ENDP
strLen PROC
INC CX
MOV AX,[SI]
INC SI
CMP AL, '$'
JNE strLen
RET
strLen ENDP
keyPress PROC
MOV AH,08H
INT 21H
CALL toLower
RET
keyPress ENDP
toLower PROC
CMP AL, 65 ;'A'
JB CONTINUE ;IF THE KEY IS LESS THAN 'A' IT DOES NOTHING
CMP AL, 90 ;'Z'
JA CONTINUE ;IF THE KEY IS GREATER THAN 'Z' IT DOES NOTHING
ADD AL, 32 ;Converts uppercase to lowercase.
CONTINUE:
RET
toLower ENDP
saveRegs PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
RET
saveRegs ENDP
;Random number generator from https://github.com/ade3l/Pseudo-random-number-generator
genRand MACRO uLimit, lLimit
PUSH AX
PUSH BX
PUSH CX
;calculate next value and make that the new seed
MOV AX, mult
MOV BX, SEED
MUL BX
ADD AX, incr
MOV SEED, AX
shr ax, 4
MOV BX, 00
;Reducing the number to in the required range
MOV BL, uLimit
MOV DX,0
DIV BX
ADD DL, lLimit
POP CX
POP BX
POP AX
ENDM
createSeed PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
;We will use Low-order part of clock count as the seed
MOV AH, 00H
INT 1AH
MOV SEED, DX
POP DX
POP CX
POP BX
POP AX
RET
createSeed ENDP
END