I don’t understand the JG/JNLE/JL/JNGE instructions, which come after CMP.
for example, If I have:
CMP al,dl
jg label1
When al=101; dl =200.
On what we ask the jg? Is it on al>dl? or al-dl>0?
Same prolbem on the next code:
test al,dl
jg label1
I don’t understand what we compare, and on what we ask the "jg".
In other words, I don’t understand when we would jump to label1, and when we wouldn’t.
For AT&T syntax, note that the subtraction does the other direction, so read right to left instead of left to right:
When you do a
cmp a,b, the flags are set as if you had calculateda - b. Then the conditional jump instructions check those flags to see if the jump should be made.In other words, the first block of code you have (with my comments added):
would jump to
label1if and only ifalwas greater thandl.You’re probably better off thinking of it as
al > dlbut the two choices you have there are mathematically equivalent:You need to be careful when using
jginasmuch as it assumes your values were signed. So, if you compare the bytes101(101in two’s complement) with200(-56in two’s complement), the former will actually be greater. If that’s not what was desired, you should use the equivalent unsigned comparison.See here for more detail on jump selection, reproduced below for completeness, in the order of:
JZ
Jump if zero
JNZ
Jump if not zero
JPE
Jump if parity even
JPO
Jump if parity odd
JECXZ
JRCXZ
Jump if ECX is zero
Jump if RCX is zero
ECX = 0
RCX = 0
JNAE
JC
Jump if not above or equal
Jump if carry
JAE
JNC
Jump if above or equal
Jump if not carry
JNA
Jump if not above
JNBE
Jump if not below or equal
JNGE
Jump if not greater or equal
JNL
Jump if not less
JNG
Jump if not greater
JNLE
Jump if not less or equal