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.
Best Answer
When you do a
cmp a,b
, the flags are set as if you had calculateda - b
.Then the
jmp
-type 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
label1
if and only ifal
was greater thandl
.You're probably better off thinking of it as
al > dl
but the two choices you have there are mathematically equivalent: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. First the ones where signed-ness is not appropriate:
Then the unsigned ones:
And, finally, the signed ones: