Post on 18-Jan-2018
description
Introduction Arithmetic instructions are used to perform arithmetic operation such as
Addition
Subtraction
Multiplication
Division
These operations can be performed on numbers expressed in a variety of formats
such as unsigned binary, signed binary, packed or unpacked decimals and signed
packed decimal numbers. The flags that are affected by the arithmetic instructions
are carry flag, auxiliary carry flag, sign flag, zero flag, parity flag and overflow flag.
ADD instructionsThis instruction adds the content of the source operand with the
destination operand. The result is stored in the destination operand. All flags are affected.
ADD
ADD operand1, operand 2
operand1 =operand 1 + operand 2
Operand 1: register/memory
Operand 2: register/memory/immediate
ADC Instruction
This instruction stands for add with carry. This instruction
adds the content of source, destination and the carry flag.
The result is stored in the destination operand.
Example:
ADC AX, [BX]
INC instructionThis instruction increments the value of register or the data in the
memory location by 1. the result is stored in the operand itself. In this carry flag (CF) is not affected. Also in many cases the used of INC instruction generated less machine code, and resulting in faster execution.
ExamplesINC SP
This instruction increments the value of SP register by 1. the result is stored in the SP register itself.
SUB InstructionThis instruction is used to subtract the value of source operand
from the destination operand. The result is stored in the
destination operand. If the source operand is larger than the
destination operand the resulting borrow is indicated by setting
the carry flag.
General format
SUB destination, source
SUB instructions
SUB
SUB operand1, operand 2
operand1 =operand 1 - operand 2
operand 1: register/memory
operand 2: register/memory/immediate
Motaz K. Saad, Dept. of CS 7
Example of the ADD and SUB instructions:
BYTE1 DB 24H ;Data elements WORD1 DW 4000H
. . .MOV CL , BYTE1 ; byte processing MOV DL , 40HADD CL , DL ; register to register SUB CL , 20H ; Immediate from register ADD BYTE1 , BL ; register to memory MOV CX , WORD1 ; word processing MOV DX , 2000HSUB CX , DX ; register from registerSUB CX , 124H ; Immediate from memoryADD WORD1 , DX ; register to memory
Instruction Before After
(a) MOV CX, 25H CX = 0000H CX=0025
(b) MOV CL, 0 CX = FFFFH CX=FF00
(c) MOV AX, BYTE1 AX=1234H AX=invalid size
(d) ADD DL, BYTE1 DX=0120H DX=0125
(e) XCHG AH,AL AX=1234H AX=3412
(f) SUB CX,CX CX=1234H CX=0000
(g) XCHG CX,CX CX=1234H CX=1234
Assume that BYTE1 is defined as DB 05, show the result for the following instructions:
For multiplication process,
• MUL instruction is used for unsigned data
• IMUL is used for signed data.
Both of these instructions affect the Carry and Overflow flag. The format for
MUL and IMUL instructions:
Observe that MUL/IMUL is a one address instruction, hence it requires an the
accumulator register to hold operand1 and result (refer Chapter 5.3 - Instruction
format) In IBM PC, the AX register (AL/AH/EAX) acts as accumulator.
The multiplication operations are byte times byte, word times word and
doubleword times doubleword.
For multiplying two one-byte values, the multiplicand (first operand) is in AL register,
and the multiplier (second operand) is a byte data in memory or another register that
determined by the address field in the MUL/IMUL instruction.
Example: MUL DL
the operation multiplies the content of AL by the contents of DL. The generated
product is in AX. The operation ignores and erases any data that may already be in AH.
Address field in the MUL/IMUL instruction refers only to the multiplier that will
determine the field sizes (whether byte, word, or doubleword). The instruction
will use the size of the address field (multiplier) to determine the position of the
multiplicand whether it is in AL, AX or EAX
A few examples on the MUL instruction together with the multiplier size,
multiplicand position and product:
MUL is used for unsigned data Examples on the usage of the MUL instructions using the data as defined below:
BYTE1 DB 80HBYTE2 DB 40HWORD1 DW 8000HWORD2 DW 2000HDWORD1 DD 00018402HDWORD2 DD 00012501H
(a) MOV AL, BYTE1 ; AL (multiplicand)=80HMUL BYTE2 ; byte x byte, product in AX
; 80H x 40H, AX= 2000H
(b) MOV AX, WORD1 ; AX (multiplicand)=8000HMUL WORD2 ; word x word, product in DX:AX
; 80000H x 2000H, ; DX:AX= 1000 0000H
Example 1 : (MUL Instruction)
MOV AL, BYTE1MUL BYTE2 ; byte x byte, product in AX
in the above example, 80H (128) is multiplied with 40H (64) and the result is 2000H (8,192) kept in AX register.
MOV AX, WORD1MUL WORD2; word x word, product in DX:AX
in the above example, 8000H is multiplied with 2000H and the result, 1000 0000H is kept in a pair of registers, DX:AX.
IMUL is used with signed dataExamples on the usage of the IMUL instructions using the data as defined below:
BYTE1 DB 80HBYTE2 DB 40HWORD1 DW 8000HWORD2 DW 2000HDWORD1 DD 00018402HDWORD2 DD 00012501H
(a) MOV AL, BYTE1 ; AL (multiplicand)=80H (-ve value) IMUL BYTE2 ; byte x byte, product in AX
; 80H (-ve) x 40H (+ve), AX= E000H
(b) MOV AX, WORD1 ; AX (multiplicand)=8000H (-ve value) IMUL WORD2 ; word x word, product in DX:AX
; 80000H (-ve) x 2000H (+ve), ; DX:AX= F000 0000H
Example 2 : (Arahan IMUL)
MOV AL, BYTE1
IMUL BYTE2 ; byte x byte, product in AX
in the above example, BYTE1 and BYTE2 is considered as signed data, that is –
128 (80H) and +64 (40H) and the result –8192 (E000H) is stored in the AX
register
MOV AX, WORD1
IMUL WORD2; word x word, product in DX:AX
in the above example, WORD1 and WORD2 is considered as signed data, that is
8000H a is –ve value and 2000H is a +ve value. The result, F000 0000H is a–ve
value and is kept in a pair of registers, DX:AX.
For division operation, the DIV instruction is used for unsigned
data whereas IDIV is used for signed data. Its format:
The basic divide operations are byte into word, word into
doubleword, and doubleword into quad word.
As in MUL/IMUL, address field in the DIV/IDIV instruction refers to the divisor (second operand) that determines the field sizes. The following example shows the divisor is in the register (example 1) and memory (example2) with a certain size.
Example1 : using register addressing mode
Example 2 : using direct addressing mode - divisor is predefined in the memory
The following are a few examples of the DIV instruction using the data definition below:
BYTE1 DB 80H ; Byte valueBYTE2 DB 16HWORD1 DW 2000H ; Word valueWORD2 DW 0010HWORD3 DW 1000H
(a) MOV AX, WORD1 ;AX=2000H DIV BYTE1 ;2000H/80H, quotient=40H, remainder=00H
;AL=40H, AH=00H
(b) MOV DX, WORD2 ;DX=0010H MOV AX, WORD3 ;AX=1000H,dividend in DX:AX (WORD2:WORD3)
;DX:AX = 0010 1000H DIV WORD1 ; 00101000H/2000H remainder:quotient in DX:AX
; 1000H:0080H
MOV AX, WORD1DIV BYTE1
In the above example, the value 2000H (8092) will be divided with 80H (128), the quotient, 40H (64) will be kept in the AL register while its remainder, 00H will be kept in the AH register.
MOV DX, WORD2MOV AX, WORD3 ; dividend in DX:AX (WORD2:WORD3)DIV WORD1; remainder:quotient in DX:AX
in the above example, the value 00101000H will be divided with 2000H. The remainder, 1000H will be kept in the DX register, whereas its result, 0080H will be kept in the AX register.
The following are a few examples of the IDIV instruction using the data definition below:
BYTE1 DB 80H ; Byte valueBYTE2 DB 16HWORD1 DW 2000H ; Word valueWORD2 DW 0010HWORD3 DW 1000H
(a) MOV AX, WORD1 ; AX=2000H IDIV BYTE1 ; 2000H(+ve)/80H (-ve),
; quotient=C0H (-ve), remainder=00H; AL=C0H, AH=00H
(b) MOV DX, WORD2 ;DX=0010H MOV AX, WORD3 ;AX=1000H,dividend in DX:AX (WORD2:WORD3)
;DX:AX = 0010 1000H (+ve) IDIV WORD1 ;00101000H (+ve)/2000H (+ve)
;remainder:quotient in DX:AX;1000H:0080H
(using the same data definition)
MOV AX, WORD1IDIV BYTE1
in the above example, the value WORD1 is a +ve number whereas BYTE1 is a –ve number. If WORD1 is divided with BYTE1, its result will be a –ve number (64 or C0H) will be kept in the AL register whereas its remainder will be kept in the AH.register
MOV DX, WORD2MOV AX, WORD3 ; dividend in DX:AX (WORD2:WORD3)DIV WORD1; remainder: quotient in DX:AX
in the above example the value 00101000H (+ve) will be divided with 2000H (+ve). Its remainder, 1000H (+ve) will be kept in the DX register, whereas its result, 0080H (+ve) will be kept in the AX register.
Divide 8003h by 100h, using 16-bit operands:
mov dx,0 ; clear dividend
mov ax,8003h ; dividend
mov cx,100h ; divisor
div cx ; AX = 0080h, DX = 3
Same division, using 32-bit operands:
mov edx,0 ; clear dividend
mov eax,8003h ; dividend
mov ecx,100h ; divisor
div ecx ; EAX = 00000080h, EDX = 3
DIV Examples
The End