You are on page 1of 20

Arithmetic and Logical

Instructions
Arithmetic and Logical instructions
• All the instructions performing arithmetic ,
logical, increment, decrement, compare and
ASCII instructions belong to this category.
• Most common arithmetic instructions are:
ADD DIV
SUB IDIV
MUL INC
IMUL DEC
ADD and ADC
• The add instruction adds the contents of the
source operand to the destination operand.
• ADD computes dest :=dest+source
• ADC computes dest :=dest+source+C where C
represents the value to carry whenthe carry
flag is set.
• Therefore, if the carry flag is clear before
execution, ADC behaves exactly like the ADD
instruction.
• Both instructions affect the flags identically.
ADD and ADC
• Syntax : ADD dest, src
• Syntax: ADC dest, src
FLAGS
• ZF := 1 if result = 0
• SF := 1 if result is negative
• CF := 1 if unsigned arithmetic overflows
• OF := 1 if result overflowed signed capacity
INC
• The INC instruction is very important because
adding one to a register is a very common
operation.
• Incrementing loop control variables or indices into
an array is a very common operation, perfect for
the INC instruction.
• INC does not affect the carry flag allowing
incrementing array indices without affecting
arithmetic computation.
• Example INC ax
SUB,SBB and DEC
• The SUB (subtract), SBB(subtract with borrow), DEC
(decrement).
• SUB and SBB syntax is similar to ADD and ADC syntax.
sub reg, reg
sub reg, mem
sub mem, reg
sub reg, immediate data
sub mem, immediate data
• Semantic: SUB dest := dest - src.
SBB dest := dest - src - C.
DEC dest-1
• Syntax: SUB\SBB dest,src
DEC dest
FLAGS
• ZF := 1 if result = 0
• SF := 1 if result is negative
• CF := 1 if unsigned arithmetic overflows
• OF := 1 if signed overflow/underflow occurs.
Examples
• Write an assembly to add 10 and 6 then
subtract 5.
Mov ax,10
Mov bx,6
ADD ax,bx
Mov bx,5
SUB ax,bx
MUL\IMUL
There are two forms of the multiply instruction:
• MUL - an unsigned multiplication
• IMUL - a signed multiplication
• Unlike addition and subtraction, you need separate
instructions for these two operations.
• The multiply instructions to use a single
operand.
• Syntax MUL reg/mem/immediate
MUL/IMUL
These instructions multiplies unsigned byte or
word by the content of accumulator register
(AL/AH/AX/EAX/RAX).

MULTPLICAND PRODUCT
AL AX
AX DX:AX
EAX EDX:EAX
MUL
• Note that when multiplying two n-bit values,
the result may require as many as 2*n bits.
• Therefore, if the operand is an eight bit
quantity, the result will require sixteen bits.
• Likewise, a 16 bit operand produces a 32 bit
result and a 32 bit operand requires 64 bits for
the result.
Example
To multiply 8 by 7 in assembly
Org 0x0100
mov al,8
mov bl,7
mul bl
int 0x21

The result is 38 in hexadecimal in AX register.


Example 2
Org 0x0100
mov ax,2000h
mov bx,100h
mul bx
int 0x21
The result is 00200000h in DX:AX registers and
CF=1
IMUL
• IMUL (signed integer multiply ) multiplies an 8
or 16 or 32-bit signed operand by either AL, AX,
or EAX.
• Preserves the sign of the product by sign
extending it into the upper half of the
destination register.
• The source can be a general purpose register,
memory operand, index register or base
register, but it cannot be an immediate data.
Example
Org 0x0100
mov al,48
mov bl,4
mul bl
int 0x21

AX = 00C0
DIV
• The DIV (unsigned divide) instruction performs
8-bit,16-bit, and 32-bit division on unsigned
integers.
• A single operand is supplied by(register or
memory operand), which is assumed to be the
divisor.
Operands
DIVIDENT DIVISOR QUOTIENT REMINDER
AX R/M8 AL AH
DX:AX R/M16 AX DX
EDX:EAX R/M32 EAX EDX
Example
Org 0x0100
mov dx,0 ; clear dividend
mov ax,9 ; dividend
mov cx,2; divisor
div cx ;
int 0x21

;Result: AX = 4, DX = 1
DIV Example
Org 0x0100
mov al,36
mov ax,9
div al
int 0x21
IDIV
• This instruction performs same operation as the
DIV instruction but with signed operands.
• The results are stored similarly as in case of DIV
instruction.
• In both cases of word and double word divisions
the results will also be signed numbers.
• Syntax
IDIV divisor

You might also like