I'm trying to make a guessing game in NASM assembly using a kernel. The boot loader just points to the kernel file. The kernel file stores everything. All the functions and variables for the game; while Ive gotten as far to print instructions for the game out. The problem arises; when I use an if statement. It prints out the winning message whether it's the correct number or not. I want the kernel to print the correct winning message, when the correct number is chosen. My code for the kernel is below ....
org 0
bits 16
_start:
push cs
pop ds
mov si, msg
jmp BeginLoop
PrintLoop:
mov ah, 0x0E
int 0x10
BeginLoop:
mov al, [si]
inc si
or al, al
jz GameLoop
test al, al
jnz PrintLoop
GameLoop:
xor ax, ax
int 16h
mov ah, 0x0E
int 0x10
cmp ax, 0x5
jne GameWon
GameWon:
mov si, won
mov al, [si]
inc si
or al, al
jz GameLoop
test al, al
jnz PrintLoop
cli
hlt
jmp $-2
msg db 'Welcome To My Guessing Game: ', 10, 13, 'Pick A Number Between 1 - 10 ', 10, 13, 0
won db 'You Guessed The Correct Number!', 10, 13, 0
loss db 'You Guessed The Incorrect Number!' 10, 13
TIMES 512 - ($ - $$) db 0
Updated code that works
org 0
bits 16
_.start:
push cs
pop ds
mov si, msg
jmp Print
GameLoop:
mov ah, 00h
int 1ah
mov ax, dx
xor dx, dx
mov cx, 10
div cx
add dl, '0'
mov ah, 0x00
int 16h
mov ah, 0x0E
int 0x10
GameCondition:
mov si, won
cmp al, dl
je GameWon
jmp GameLost
GameLost:
mov si, loss
jmp Print
GameWon:
jmp Print
Print:
jmp BeginLoop
PrintLoop:
mov ah, 0x0E
int 0x10
BeginLoop:
mov al, [si]
inc si
or al, al
jz GameLoop
test al, al
jnz PrintLoop
ret
; -------------------
msg db 'Welcome To My Guessing Game: ', 10, 13, 'Pick A Number Between 0 - 9 ', 10, 13, 0
won db 'You Guessed The Correct Number!', 10, 13, 0
loss db 'You Guessed The Incorrect Number!', 10, 13, 0
times 512 - ($-$$) db 0
There are a couple of things wrong. First is, as @vitsoft stated earlier, that you don't have a GameLoss condition. You are checking if something is 5 and basically ignoring the condition.
To fix that the above code can be:
Also there is this thing
You are moving 0x0E to
ahand then expectingaxto have a value of 0x5. This is not possible since as far as I knowint 0x10doesn't modifyah. Therefore the code should be:Here is a simple guessing game code, I haven't tried this per se, try it and adapt it to yourself:
Edit:
This code is wrong:
This doesn't even make sense:
you are comparing two values and if they are equal executing
GameWon. If they are not equal you are not doing anything and your code continues to execute and reachesGameWonanyways. I have said this problem several times throughout this post, it is actually the second sentence of the post. To fix the code it should be:Now you are checking if
alanddlare the same. Then if they are not you are jumping toGameLost, if they are equal you automatically executeGameWon.This combined with @SepRoland's comment makes your final code like this: