我不理解CMP之后的JG/JNLE/JL/JNGE
指令。
例如,如果我有:
CMP al,dl
jg label1
当
al=101; dl =200
时。关于我们问
jg
?是否在al>dl
上?还是al-dl>0
?下一个代码相同的问题:
test al,dl
jg label1
我不明白我们比较什么,以及我们问“
jg
”。换句话说,我不知道什么时候跳转到label1,什么时候不跳转。
最佳答案
当您执行cmp a,b
时,将设置标志,就好像您已经计算出a - b
一样。
然后,jmp
-type指令检查这些标志,以查看是否应进行跳转。
换句话说,您拥有的第一段代码(添加了我的注释):
cmp al,dl ; set flags based on the comparison
jg label1 ; then jump based on the flags
当且仅当
label1
大于al
时,才会跳到dl
。您可能最好将其视为
al > dl
,但在数学上等效的两种选择是: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)进行比较,则前者实际上会更大。如果这不是所需的,则应使用等效的无符号比较。有关跳转选择的更多详细信息,请参见here,为完整起见,下面对其进行了复制。首先是那些不适合签名的人:
+--------+------------------------------+-------------+--------------------+
|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 | | |
+--------+------------------------------+-------------+--------------------+
关于assembly - 组装-CMP后的JG/JNLE/JL/JNGE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9617877/