ALT by Bilal Ahmed Shaik - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

img76.png

 

  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:

 

img77.png 

  INC destination

 

  The operand destination could be an 8-bit, 16-bit or 32-bit operand.

 

img78.png

  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:

img79.png

  DEC Destination

  The DEC instruction has the following syntax:

 

 

  The operand destination could be an 8-bit, 16-bit or 32-bit operand.

 

img80.png

img81.png

 

  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:

 

img82.png 

  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.

 

 img83.png

img84.pngimg85.png

img86.png

  When the above code is compiled and executed, it produces fol owing result:

img87.png

 

  The program with hardcoded variables:

img88.png

  When the above code is compiled and executed, it produces fol owing result:

img89.png 

  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:

 

img90.png 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:

 

img91.png 

  EXAMPLE:

img92.png 

  EXAMPLE:

 

  The following example multiplies 3 with 2, and displays the result:

 

img93.png

  When the above code is compiled and executed, it produces fol owing result:

 

img94.png 

  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:

 

img95.png 

  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:

 

img96.png

img97.png

img98.png 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.

 

img99.pngimg100.png

When the above code is compiled and executed, it produces the following result

img101.png