I'm currently studying for an exam and I don't understand the answer to this assembly question. link
Basically the correct answer is apparently jle, but to me it seems it should be jge. For example, if you set a=4 in the C code, you should get 1. Based on my reading of the assembly, this is true for it as well. My logic is as follows: If (2>=4) return 0 else return 1.
I think I am misunderstanding some basic aspect of machine code but I've read references on cmp and jge/jle and I still am confused. Thanks for any help
CodePudding user response:
The assembly in that image looks wrong. The cmp instruction subtracts 2 from a
, but jumps to the return 0 path if the result is positive or zero. Oops.
CodePudding user response:
You're comparing [rbp-8] and 2 then jumping to return 0 if the result is greater than or equal 2? I don't follow AT&T syntax very well, but why compare it to 2? Also you wouldn't jump to return 0 if the result is greater than or equal to, rather you would return 1 in that case.
The correct solution would be to compare [rbp-8] and 3, then jumping to return 0 if less than otherwise return 1.
push rbp
Mov [rbp-8],5
cmp [rbp-8],3
jl L1
mov rax,1
jmp L2
L1:
xor rax,rax
L2:
pop rbp
ret
If I were strictly following your solution then the correction would be here
main:
push %rbp
mov %rsp,%rbp
movq $0x5,-0x8(%rbp)
cmpq $0x3, -0x8(%rbp) ;;this need to be 0x3
jl L1 ;;this needs to be jl;
mov $0x1,