Arithmetic Instructions
The INC Instruction
The INC instruction is used for incrementing an operand by one. It works on a single operand that can be either in a register or in memory.
SYNTAX:
INC destination
The operand destination could be an 8-bit, 16-bit or 32-bit operand.
The DEC Instruction
The DEC instruction is used for decrementing an operand by one. It works on a single operand that can be either in a register or in memory.
SYNTAX:
DEC Destination
The DEC instruction has the following syntax:
The operand destination could be an 8-bit, 16-bit or 32-bit operand.
The ADD and SUB Instructions
The ADD and SUB instructions are used for performing simple addition/subtraction of binary data in byte, word and doubleword size, i.e., for adding or s ubtracting 8-bit, 16-bit or 32-bit operands respectively.
SYNTAX:
The ADD and SUB instructions have the following syntax:
The ADD/SUB instruction can take place between:
Register to register
Memory to register
Register to memory
Register to constant data
Memory to constant data
However, like other instructions, memory-to-memory operations are not possible using ADD/SUB instructions. An ADD or SUB operation sets or clears the overflow and carry flags.
EXAMPLE:
The following example asks two digits from the user, stores the digits in the EAX and EBX register respectively,
adds the values, stores the result in a memory location ' res' and finally displays the result.
When the above code is compiled and executed, it produces fol owing result:
The program with hardcoded variables:
When the above code is compiled and executed, it produces fol owing result:
The MUL/IMUL Instruction
There are two instructions for multiplying binary data. The MUL (Multiply) instruction handles unsigned data and the IMUL (Integer Multiply) handles signed data. Both instructions affect the Carry and Overflow flag.
SYNTAX:
The syntax for the MUL/IMUL instructions is as follows:
Multiplicand in both cases will be in an accumulator, depending upon the size of the multiplicand and the multiplier and the generated product is also stored in two registers depending upon the size of the operands. Following section explains MULL instructions with three different cases:
EXAMPLE:
EXAMPLE:
The following example multiplies 3 with 2, and displays the result:
When the above code is compiled and executed, it produces fol owing result:
The DIV/IDIV Instructions
The division operation generates two elements - a quotient and a remainder. In case of multiplication, overflow does not occur because double-length registers are used to keep the product. However, in case of division, overflow may occur. The processor generates an interrupt if overflow occurs.
The DIV (Di vide) instruction is used or unsigned data and the IDIV (Integer Divide) is used for signed data.
SYNTAX:
The format for the DIV/IDIV instruction:
The dividend is in an accumulator. Both the instructions can work with 8-bit, 16-bit or 32-bit operands. The operation affects al six status flags. Following section explains three cases of division with different operand size:
The following example divides 8 with 2. The divide nd 8 is stored in the 16 bit AX register and thedivisor 2 is stored in the 8 bit BL register.
When the above code is compiled and executed, it produces the following result