组装 - CMP 后的 JG/JNLE/JL/JNGE [英] Assembly - JG/JNLE/JL/JNGE after CMP
问题描述
我不明白 CMP 之后的 JG/JNLE/JL/JNGE
指令.
I don't understand the JG/JNLE/JL/JNGE
instructions, which come after CMP.
例如,如果我有:
CMP al,dl
jg label1
当 al=101;dl =200
.
我们问jg
有什么问题?是否在 al>dl
上?还是 al-dl>0
?
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
我不明白我们比较什么,以及我们问jg
"什么.
I don't understand what we compare, and on what we ask the "jg
".
换句话说,我不明白我们什么时候会跳转到 label1,什么时候不会.
In other words, I don't understand when we would jump to label1, and when we wouldn't.
推荐答案
当你做一个 cmp a,b
时,标志被设置,就像你计算了 a - b
代码>.
When you do a cmp a,b
, the flags are set as if you had calculated a - b
.
然后 jmp
类型的指令检查这些标志以查看是否应该进行跳转.
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
当且仅当 al
大于 dl
时才会跳转到 label1
.
would jump to label1
if and only if al
was greater than dl
.
你最好把它想成 al >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)
使用 jg
时需要小心,因为它假定您的值已签名.因此,如果将字节 101(二进制补码中的 101)与 200(二进制补码中的 -56)进行比较,实际上前者会更大.如果这不是您想要的,您应该使用等效的无符号比较.
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 |
+--------+------------------------------+-------------+--------------------+
然后是未签名的:
+--------+------------------------------+-------------+--------------------+
|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 | | |
+--------+------------------------------+-------------+--------------------+
最后,签名的:
+--------+------------------------------+-------------+--------------------+
|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 | | |
+--------+------------------------------+-------------+--------------------+
这篇关于组装 - CMP 后的 JG/JNLE/JL/JNGE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!