Professional Documents
Culture Documents
MP - Lec 04 - Arithmetic & Logic Instructions
MP - Lec 04 - Arithmetic & Logic Instructions
LECTURE(04)
1
Chapter Contents
Arithmetic instructions: ADD, ADC, INC, SUB, SBB, DEC, MUL, and DIV
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.
EX:
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
4
Arithmetic Instructions - ADC
The ADC instruction adds the carry bit into the sum.
Ex.
5
Arithmetic Instructions - Sub
The SUB instruction performs binary subtraction
Ex.
SUB AL, 3 ; AL = AL - 3
6
Arithmetic instructions - DEC
The DEC instruction subtracts one from a register or the contents of a
memory location.
Ex.
7
Arithmetic Instructions - SBB
The SBB instruction subtracts with borrow (where the borrow is held in
the carry flag).
Ex.
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
Ex.
9
Arithmetic Instructions – MUL, IMUL
The MUL (unsigned) and IMUL (signed) instruction exist to perform 8, 16,
or 32-bit multiplication.
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.
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,
Ex.
NOT EAX
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.
18
TEST
The TEST instruction is a special form of the AND instruction that
produces no result except for a change of the flags.
Ex.
TEST AL,3 ; test the right two bits (if both are zero the result is zero)
20
21