NEG replaces a byte or word operand with its two's complement, computing 0 − operand. As signed numbers this flips the sign: 5 becomes −5 (FBh) and −5 becomes 5. A memory operand needs a size when it has none: NEG WORD PTR [BX].
The result is the same as inverting every bit and adding 1, that is NOT followed by INC, but NEG does it in one step and sets the flags.
Flags
The flags are those of the subtraction 0 − operand. SF, ZF and PF describe the result. CF = 1 for any non-zero operand and CF = 0 only when the operand was 0 (zero is the one value that needs no borrow). OF = 1 when the operand is the most negative value, 80h or 8000h: −128 has no positive byte counterpart, so NEG leaves it unchanged and reports overflow.
Printing a signed number
To print a value that may be negative, test its sign, print a minus and negate it, then print it as an unsigned number. The example prints −4.
MOV AL, 3SUB AL, 7 ; AL = FCh = -4, SF = 1JNS POS1 ; not negative: print as isNEG AL ; AL = 4MOV BL, ALMOV DL, '-'MOV AH, 02hINT 21hMOV AL, BLPOS1:ADD AL, '0' ; digit -> characterMOV DL, ALMOV AH, 02h ; DOS: print DLINT 21h
Other uses
NEG turns a subtraction around: SUB AX, BX followed by NEG AX gives BX − AX. To negate a 32-bit value in DX:AX, negate both halves and fix the high word with the borrow from the low one:
NEG DXNEG AX ; CF = 1 unless AX was 0SBB DX, 0 ; DX:AX = -(DX:AX)