The INC (Increment) and DEC (Decrement) instructions are some of the most fundamental and frequently used operations in assembly language programming. These instructions are used to increase or decrease the value stored in a register or memory location by 1. They are very efficient because they are single-byte instructions in most assembly languages, making them quicker to execute compared to the ADD and SUB instructions when the increment or decrement is by 1.
The INC instruction increases the value of a register or memory location by 1. It is shorthand for adding 1 to the operand.
INC operand
The INC instruction automatically updates the Zero Flag (ZF) and the Sign Flag (SF) based on the result. It does not affect the carry flag (CF) directly. If the result of the increment is zero, the Zero Flag is set. If the result is negative (for signed numbers), the Sign Flag is set.
MOV AX, 5 ; AX = 5
INC AX ; AX = 6 (AX is incremented by 1)
In this example:
AX.INC AX instruction increases the value in AX by 1, so now AX holds the value 6.MOV AX, [num] ; Load the value from memory location 'num' into AX
INC [num] ; Increment the value at memory location 'num' by 1
In this case, the value stored at the memory address num is incremented by 1.
The DEC instruction decreases the value of a register or memory location by 1. It is shorthand for subtracting 1 from the operand.
DEC operand
The DEC instruction also updates the Zero Flag (ZF) and the Sign Flag (SF) based on the result. However, similar to INC, the carry flag (CF) is not affected by the DEC operation. If the result of the decrement is zero, the Zero Flag is set, and if the result is negative, the Sign Flag is set.
MOV AX, 10 ; AX = 10
DEC AX ; AX = 9 (AX is decremented by 1)
In this example:
AX.DEC AX instruction decreases the value in AX by 1, so now AX holds the value 9.MOV AX, [num] ; Load the value from memory location 'num' into AX
DEC [num] ; Decrement the value at memory location 'num' by 1
In this case, the value stored at the memory address num is decremented by 1.
Zero Flag (ZF):
Sign Flag (SF):
Overflow Flag (OF):
Carry Flag (CF):
The INC and DEC instructions are commonly used in loops where a counter is needed. For example, you can use DEC to count down to zero, or INC to count up.
Example of a simple countdown using DEC:
MOV CX, 10 ; Set counter to 10
loop_start:
DEC CX ; Decrease CX by 1
JNZ loop_start ; Jump if CX is not zero (CX != 0)
In this loop:
CX starts at 10.CX is decremented by 1.JNZ (Jump if Not Zero) instruction causes the loop to continue until CX becomes 0.INC and DEC are useful for adjusting pointers, especially when iterating through arrays or buffers.
For example, incrementing a pointer in an array:
MOV SI, 0 ; SI points to the first element of an array
INC SI ; Move to the next element in the array (assuming SI is an index register)
In this case, SI points to the first element of an array. After the INC SI instruction, SI will point to the next element.
In certain cases, you can use INC and DEC to perform simple arithmetic, especially in tight loops or for simple algorithms that only need to add or subtract 1. They are efficient because they use fewer machine cycles than the more general ADD or SUB instructions.
While ADD and SUB are more general-purpose instructions for adding or subtracting arbitrary values, INC and DEC are specifically optimized for increments and decrements by 1. They are usually more efficient in terms of code size and execution time in these cases.
ADD and SUB can handle a wide range of values, not just 1.
ADD AX, 5 ; AX = AX + 5
SUB AX, 2 ; AX = AX - 2
INC and DEC are faster and take fewer machine cycles when you only need to add or subtract 1:
INC AX ; AX = AX + 1
DEC AX ; AX = AX - 1
These simple operations form the building blocks of more complex algorithms and are particularly important in assembly and systems programming where performance and direct hardware control are crucial.
Open this section to load past papers