Im using the following code at the end of my simple console program, it seems erratic - works sometimes..
Is there anything wrong here?
.data
y_string db "yY"
n_string db "nN"
.code
ask_again:
mov esi,input(13,10,13,10,"The game is over, Play again (y/n)")
lea edi,y_string
cmpsb
je restart
inc edi
cmpsb
je restart
lea edi,n_string
cmpsb
je quit
inc edi
cmpsb
jne ask_again
quit:
exit
If i were you, i would throw away the cmps, and do something like: :P
ask_again:
mov esi, input(13,10,13,10,"The game is over, Play again (y/n)")
movzx eax, byte ptr [esi] ; get the character
or eax, 20h ; make it lowercase
cmp eax, 'y'
je restart
cmp eax, 'n'
jne ask_again
quit:
exit
Less code, nothing in data section. Also, when you are referring to something in the data section, just use 'mov xxx, OFFSET y_string' instead of lea instruction, you save one byte.. :wink
Thanks for the tips, code now works! :thumbu
Any idea why the original code didnt work?
cheers
The original didn't work because the cmpsb increases the edi and esi automatically, so your first compare would work, but after that esi would no longer point to the original character. Also for the same reason the two 'inc edi' would not be needed.
You "could" make it like this:
.data
the_string db "yYnN"
.code
ask_again:
; assuming that direction flag is cleared..
mov esi, input(13,10,13,10,"The game is over, Play again (y/n)")
mov edi, OFFSET the_string
cmpsb ; already does 'inc esi' and 'inc edi'
je restart
dec esi ; go back esi..
cmpsb
je restart
dec esi ; back to the original character
cmpsb
je quit
dec esi ; and again
cmpsb
jne ask_again
quit:
exit
..but it's an total overkill to use cmps on one character comparing. Also you must make sure the direction flag is correctly set, so the cmpsb won't decrease the edi and esi instead.
Got it!
thanks for the advice