Arithmetic Instructions
Overview
• inc and dec
• Add
• Sub
• mul and imul
• div and idiv
INC and DEC instruction
• The INC and DEC instructions add 1 to or subtract 1 from a single
operand, respectively.
• Syntax:
– INC destination
– DEC destination
• Destination may be an 8 or 16 bit register or memory operand.
• INC and DEC are faster than the ADD and SUB instructions, so they
should be used where practical
• All status flags are affected except the Carry Flag.
INC and DEC instruction
• Example:
– inc al ; increment an 8-bit register
– dec bx ; decrement of a 16-bit
register
– inc membyte ; increment memory
operand
ADD instruction
• The ADD instructions adds an 8-bit or 16-bit source operand to a
destination operand of the same size.
• Syntax:
– ADD destination, source
• Source is unchanged by the operation
• The sizes of the operands must match, & only one memory operand
may be used.
• A segment register may not be the destination.
• All status flags are affected.
ADD instruction
• Examples:
– Add al, 1 ; add immediate value to 8-bit register.
– Add cl, al ; add 8 bit register to register.
– Add bx, 1000h ; add immediate value
– Add var1, ax ; add 16-bit register to memory
– Add var1, 10 ; add immediate value to memory
SUB instruction
• The SUB instructions subtracts a source operand from a destination
operand.
• Syntax:
– SUB destination, source
• The sizes of the two operands must match, & only one may be a
memory operand.
• A segment register may not be the destination operand.
• All status flags are affected.
SUB instruction
• Examples:
– Sub al, 1 ; subtract immediate value from 8-bit
register
– Sub cl, al ; subtract 8-bit register from register
– Sub bx, 1000h ; subtract immediate value from 16-bit register
memory
– Sub var1, 10 ; subtract immediate value from memory
Flags Affected by ADD and SUB
• If the result of an addition operation is too large for the destination
operand, the Carry Flag is set:
– Mov al, FA
Add al, 32 ; Carry Flag set
(the result is 300 (12C), too large to fit into AL)
• If the source is larger than the destination, a subtraction operation
requires a borrow, the Carry Flag is set:
– Mov al, 5
sub al, 10 ; Carry Flag set
Flags Affected by ADD and SUB
• If the result of an arithmetic operation is zero, the Zero flag is set:
– Mov bl, 0
mov al, 0
add al, bl ; Zero Flag is set
mov al, 10
sub al, 10 ; Zero Flag is set
• When an addition operation generates a result exactly 1 too large for the
destination, it sets the Zero Flag.
– Mov bl, FF
add bl, 1 ; Zero Flag set, & BL= 0h.
Flags Affected by ADD and SUB
• The Overflow Flag is set when an addition operation generates a signed
number that is out of range.
• Range of a signed 8-bit value is -128 to +127 & a singed 16-bit value is
limited to -32768 to +32767 because of the highest bit is reserved for
the sign
– Mov cl, 7F
add cl, 2 ; Overflow flag set & CL = 80
MUL and IMUL instructions
• The MUL and IMUL instructions multiply an 8-bit or 16-bit operand by AL
or AX.
• If an 8-bit source operand is supplied, it is automatically multiplied by
AL, and the result is stored in AX
• If 16-bit source operand is supplied, it is multiplied by AX, & the result is
stored in DX & AX (the high 16 bits are in DX).
• Syntax:
– MUL source
– IMUL source
MUL and IMUL instructions
• The source operand may be either a register or a memory operand,
but it may not be immediate data.
• The destination (either AX or DX:AX) is implied & may not be
changed.
• Example:
Multiply AL by 10h
mov al, 6h
mov bl, 10h
mul bl ; AX= 0060h
MUL and IMUL instructions
• Example:
Multiply AX by 5003
mov AX,1111
mov CX, 5003
mul CX ; DX=0555h, AX=8333h
IMUL instructions
• The IMUL (Integer Multiply) instructions multiply signed binary values
• Its sign-extends the result through the highest bit of AX or DX:AX,
depending on the size of the source operand.
• An 8-bit operation sign extends AL into AH; a 16-bit operation sign
extends AX into DX.
• Example: if the result of IMUL is -10
– 8-bit operation AX=FFF6h
– 16-bit operation DX= FFFFh, AX=FFF6h
Flags affected by IMUL
• If the product fits completely within the lower half of the destination
(AL or AX), the Carry and Overflow flags are cleared.
• Example:
– Result of an 8-bit IMUL operation is -1, so AX = FFFFFFFFh. The
carry and overflow flags are clear:
• Mov al, 1
• Mov bl, -1
• Imul bl ; AX = FFFFFFFFh, CF=0, OF=0
DIV and IDIV Instructions
• The DIV instruction is for division of unsigned
integers.
• IDIV instructions is for division of signed 2’s
complement integers .
• A single operand is supplied (register or memory
operand), which is assumed to be the divisor.
• Syntax:
– DIV source
– IDIV source
DIV and IDIV instructions
• If the divisor is 8-bit long, AX is the
dividend, AL the quotient, and AH the
remainder.
• If the divisor is 16 bits, DX:AX is the
dividend, AX the quotient, and DX the
remainder.
DIV and IDIV instructions
• Example:
8 bit division (83h/2 =40h, remainder 3):
mov ax, 0083h ;dividend
mov bl, 2 ;divisor
div bl ; AL =41 AH=01
DIV and IDIV instructions
• Example:
16-bit division (8003h/100h=80h remainder 3).
mov ax, 8003h ; dividend low
mov cx, 100h ; divisor
div cx ; AX=0080h
DX=0003h
DIV and IDIV instructions
• The IDIV instruction performs a signed division
operation.
• Syntax:
– IDIV register
– IDIV memory
• For 8 bit operation, answers will be in AL (answer)
and AH (remainder).
• For 16 bit operation, answers will be in AX
(answer) and DX (remainder).
DIV and IDIV instructions
• Example:
-5/3
– MOV AL, -5 ------------- AL:FB
– MOV BL, 3
– IDIV BL ------------- AX: FEFF
Answer and remainder 2’s complement