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 calculated a - 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):
cmp al, dl ; set flags based on the comparison.
jg label1 ; then jump based on the flags.
would jump to label1
if and only if al
was greater than dl
.
You're probably better off thinking of it as al > dl
but the two choices you have there are mathematically equivalent:
al > dl
(al - dl) > (dl - dl) [subtract dl from both sides]
(al - dl) > 0 [cancel the terms on the right hand side]
You need to be careful when using jg
inasmuch as it assumes your values were signed. So, if you compare the bytes 101
(101
in two's complement) with 200
(-56
in 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:
Instruction | Description | Signed-ness | Conditions checked |
---|---|---|---|
JO | Jump if overflow | OF = 1 | |
JNO | Jump if not overflow | OF = 0 | |
JS | Jump if sign | SF = 1 | |
JNS | Jump if not sign | SF = 0 | |
JE JZ |
Jump if equal Jump if zero |
ZF = 1 | |
JNE JNZ |
Jump if not equal Jump if not zero |
ZF = 0 | |
JP JPE |
Jump if parity Jump if parity even |
PF = 1 | |
JNP JPO |
Jump if no parity Jump if parity odd |
PF = 0 | |
JCXZ JECXZ JRCXZ |
Jump if CX is zero Jump if ECX is zero Jump if RCX is zero |
CX = 0 ECX = 0 RCX = 0 |
|
JB JNAE JC |
Jump if below Jump if not above or equal Jump if carry |
unsigned | CF = 1 |
JNB JAE JNC |
Jump if not below Jump if above or equal Jump if not carry |
unsigned | CF = 0 |
JBE JNA |
Jump if below or equal Jump if not above |
unsigned | CF = 1 or ZF = 1 |
JA JNBE |
Jump if above Jump if not below or equal |
unsigned | CF = 0 and ZF = 0 |
JL JNGE |
Jump if less Jump if not greater or equal |
signed | SF <> OF |
JGE JNL |
Jump if greater or equal Jump if not less |
signed | SF = OF |
JLE JNG |
Jump if less or equal Jump if not greater |
signed | ZF = 1 or SF <> OF |
JG JNLE |
Jump if greater Jump if not less or equal |
signed | ZF = 0 and SF = OF |