'x86 jge vs jle in assembly [duplicate]
I'm currently studying for an exam and I don't understand the answer to this assembly question: This asm is GCC's output from the C source, except for the jge instruction. What instruction should be there?

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.
Solution 1:[1]
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.
Solution 2:[2]
In effect, the logic involved when using branches/jumps is reversed - you are testing for the case where the consequent is being bypassed. Look at this pseudo-code:
if something is true then skip to L2
do the false case here
skip to L2
L1:
do the true case here
L2:
continue from here
This page regarding compiler implementation discusses the issues involved in implementing high-level conditional statements in an assembly language.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joshua |
| Solution 2 |
