How to compare a signed value and an unsigned value in x86 assembly

前端 未结 2 1465
-上瘾入骨i
-上瘾入骨i 2020-12-15 12:59

I am having trouble finding a way to compare a positive number and a negative number in x86 assembly code.

For example: when I compare -1 and 1 I always get -1 as g

2条回答
  •  借酒劲吻你
    2020-12-15 13:21

    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
    

    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
    

提交回复
热议问题