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

前端 未结 2 1459
-上瘾入骨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:38

    You can't directly compare two numbers that have different signs. Actually most software languages have that flow. C and C++ specifically mention that in their documentation and in most cases will generate a warning when you use a signed and an unsigned integer in the same expression which then can result in an unknown sign.

    The only way is to first check whether the signed number is negative, if so then you know it is smaller. Then you can compare the two numbers as unsigned integers.

    ; is eax < ebx (eax signed, ebx unsigned)
    
    cmp eax, $0
    jl less
    
    cmp eax, ebx
    jc less
    

    Side note: it is obviously possible to compare two numbers signed if their size is less than the maximum size supported by the processor. In that case you extend the bits of the signed and unsigned appropriately, then you can compare as if both values were signed.

    Assuming you wanted to compare two bytes al and bl, then you could have something like this:

    movsx ax, al
    xor bh, bh    ; or movzx bx, bl
    cmp ax, bx
    jl less
    

    (Note, I do not guarantee that jl is correct, it may be jle or jnl...)

提交回复
热议问题