Irvine assmembly x86 guess the number between 1-100 game? I have successfully be
ID: 3772052 • 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?
.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
Respective c program:
----------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main()
{
int totalAttempts = 10,i,num,randomNum;
randomNum = rand() % 100 + 1;
for(i=0;i<10;i++){
printf(" You have a total of %d attempts",totalAttempts);
printf(" Hi Guess the number: ");
scanf("%d",&num);
if(num > randomNum){
printf(" The number is higher");
}
else if(num < randomNum){
printf(" The number is lower");
}
else{
printf("YOU GUESSED RIGHT");
}
if(totalAttempts == 0){
printf("YOU LOSE");
}
totalAttempts--;
}
printf("GAME OVER");
return 0;
}
.LC0:
.ascii "
You have a total of %d attempts"
.LC1:
.ascii "
Hi Guess the number: "
.LC2:
.ascii "%d"
.LC3:
.ascii "
The number is higher"
.LC4:
.ascii "
The number is lower"
.LC5:
.ascii "YOU GUESSED RIGHT"
.LC6:
.ascii "YOU LOSE"
.LC7:
.ascii "GAME OVER"
main:
push {r7, lr}
sub sp, sp, #16
add r7, sp, #0
mov r3, #10
str r3, [r7, #12]
bl rand
mov r2, r0
movw r3, #34079
movt r3, 20971
smull r1, r3, r3, r2
asr r1, r3, #5
asr r3, r2, #31
subs r3, r1, r3
mov r1, #100
mul r3, r1, r3
subs r3, r2, r3
add r3, r3, #1
str r3, [r7, #4]
mov r3, #0
str r3, [r7, #8]
b .L2
.L7:
movw r0, #:lower16:.LC0
movt r0, #:upper16:.LC0
ldr r1, [r7, #12]
bl printf
movw r0, #:lower16:.LC1
movt r0, #:upper16:.LC1
bl printf
mov r3, r7
movw r0, #:lower16:.LC2
movt r0, #:upper16:.LC2
mov r1, r3
bl scanf
ldr r2, [r7, #0]
ldr r3, [r7, #4]
cmp r2, r3
ble .L3
movw r0, #:lower16:.LC3
movt r0, #:upper16:.LC3
bl printf
b .L4
.L3:
ldr r2, [r7, #0]
ldr r3, [r7, #4]
cmp r2, r3
bge .L5
movw r0, #:lower16:.LC4
movt r0, #:upper16:.LC4
bl printf
b .L4
.L5:
movw r0, #:lower16:.LC5
movt r0, #:upper16:.LC5
bl printf
.L4:
ldr r3, [r7, #12]
cmp r3, #0
bne .L6
movw r0, #:lower16:.LC6
movt r0, #:upper16:.LC6
bl printf
.L6:
ldr r3, [r7, #12]
add r3, r3, #-1
str r3, [r7, #12]
ldr r3, [r7, #8]
add r3, r3, #1
str r3, [r7, #8]
.L2:
ldr r3, [r7, #8]
cmp r3, #9
ite gt
movgt r3, #0
movle r3, #1
uxtb r3, r3
cmp r3, #0
bne .L7
movw r0, #:lower16:.LC7
movt r0, #:upper16:.LC7
bl printf
mov r3, #0
mov r0, r3
add r7, r7, #16
mov sp, r7
pop {r7, pc}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.