Irvine assmembly x86 guess the number between 1-100 game? I have successfully be
ID: 3772035 • Letter: I
Question
Irvine assmembly x86 guess the number between 1-100 game? I have successfully been able to make it run, but when the program runs it outputs incorrectly with the loop. Is there a solution to make this code work?
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.const
.data
; MSG
startMsg BYTE "HI, Guess the number",0
attempts BYTE "You have a total of 10 attempts ",0
lower BYTE "The number is lower ",0
higher BYTE "The number is higher ",0
Youhave BYTE "you have ",0
guess BYTE " guess the number: ",0
won BYTE "YOU GUESSED RIGHT",0
loss BYTE "YOU LOSE",0
over BYTE "GAME OVER",0
; Empty variables
minNum BYTE 0
maxNum BYTE 100
randomNumber BYTE ?
myNumber BYTE ?
loopCounter BYTE ?
.code
main PROC
call Randomize
mov edx, offset startMsg
call writeString
call Crlf
mov edx, offset lower
call writeString
call readInt
mov minNum, al
mov edx, offset higher
call writeString
call readInt
mov maxNum, al
mov edx, offset attempts
call writeString
call readInt
mov loopCounter, al
call Crlf
call Range ; calling Range function
call RandomRange
add al, minNum
mov randomNumber, al
call Looper
YOULOSE: ; Display if you loose
mov edx, offset loss
call writeString
call Crlf
call WaitMsg
invoke ExitProcess,0
main endp
goLower:
mov edx, offset lower
call writeString ;call Looper
call Crlf
RET
goHigher:
mov edx, offset higher
call writeString ;call Looper
call Crlf
RET
WINNER:
mov edx, offset won
call writeString
jmp Done
call Crlf
Done:
mov edx, offset over
call readString
call Crlf
Looper proc
movzx ecx, loopCounter
MSG_Attempts:
mov edx, offset Youhave
call writeString
mov eax, ecx
call writeDec
mov edx, offset guess
call writeString
call readInt
mov myNumber, al
call Compare
loop MSG_Attempts
ret
Looper endp
Compare proc
mov myNumber, al
mov bl, myNumber
cmp bl, randomNumber
call Jumps
ret
Compare endp
Jumps proc
jg goLower
jl goHigher
je Winner
ret
Jumps endp
Range proc
movzx eax, maxNum
sub al, minNum
inc eax
ret
Range endp
END main
Explanation / Answer
Answer:
As analyzed with your code the problem is with the loopCounter(number of attempts). Each time you need to decrement the loopCounter and check for 0. If loopCouter is 0 then you lose the game because with number of attempts you failed to guess the number.
Modified code:
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.const
.data
; MSG
startMsg BYTE "HI, Guess the number",0
attempts BYTE "You have a total of 10 attempts ",0
lower BYTE "The number is lower ",0
higher BYTE "The number is higher ",0
Youhave BYTE "you have ",0
guess BYTE " guess the number: ",0
won BYTE "YOU GUESSED RIGHT",0
loss BYTE "YOU LOSE",0
over BYTE "GAME OVER",0
; Empty variables
minNum BYTE 0
maxNum BYTE 100
randomNumber BYTE ?
myNumber BYTE ?
loopCounter BYTE ?
.code
main PROC
call Randomize
mov edx, offset startMsg
call writeString
call Crlf
mov edx, offset lower
call writeString
call readInt
mov minNum, al
mov edx, offset higher
call writeString
call readInt
mov maxNum, al
mov edx, offset attempts
call writeString
call readInt
mov loopCounter, al
call Crlf
call Range ; calling Range function
call RandomRange
add al, minNum
mov randomNumber, al
call Looper
YOULOSE: ; Display if you loose
mov edx, offset loss
call writeString
call Crlf
call WaitMsg
invoke ExitProcess,0
main endp
goLower:
mov edx, offset lower
call writeString ;call Looper
call Crlf
RET
goHigher:
mov edx, offset higher
call writeString ;call Looper
call Crlf
RET
WINNER:
mov edx, offset won
call writeString
jmp Done
call Crlf
Done:
mov edx, offset over
call readString
call Crlf
Looper proc
movzx ecx, loopCounter
MSG_Attempts:
; check whether you have enough attempts
; If attempts are not available then jump to YOULOSE
cmp cl,0
je YOULOSE
mov edx, offset Youhave
call writeString
mov eax, ecx
call writeDec
mov edx, offset guess
call writeString
call readInt
mov myNumber, al
call Compare
;decrement the loopCounter(number of attempts) on each iteration
dec ecx
loop MSG_Attempts
ret
Looper endp
Compare proc
mov myNumber, al
mov bl, myNumber
cmp bl, randomNumber
call Jumps
ret
Compare endp
Jumps proc
jg goLower
jl goHigher
je Winner
ret
Jumps endp
Range proc
movzx eax, maxNum
sub al, minNum
inc eax
ret
Range endp
END main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.