
The NEG (Negate) instruction is used in assembly language to change the sign of an operand. In other words, it negates (or inverts) the value of the...
The NEG (Negate) instruction is used in assembly language to change the sign of an operand. In other words, it negates (or inverts) the value of the operand — it changes a positive value to negative and a negative value to positive. This is equivalent to multiplying the operand by -1.
The NEG instruction operates on a single operand and negates its value.
NEG operand
-1).Internally, NEG works by performing a two's complement operation on the operand:
The NEG instruction affects the following flags in the processor's Flags Register:
0, negating it will still result in 0, setting the Zero Flag.0x80 (which is -128) would cause an overflow because 128 cannot be represented in 8 bits.MOV AX, 5 ; AX = 5
NEG AX ; AX = -5 (AX is negated)
In this example:
5 is loaded into the register AX.NEG AX instruction negates AX, changing its value to -5.MOV AX, -3 ; AX = -3
NEG AX ; AX = 3 (AX is negated)
Here, the value -3 is stored in AX, and after the NEG instruction, the value becomes +3.
MOV AX, [num] ; Load the value at memory location 'num' into AX
NEG [num] ; Negate the value at memory location 'num'
In this case:
num is negated, and the result is stored back at the same location.The NEG instruction essentially performs the two's complement of the operand. Here's how two's complement negation works:
For example, let's negate the value 5 (which in binary for a 16-bit register is 0000 0000 0000 0101):
0000 0000 0000 0101 → 1111 1111 1111 1010
1111 1111 1111 1010
This binary result (1111 1111 1111 1011) is -5 in two's complement, which is the correct negation of 5.
If we try to negate the smallest possible signed value for a given register size (e.g., -128 in an 8-bit system), we can experience overflow because the result cannot be represented in the same number of bits.
MOV AL, 0x80 ; AL = -128 (0x80 in two's complement)
NEG AL ; This will cause a signed overflow, as 128 cannot be represented in 8-bit signed integer
Here, 0x80 is the two's complement representation of -128 in an 8-bit signed register. When we negate it, the result would be +128, which is too large to fit in an 8-bit signed integer (which can only hold values from -128 to +127). The Overflow Flag (OF) will be set to indicate the overflow.
-1) the value of a register or memory location.-1.The NEG instruction is a simple but powerful tool for low-level manipulation of numbers, and it's widely used in both system programming and performance-critical applications.
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.