Instructions
x86-64NEG
Two's-complement negation — computes 0 minus the operand. Useful for unary minus, absolute value, and as a quick 'is it zero?' carry test.
NEG dst replaces the operand with its two's-complement
negation — exactly 0 - dst. It works on a register or memory operand.
mov eax, 5
neg eax ; eax = -5 (0xFFFFFFFB)
neg eax ; eax = 5 againFlags
NEG sets the flags as if it had computed 0 - dst:
CF= 0 only when the operand was 0, and 1 otherwise. (Because0 - 0produces no borrow, but0 - anythingdoes.) This makesnega one-byte "is this value non-zero?" test that drops the answer intoCF.ZF,SF,OF,AF,PFare set from the result in the usual way.negofINT_MIN(0x80000000) yields itself and setsOF— the only value that has no positive counterpart.
Reverse-engineering notes
- The obvious source mapping is unary minus:
-x. - Absolute value is often compiled branchlessly as
mov tmp, x; sar tmp, 31; xor x, tmp; sub x, tmprather than withneg— butnegdoes appear in the simpler patterntest/js … neg. Recognise both. negthensbb reg, reg(oradc) is an idiom that broadcasts "was it non-zero?" across a whole register (0 → 0, non-zero → all-ones), used to build branchless masks.- Don't confuse
NEG(arithmetic,0 - x) withNOT(bitwise complement,~x). They differ by one:-x == ~x + 1.
See also: Two's complement · AND / OR / NOT · ADC / SBB.
Try it — Virtual CPU
open full playground →- 1 mov eax, 5
- 2 neg eax
- 3 neg eax
- 4
step 0
▸ Loading emulator…