Reputation: 45
I am trying to code a little application where a random number between 1-5 is generated using the system clock and the user has to guess the number and a message is displayed if the guess is correct.
Here is my code so far:
org 0x100
;Guess:
MOV AH,2CH ;DOS CALL TO GET SYSTEM CLOCK
INT 21H ;EXECUTE INTERRUPT 21H
MOV AL,DL ;RESULT RETURNED IN DL
MOV AH,0 ;CLEAR THE HIGH BYTE
MOV CL,20 ;GET DIVISOR
DIV CL ;DIVIDE VALUE IN AX BY VALUE IN CL
MOV BL,AL ;MOVE RESULT OF DIVISION INTO BL
INC BL ;INCREMENT BL TO GET NO BETWEEN 1-5
;Prompt:
JMP START
MSG DB "Enter a number between 1-5:$"
START:
MOV DX, OFFSET MSG
MOV AH, 09h
INT 21h
;User input:
MOV AH, 1h ;GET INPUT FROM THE KEYBOARD
INT 21h ;THE CHARACTER INPUT IS STORED IN AL
;The Comparison:
;MOV AX,AL ;LOAD VALUE OF AL (USER INPUT)
CMP AL,BL ;COMPARE WITH BL
JE Equal ;IF EQUAL JUMP TO Equal
JNE Not_Equal;IF NOT EQUAL JUMP TO Not_Equal
Equal:
ABC DB "Your guess was correct!$"
MOV DX, OFFSET ABC
MOV AH, 09h
INT 21h
Not_Equal:
DEF DB "Your guess was incorrect!$"
MOV DX, OFFSET DEF
MOV AH, 09h
INT 21h
My problem is that it just goes into an infinite loop.
Upvotes: 0
Views: 1120
Reputation: 58507
You've got a few different problems in your code:
You can't mix code and data like this:
ABC DB "Your guess was correct!$"
MOV DX, OFFSET ABC
The CPU can't tell the difference between code and data and will try to execute whatever it encounters. Move the result strings to the same place where you've got the MSG
string.
You're comparing numeric values with their corresponding ASCII characters:
MOV AH, 1h ;GET INPUT FROM THE KEYBOARD
INT 21h ;THE CHARACTER INPUT IS STORED IN AL
If you enter the digit 2
in the terminal you'd get the character '2'
(which is equal to 50) in AL
. To successfully compare that against what you've got in BL
you have to subtract '0'
from AL
.
You'll always print the "Your guess was incorrect" string. After the code that prints the "Your guess was correct" string there's no jump to skip the next print:
ABC DB "Your guess was correct!$"
MOV DX, OFFSET ABC
MOV AH, 09h
INT 21h
; Execution will simply continue here since there's nothing that changes the
; flow of execution.
Not_Equal:
You're not terminating the program properly. At the end of your program you should add something like:
MOV AX,4C00h
INT 21h ; Terminate with exit code 00h
Upvotes: 1