Search code examples
assemblymacrosx86masmmasm32

MASM: .IF with signed numbers comparison


I have:

    mov ecx, r
    .if ecx < 0
        mov cl, 0
    .elseif ecx > 255
        mov cl, 255
    .endif
    mov [eax + 2], cl

r is signed integer. I want it to cap it within byte limit. But problem is when "r" is negative. It is treated as if it is unsigned.

Input -> Expected output
r = 300 -> 255
r = 12 -> 12
r = -134 -> 0

What actually happenes:
r = 300 -> 255
r = 12 -> 12
r = -134 -> 255 <--------- Here it gets treated as if -134 is bigger than 255

How to fix it?


Solution

  • Shortest solution:

    .if SDWORD PTR ecx < 0