You are on page 1of 21

Microprocessor Course

LECTURE(04)

Arithmetic and Logic Instructions

1
Chapter Contents
 Arithmetic instructions: ADD, ADC, INC, SUB, SBB, DEC, MUL, and DIV

 Logic instructions: AND, OR, XOR, Test, Not, and NEG

 Shift and rotate instructions: SHL, SHR, SAL, SAR, ROL, ROR, RCL, and
RCR

2
Arithmetic Instructions - Add
 The ADD instruction is used for binary addition.

 The addition causes the flag bits to change.

 Addition can be 8, 16, and 32 bits.

 All of the addressing modes presented in Chapter 3 are used by


addition.

 EX:

ADD EAX, EBX ;EAX = EAX + EBX

3
Arithmetic Instructions - INC
 The INC instruction adds a one to a register or the contents of a
memory location.

 EX:

INC BX ; BX = BX + 1

INC BYTE PTR [EBX] ; INC a memory location

4
Arithmetic Instructions - ADC
 The ADC instruction adds the carry bit into the sum.

 Used for wide additions (wider than 32-bits).

 Ex.

ADC AX,DX ;AX = AX + DX + C

ADC ESI, [EBX] ; ESI=ESI+[EBX]+C

5
Arithmetic Instructions - Sub
 The SUB instruction performs binary subtraction

 Flags change to reflect condition of the result.

 As with other arithmetic and logic instructions, subtraction exists for 8,


16, and 32 bits data.

 Ex.

SUB AL, 3 ; AL = AL - 3

SUB ECX, ESI ; ECX=ECX-ESI

6
Arithmetic instructions - DEC
 The DEC instruction subtracts one from a register or the contents of a
memory location.

 Ex.

DEC EBX ; EBX = EBX - 1

DEC DWORD PTR [EAX] ; [EAX]=[EAX]-1

7
Arithmetic Instructions - SBB
 The SBB instruction subtracts with borrow (where the borrow is held in
the carry flag).

 Ex.

SBB EAX, EBX ;EAX = EAX – EBX – C

8
Arithmetic Instructions - CMP
 The CMP instruction is a special form of the SUB instruction. A compare
does not result in a difference that is saved

 Flag bits change to reflect the difference.

 Usually followed by a conditional jump

 Ex.

CMP AL,3 ; Execute AL–3 and update flags accordingly

9
Arithmetic Instructions – MUL, IMUL
 The MUL (unsigned) and IMUL (signed) instruction exist to perform 8, 16,
or 32-bit multiplication.

 The result is always a double wide result.

 The carry and overflow bits indicate conditions about the


multiplication.

 For 8-bit multiplication, AL should contain the multiplicand. Results will


be in AX

 Ex. MUL CL
 For 16-bit multiplication, AX should contain the multiplicand. Results
will be in DX-AX

10
Arithmetic Instructions – DIV, IDIV
 The DIV (unsigned) and IDIV (signed) instruction exist to perform
division on 8, 16, or 32-bit numbers.

 Division is always performed on a double wide dividend.

 The result is always in the form of an integer quotient and integer


remainder.

 For 8-bit division, AX should contain the dividend. Quotient will be in AL


and the remainder will be in AH.

 Ex. DIV CL

11
Logical Instructions - AND
 The AND instruction performs logical multiplication (the AND operation).

12
Logical Instructions - OR
 The OR instruction generates the logical sum (OR operation).

13
Logical Instructions - XOR
 The XOR instruction performs the Exclusive OR operation.

14
Logical Instructions - NEG and NOT
 The NEG (negate) instruction 2’s complements a number,

 The NOT instruction 1’s complements a number.

 Ex.

NOT EAX

NEG DWORD PTR [EBX]

15
Shift Instructions
 There are 4 shift instructions. Two are logical shifts and two are
arithmetic shifts.

 The logical shifts reposition the bits in a number. The arithmetic shifts
multiply or divide signed numbers by powers of two.

 SHR and SHL are logical, and SAR and SAL are arithmetic shifts.

 Ex.

SHL AL,3

SHL AL,CL

16
Shift Instructions - Rotates
 Rotates are shifts that re-circulate the bit that moves out of an end of
the register or memory location.

 Four rotates exist where two just rotate the target and two rotate the
target through the carry flag.

 Ex.

ROL AL,3 or RCL AL,3

18
TEST
 The TEST instruction is a special form of the AND instruction that
produces no result except for a change of the flags.

 This instruction is often used to test multiple bits of a number.

 Ex.

TEST AL,3 ; test the right two bits (if both are zero the result is zero)

20
21

You might also like