如何在x86程序集中比较带符号的值和无符号的值 [英] How to compare a signed value and an unsigned value in x86 assembly

查看:102
本文介绍了如何在x86程序集中比较带符号的值和无符号的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到一种在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆