如何在 x86 程序集中比较有符号值和无符号值 [英] How to compare a signed value and an unsigned value in x86 assembly
问题描述
我无法找到一种方法来比较 x86 汇编代码中的正数和负数.
I am having trouble finding a way to compare a positive number and a negative number in x86 assembly code.
例如:当我比较 -1 和 1 时,我总是得到 -1 更大.我知道这是因为 2 的补码格式使基础二进制中的 -1 大于 1.
For example: when I compare -1 and 1 I always get -1 as greater. I know that it's because 2's complement format makes the -1 bigger than 1 in underlying binary.
但是任何人都可以提供 x86 程序集的片段来比较正数和负数并使其在数学上正确吗?(例如 1 > -1)
But can anyone provide a snippet of x86 assembly to compare positive number with a negative one and get it mathematically correct? (e.g 1 > -1)
谢谢!
推荐答案
您可能使用了以下无符号变体之一:
You're probably using one of the unsigned variants like:
cmp eax, ebx
jb lesser
有等价物可以相互检查带符号的数字,例如:
There are equivalents for checking signed numbers against each other, such as:
cmp eax, ebx
jl lesser
此链接很好地介绍了跳跃变化,包括它们的签名-ness 和他们检查的标志,部分复制在这里以进行自我控制:
This link gives a good run down on the jump variations, including their signed-ness and the flags they check, partially copied here for self-containment:
Instruction Jump if ... Signed? Flags
----------- ----------- -------- -----
JO overflow OF=1
JNO not overflow OF=0
JS sign SF=1
JNS not sign SF=0
JE/JZ equal
zero ZF=1
JNE/JNZ not-equal
not-zero ZF=0
JB/JNAE/JC below
not-above-or-equal
carry unsigned CF=1
JNB/JAE/JNC not-below
above-or-equal
no-carry unsigned CF=0
JBE/JNA below-or-equal
not-above unsigned CF=1 or ZF=1
JA/JNBE above
not-below-or-equal unsigned CF=0 and ZF=0
JL/JNGE less
not-greater-or-equal signed SF<>OF
JGE/JNL greater-or-equal
not-less signed SF=OF
JLE/JNG less-or-equal
not-greater signed ZF=1 or SF<>OF
JG/JNLE greater
not-less-or-equal signed ZF=0 and SF=OF
JP/JPE parity
parity-even PF=1
JNP/JPO not-parity
parity-odd PF=0
JCXZ/JECXZ CX register is zero
ECX register is zero
这篇关于如何在 x86 程序集中比较有符号值和无符号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!