Chapter 7
Arithmetic and Logical
Instructions
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.
• The INC instruction has the following syntax −
INC destination
• The operand destination could be an 8-bit, 16-bit or
32-bit operand.
• Example:
INC EBX ; Increments 32-bit register
INC DL ; Increments 8-bit register
INC [count] ; Increments the count variable 3
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.
• The DEC instruction has the following syntax −
DEC destination
• The operand destination could be an 8-bit, 16-
bit or 32-bit operand.
4
Example
5
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 subtracting 8-bit, 16-bit
or 32-bit operands respectively.
• The ADD and SUB instructions have the following syntax:
ADD/SUB destination, source
• 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
6
flags.
Program Example
The program:
• requests for two
digits from the user,
• store the digits in the
EAX and EBX register,
respectively,
• adds them,
• store the result in a
memory location 'res'
and
• finally display the
result.
7
• When this code is
compiled and
executed, it
produces the
following result −
Enter a digit:
3
Please enter a
second digit:
4
The sum is:
7
8
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.
• The syntax for the MUL/IMUL instructions is as follows
−
MUL/IMUL multiplier
• 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. 9
Different Cases of MUL instructions
• Multiplying Two Bytes
– The multiplicand is in the AL register, and the
multiplier is a byte in the memory or in another
register.
– The product is in AX.
– High-order 8 bits of the product is stored in AH
and the low-order 8 bits are stored in AL.
10
Different Cases of MUL instructions
• Multiplying Two One-Word Values
– The multiplicand should be in the AX register, and the
multiplier is a word in memory or another register.
– For example, for an instruction like MUL DX, you must
store the multiplier in DX and the multiplicand in AX.
– The resultant product is a doubleword, which will need
two registers.
– The high-order (leftmost) portion gets stored in DX and
the lower-order (rightmost) portion gets stored in AX.
11
Different Cases of MUL instructions
• Multiplying Two Double-Word Values
– When two doubleword values are multiplied, the
multiplicand should be in EAX and the multiplier is a
doubleword value stored in memory or in another
register.
– The product generated is stored in the EDX:EAX
registers, i.e., the high order 32 bits gets stored in the
EDX register and the low order 32 bits are stored in the
EAX register.
12
Example
MOV AL, 10
MOV DL, 25
MUL DL
...
MOV DL, 0FFH ; DL= -1
MOV AL, 0BEH ; AL = -66
IMUL DL
13
Program Example
• This program multiplies 3 by 2 to get 6 as the product
14
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 (Divide) instruction is used for unsigned data and the
IDIV (Integer Divide) is used for signed data.
• The format for the DIV/IDIV instruction −
DIV/IDIV divisor
• The dividend is in an accumulator.
• Both the instructions can work with 8-bit, 16-bit or 32-bit
operands. The operation affects all six status flags. 15
Different Cases of DIV instructions
• When the Divisor is 1 Byte
– The dividend is assumed to be in the AX register
(16 bits).
– After division, the quotient goes to the AL register
and the remainder goes to the AH register.
16
Different Cases of DIV instructions
• When the Divisor is 1 Word
– The dividend is assumed to be 32 bits long and in
the DX:AX registers.
– The high-order 16 bits are in DX and the low-
order 16 bits are in AX.
– After division, the 16-bit quotient goes to the AX
register and the 16-bit remainder goes to the DX
register.
17
Different Cases of DIV instructions
• When the Divisor is DoubleWord
– The dividend is assumed to be 64 bits long and in
the EDX:EAX registers.
– The high-order 32 bits are in EDX and the low-
order 32 bits are in EAX.
– After division, the 32-bit quotient goes to the EAX
register and the 32-bit remainder goes to the EDX
register.
18
Program Example
• This program
divides 8 with 2.
• The dividend
8 is stored in
the 16-bit AX
register
• The divisor 2 is
stored in the 8-
bit BL register.
• The result of the
program is 4
19
Logical Instructions
Introduction
• The processor instruction set provides the instructions AND,
OR, XOR, TEST, and NOT Boolean logic, which tests, sets, and
clears the bits according to the need of the program.
• The first operand in all the cases could be either in register or
in memory.
• The second operand could be either in register/memory or
an immediate (constant) value.
• However, memory-to-memory operations are not possible.
• These instructions compare or match bits of the operands
and set the CF, OF, PF, SF and ZF flags. 21
The AND Instruction
• The AND instruction is used for supporting logical
expressions by performing bitwise AND
operation.
• The bitwise AND operation returns 1, if the
matching bits from both the operands are 1,
otherwise it returns 0.
• For example −
Operand1: 0101
Operand2: 0011
-----------------------------------
After AND -> Operand1: 0001 22
The AND Instruction
• Example 1
– The AND operation can be used for clearing one or
more bits.
– For example, say the BL register contains 0011 1010.
If you need to clear the high-order bits to zero, you
AND it with 0FH.
AND BL, 0FH ; This sets BL to 0000 1010
• Example 2
– If you want to check whether a given number is odd
or even, a simple test would be to check the least
significant bit of the number.
– If this is 1, the number is odd, else the number is
23
even.
Program Example
• This program
checks if a given
number is odd or
even
• The results will be
Even Number!
• When the value in
ax register is
substituted with
an odd digit, the
result is Odd
Number!
• For Example:
mov ax, 9h
• To clear the entire
register you can
AND it with 00H. 24
The OR Instruction
• The OR instruction is used for supporting logical
expression by performing bitwise OR operation.
• The bitwise OR operator returns 1, if the matching
bits from either or both operands are one.
• It returns 0, if both the bits are zero.
• For example,
Operand1: 0101
Operand2: 0011
-------------------------------
After OR -> Operand1: 0111
25
The OR Instruction
• The OR operation can be used for setting one
or more bits.
• For example, let us assume the AL register
contains 0011 1010, you need to set the four
low-order bits, you can OR it with a value
0000 1111, i.e., FH.
OR BL, 0FH ; This sets BL to 0011 1111
26
Program Example
• This program demonstrates the OR instruction.
• Let us store the value 5 and 3 in the AL and BL registers, respectively, then the instruction,
– OR AL, BL
• should store 7 in the AL register
27
• When the program is executed, it produces the result 7.
The XOR Instruction
• The XOR instruction implements the bitwise XOR operation.
• The XOR operation sets the resultant bit to 1, if and only if
the bits from the operands are different.
• If the bits from the operands are same (both 0 or both 1), the
resultant bit is cleared to 0.
• For example,
Operand1: 0101
Operand2: 0011
------------------------------
After XOR -> Operand1: 0110
• XORing an operand with itself changes the operand to 0.
– This is used to clear a register.
XOR EAX, EAX 28
The TEST Instruction
• The TEST instruction works same as the AND
operation, but unlike AND instruction, it does
not change the first operand.
• So, if we need to check whether a number in
a register is even or odd, we can also do this
using the TEST instruction without changing
the original number.
TEST AL, 01H
JZ EVEN_NUMBER
29
The NOT Instruction
• The NOT instruction implements the bitwise
NOT operation.
• NOT operation reverses the bits in an
operand.
• The operand could be either in a register or in
the memory.
• For example,
Operand1: 0101 0011
After NOT -> Operand1: 1010 1100
30