Lab Manual 5 cao
Lab Manual 5 cao
Information Technology
Lab # 05
Arithmetic Instructions
Integer Addition and Subtraction Instructions
Possible Operands:
Operand1 Operand2
Register Register
Memory Register
Register constant data
Memory constant data
The integer in source is added to the integer at destination and the sum replaces the old value at
destination. The SF, ZF, OF, CF, PF and AF flags are set according to the value of the result of the
operation. A subtraction instruction also has the same format. The integer at source is subtracted
from the integer at destination and the difference replaces the old value at destination. One reason
that 2's complement notation is used to represent signed numbers is that it does not require special
hardware for addition or subtraction – the same circuits can be used to add/subtract unsigned and
2's complement numbers.
Two very useful instructions are inc and dec instructions which have the following formats:
Inc/Dec Operand Operation: Operand Operand + 1
These instructions treat the value of the destination as an unsigned integer. They are especially
useful for incrementing and decrementing counters. They sometimes take fewer bytes of code than
corresponding addition and subtraction instructions.
Another useful instruction is neg instruction having the following format:
Neg Operand Operation: Operand 0 - (OPERAND)
It negates (takes the 2's complement of) destination replacing the value in the destination by the
new value. Hence, a positive value gives a negative result and negative value will become positive.
Multiplication Instructions
There are two versions of multiplication instructions in the 80x86 assembly language. The mul
instruction is for unsigned multiplication. Operands are treated as unsigned numbers. The imul
instruction is for signed multiplication. Operands are treated as signed numbers and result is
Computer Architecture & Organization Department of Computer Science &
Information Technology
Positive or negative depending on the signs of the operands. The formats of these instructions are
discussed below.
MUL Operand Operation: AX AL * (8 bit value)
DX AX
AX * (16 bit value)
Single operand may be byte, word, double word or quad word in register or memory (not
immediate) and specifies one factor – that is the other number to be multiplied is always the
accumulator. Location of this factor is implied. For example, AL for byte-size source, AX for word
source and EAX for double word source. When a byte source is multiplied by the value in AL, the
product is put in AX. When a word source is multiplied by the value in AX, the product is put in
DX:AX (this strange placement is to keep backward compatibility) with the high-order 16 bits in
DX and the low-order 16 bits in AX. When a double word source is multiplied by the value in EAX,
the product is put in EDX:EAX with the high order 32 bits in DX and the low-order 32 bits in AX.
In each case the source operand is unchanged unless it is half of the destination. The format of imul
instruction as presented below: IMUL Operand
Division Instructions
The division operation generates two elements - a quotient and a remainder. The dividend is an
accumulator. This instruction can work with 8-bit, 16-bit or 32-bit operands. There are two types
of division instructions in x86 assembly. The instruction idiv source is for signed operands, while
div source is for unsigned operands. The source identifies the divisor which may be in byte, word,
double word or quad word. It may be in memory or register, but not an immediate operand. The
dividend is always double the size of divisor.
AH R[(AX)/S8]
AX Q[(DXAX)/S16]
DX R[(DXAX)/S16]
All division operations must satisfy the relation: dividend = quotient*divisor + remainder .For
signed division, the remainder will have same sign as dividend and the sign of quotient will be
positive if signs of divisor and dividend agree, negative otherwise.
EXERCISE:
Q1 Assumes num1 and num2 3 & 2 respectively, Your assembly code should generate the output
as mention below:
Num1+num2 = a ; where a,b,c,d are the results of specific operation.
Num1- num2 = b
Num1* num2= c
Num1 / num2= d
Computer Architecture & Organization Department of Computer Science &
Information Technology
.model small
.stack 100h
.data
num1 db 3
num2 db 2
sum db ?
diff db ?
prod db ?
quot db ?
msg1 db 'Num1+num2 = $'
msg2 db 13, 10, 'Num1-num2 = $'
msg3 db 13, 10, 'Num1*num2 = $'
msg4 db 13, 10, 'Num1/num2 = $'
.code
main:
mov ax, @data
mov ds, ax
; ADDITION
mov al, num1
add al, num2
mov sum, al
; SUBTRACTION
mov al, num1
sub al, num2
mov diff, al
; MULTIPLICATION
mov al, num1
mov bl, num2
mul bl
mov prod, al
; DIVISION
mov al, num1
mov ah, 0
mov bl, num2
div bl
mov quot, al
; OUTPUTS
; SUM
lea dx, msg1
mov ah, 09h
int 21h
mov dl, sum
Computer Architecture & Organization Department of Computer Science &
Information Technology
; DIFF
lea dx, msg2
mov ah, 09h
int 21h
mov dl, diff
add dl, 30h
mov ah, 02h
int 21h
; PROD
lea dx, msg3
mov ah, 09h
int 21h
mov dl, prod
add dl, 30h
mov ah, 02h
int 21h
; QUOT
lea dx, msg4
mov ah, 09h
int 21h
mov dl, quot
add dl, 30h
mov ah, 02h
int 21h
; EXIT
mov ah, 4ch
int 21h
end main
Output:
Q2 i. write an assembly language program , assume 8,3,2 values for variables x, y and z ,the output
should display an appropriate label and value of the expression x –2 y + 2z
Computer Architecture & Organization Department of Computer Science &
Information Technology
.model small
.stack 100h
.data
x db 8
y db 3
z db 2
result db ?
msg db 'Result of x - 2y + 2z = $'
.code
main:
mov ax, @data
mov ds, ax
; Calculate 2y
mov al, y
mov bl, 2
mul bl ; AL = 2*y
; Calculate 2z
mov al, z
mov bl, 2
mul bl ; AL = 2*z
; Compute x - 2y + 2z
mov al, x
sub al, bh
add al, bl
mov result, al
; Print label
lea dx, msg
mov ah, 09h
int 21h
; Print result
mov dl, result
add dl, 30h
mov ah, 02h
int 21h
; Exit
mov ah, 4ch
Computer Architecture & Organization Department of Computer Science &
Information Technology
int 21h
end main
Output:
Q2 ii. Modify above code by taking input for x, y and z ,and compute the value of x –2 y +
2z
.model small
.stack 100h
.data
x db ?
y db ?
z db ?
result db ?
msg1 db 'Enter x: $'
msg2 db 13,10, 'Enter y: $'
msg3 db 13,10, 'Enter z: $'
msg4 db 13,10, 'Result of x - 2y + 2z = $'
.code
main:
mov ax, @data
mov ds, ax
; Input x
lea dx, msg1
mov ah, 09h
int 21h
mov ah, 01h
int 21h
sub al, 30h
mov x, al
; Input y
lea dx, msg2
mov ah, 09h
int 21h
mov ah, 01h
int 21h
sub al, 30h
Computer Architecture & Organization Department of Computer Science &
Information Technology
mov y, al
; Input z
lea dx, msg3
mov ah, 09h
int 21h
mov ah, 01h
int 21h
sub al, 30h
mov z, al
; Calculate 2y
mov al, y
mov bl, 2
mul bl
mov bh, al
; Calculate 2z
mov al, z
mov bl, 2
mul bl
mov bl, al
; x - 2y + 2z
mov al, x
sub al, bh
add al, bl
mov result, al
; Output result
lea dx, msg4
mov ah, 09h
int 21h
Output:
Computer Architecture & Organization Department of Computer Science &
Information Technology
Q3 Examine the list file of Q2i and write down the bytes reserved in memory by x,y,z and
also note the operation codes of each instruction
Computer Architecture & Organization Department of Computer Science &
Information Technology
Computer Architecture & Organization Department of Computer Science &
Information Technology