I’ve been having a lot of trouble figuring this out. So I’m using inline Assembly and I’m basically trying to compare a number the user types in using Scanf with the number “$20.”
But I think it’s comparing the location rather than the actual value.
Here is what I have:
asm(
"main: \n"
" \n"
"push $msg \n"
"call _printf \n"
"add $4, %esp \n"
" \n"
"push $gus \n"
"push $fmt \n"
"call _scanf \n"
"add $8, %esp \n"
" \n"
"movl $20, %eax \n"
"movl $gus, %ebx \n"
"cmp %eax, %ebx \n"
"jg lower \n"
"jl higher \n"
" \n"
"lower: \n"
"push $lss \n"
"call _printf \n"
"add $4, %esp \n"
"jmp next \n"
" \n"
"higher: \n"
"push $gtr \n"
"call _printf \n"
"add $4, %esp \n"
"jmp next \n"
" \n"
"next: \n"
"jmp main \n"
"ret \n"
" \n"
".section .data \n"
"msg: .asciz \"Please enter your guess: \" \n"
"gtr: .asciz \"Higher! \" \n"
"lss: .asciz \"Lower! \" \n"
// "crr: .asciz \"Please enter your guess: \" \n"
"fmt: .asciz \"%d\" \n"
".double 100 \n"
".byte 0 \n"
"gus: .double 100 \n"
".byte 0 \n"
);
Yes, I know there really isn’t a point in this code for actual use. The purpose of me writing this is really just to figure out how comparisons.
But my problem is, at:
"movl $20, %eax \n"
"movl $gus, %ebx \n"
"cmp %eax, %ebx
It’s supposed to compare 20 with the “gus”, or the “guess” the user typed in. But it always says “Lower” no matter what they type in.
You load the address of
gus, not its value, to EBX before comparing.Your bigger problem is that you try to do way too much with inline assembly.
You want to compare in assembly? Very well, compare. But why call
printfandscanfand all the rest?