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.
The command JG simply means: Jump if Greater. The result of the preceding instructions is stored in certain processor flags (in this it would test if ZF=0 and SF=OF) and jump instruction act according to their state. Share.
The CMP instruction compares two operands. It is generally used in conditional execution. This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the destination or source operands.
The jl instruction is a conditional jump that follows a test. It performs a signed comparison jump after a cmp if the destination operand is less than the source operand.
Home » Instructions » JB. The JB instruction branches to the address specified in the second operand if the value of the bit specified in the first operand is 1. The bit that is tested is not modified. No flags are affected by this instruction. See Also: JNB.
When you do a cmp a,b
, the flags are set as if you had calculated a - 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):
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. First the ones where signed-ness is not appropriate:
+--------+------------------------------+-------------+--------------------+ |Instr | Description | signed-ness | Flags | +--------+------------------------------+-------------+--------------------+ | 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/ | Jump if equal | | ZF = 1 | | JZ | Jump if zero | | | +--------+------------------------------+-------------+--------------------+ | JNE/ | Jump if not equal | | ZF = 0 | | JNZ | Jump if not zero | | | +--------+------------------------------+-------------+--------------------+ | JP/ | Jump if parity | | PF = 1 | | JPE | Jump if parity even | | | +--------+------------------------------+-------------+--------------------+ | JNP/ | Jump if no parity | | PF = 0 | | JPO | Jump if parity odd | | | +--------+------------------------------+-------------+--------------------+ | JCXZ/ | Jump if CX is zero | | CX = 0 | | JECXZ | Jump if ECX is zero | | ECX = 0 | +--------+------------------------------+-------------+--------------------+
Then the unsigned ones:
+--------+------------------------------+-------------+--------------------+ |Instr | Description | signed-ness | Flags | +--------+------------------------------+-------------+--------------------+ | JB/ | Jump if below | unsigned | CF = 1 | | JNAE/ | Jump if not above or equal | | | | JC | Jump if carry | | | +--------+------------------------------+-------------+--------------------+ | JNB/ | Jump if not below | unsigned | CF = 0 | | JAE/ | Jump if above or equal | | | | JNC | Jump if not carry | | | +--------+------------------------------+-------------+--------------------+ | JBE/ | Jump if below or equal | unsigned | CF = 1 or ZF = 1 | | JNA | Jump if not above | | | +--------+------------------------------+-------------+--------------------+ | JA/ | Jump if above | unsigned | CF = 0 and ZF = 0 | | JNBE | Jump if not below or equal | | | +--------+------------------------------+-------------+--------------------+
And, finally, the signed ones:
+--------+------------------------------+-------------+--------------------+ |Instr | Description | signed-ness | Flags | +--------+------------------------------+-------------+--------------------+ | JL/ | Jump if less | signed | SF <> OF | | JNGE | Jump if not greater or equal | | | +--------+------------------------------+-------------+--------------------+ | JGE/ | Jump if greater or equal | signed | SF = OF | | JNL | Jump if not less | | | +--------+------------------------------+-------------+--------------------+ | JLE/ | Jump if less or equal | signed | ZF = 1 or SF <> OF | | JNG | Jump if not greater | | | +--------+------------------------------+-------------+--------------------+ | JG/ | Jump if greater | signed | ZF = 0 and SF = OF | | JNLE | Jump if not less or equal | | | +--------+------------------------------+-------------+--------------------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With