Random Card Number Simulator
Generates a Random Card Number in x86 Assembly
INCLUDE Irvine32.inc
.data
promptMessage BYTE "Do you want to draw a card? (Y/N)",13,10,0
drawMessage BYTE 13,10,"Please draw a card. Press any key.",13, 10,0
endMessage BYTE "Thank you for playing. Come back soon.",13,10,0
cardMessage BYTE "Your card is ",0
endCardMessage BYTE ".",10,10,0
.code
main PROC
call Randomize ; randomize to seed the random number generator
prompt:
mov edx, OFFSET promptMessage ; prompt address
call WriteString ; print the prompt string on screen
call ReadChar ; read character from user
call WriteChar ; echo the read char
call CrLf ; leave an empty line
cmp al, 'N'
je terminate ; if the answer was N, terminate program
cmp al, 'n'
je terminate ; if the answer was n, terminate program
cmp al, 'Y'
je start ; if the answer was Y, continue
cmp al, 'y'
je start ; if the answer was n, continue
jmp prompt ; if it's any other character, try again
start:
mov edx, OFFSET drawMessage ; draw message address
call WriteString ; print the message to draw a card on screen
call ReadChar ; read character from user
mov edx, OFFSET cardMessage ; card message address
call WriteString ; print the message to show the generated card on screen
call generateCardNumber ; generate the card number
cmp eax, 'A' ; compare with A
jg printFigure ; if it's a letter, print as figure
call WriteDec ; else, write as number
jmp printSuite ; go to print the suite
printFigure:
call WriteChar ; if figure, write as a character
printSuite:
call generateCardSuite ; generate the card suite
call WriteChar ; print the suite
mov edx, OFFSET endCardMessage ; print the end of the card message
call WriteString
jmp prompt ; repeat to prompt user again
terminate:
mov edx, OFFSET endMessage ; end message address
call WriteString ; print the ending message on screen
exit ; exit the program
main ENDP
; Procedure to generate the card number of figure
generateCardNumber PROC
mov eax, 13 ; use the range 0-12
call RandomRange ; generate the random number
add eax, 1 ; add 1 to have the range 1-13
cmp eax, 1 ; compare with 1
je ace ; if it's 1, go to ace
cmp eax, 11 ; compare with 11
je jack ; if it's 11, go to jack
cmp eax, 12 ; compare with 12
je queen ; if it's 11, go to queen
cmp eax, 13 ; compare with 13
je king ; if it's 11, go to king
jmp endGenerator ; in any other case (2 to 10), return the number
ace:
mov eax, 'A' ; return character A
jmp endGenerator ; end procedure
jack:
mov eax, 'J' ; return character J
jmp endGenerator ; end procedure
queen:
mov eax, 'Q' ; return character Q
jmp endGenerator ; end procedure
king:
mov eax, 'K' ; return character K
endGenerator:
ret
generateCardNumber ENDP
; Procedure to generate the suite of figure
generateCardSuite PROC
mov eax, 4 ; use the range 0-3
call RandomRange ; generate the random number
add eax, 1 ; add 1 to have the range 1-4
cmp eax, 1 ; compare with 1
je spade ; if it's 1, go to spade
cmp eax, 2 ; compare with 2
je heart ; if it's 2, go to heart
cmp eax, 3 ; compare with 3
je diamond ; if it's 3, go to diamond
club: ; the only remaining option is 4: clubs
mov eax, 'c' ; use the character c
jmp endGenSuite ; return the character
spade:
mov eax, 's' ; use the character s
jmp endGenSuite ; return the character
heart:
mov eax, 'h' ; use the character h
jmp endGenSuite ; return the character
diamond:
mov eax, 'd' ; use the character d
endGenSuite:
ret
generateCardSuite ENDP
END main