Signed vs unsigned

Contents

Signed vs unsigned#

Arithmetic instructions are the same mnemonic (add, sub, mul / imul, div / idiv); the differentiator is whether the operator interprets the result as signed or unsigned and which flags / jumps the operator reads.

  • mul, div — unsigned.

  • imul, idiv — signed.

  • ja / jae / jb / jbe — unsigned compare (above / below).

  • jg / jge / jl / jle — signed compare (greater / less).

See Control flow for the flag / jump matrix in full.

Sign-extension moves a signed value to a wider register.

movsx   rax, dword [rsi]       ; sign-extend a 32-bit signed value

Zero-extension moves an unsigned value to a wider register.

movzx   rax, byte  [rsi]       ; zero-extend a byte

The 32-bit special form for sign-extending into a 64-bit register.

movsxd  rax, dword [rsi]

ARM64 has the same idea with separate load mnemonics.

ldrsb   x0, [x1]                // load signed byte, sign-extend to x0
ldrb    w0, [x1]                // load byte, zero-extend to w0 (then x0)

References#

  • Sizes for the widths these conversions cross.

  • Control flow for the conditional-jump matrix.