Professional Documents
Culture Documents
Lab 4
Lab 4
Member:____________________
OBJECTIVE:
In this lab you would perform following arithmetic operations
1. Multiplication
2. Division
Getting introduced to the registers and basic commands used to perform arithmetic
operations i.e. multiplication and division in 8086 assembly language.
To familiarize with the initialization of array in assembly language.
Defining and displaying an array using loop.
EQUIPMENT:
SOFTWARE:
1. Turbo assembler(TASM)
DISCUSSION:
ARRAYS
Why arrays are used? let say 1, 2, 3, 4 are the numbers that can be store as a
variable. Now if they are stored in variables separately. Therefore 4 variables
are required to initialize. Following 4 variables are initialized. These variables
were stored inside RAM at random locations where the space is available as
shown in fig (1). So there is no sequence/order in which these variables are
stored.
Var1 db 1
Var2 db 2
Figure 1: shows ram with random
Var3 db 3 locations where variables are
stored.
Var4 db 4
If an array is used instead of variables than these four numbers could be
stored in single variable of array, secondly all these values will be stored in
sequence/order inside RAM as shown in fig (2). So array is the collection of
characters in a sequence.
DEFINING ARRAYS
Figure 2: Shows RAM with
Arrays are defined in the .data directive of the program structure. sequence of locations for
array elements.
Syntax: ArrayName Datasize Value1,Value2,Value3,…
Now as in Array4 same character has been initialized three times. Instead dup (duplicate) can be
used. If we initialize Array4 and 5 again using dup.
ArrayName should not be a reserved op-code or operand e.g AL, BL, CL, DL, Sub, Add etc.
MOV DS, AX
Now to hold the array address source index (SI) register will be utilized. Source index register will
be used as a pointer to access array.
In this instruction offset address of array1’s first character will be moved to SI.
To print array1’s first character SI should be moved to DX but remember the value at that address
needs to be printed, to do that [SI] is used. [ ] are used to access the value at that particular address.
.data
CODE
Array1 db 1,2,3,4
.code
Main proc moving AX into DS, (register
addressing only)
; accessing address of data in code
mov AX, @data ; It moves memory location of @DATA into AX (16-bit)
mov DS, AX ; DS gets initialized as Heap memory to access variables
; Printing Array1
mov SI, offset Array1 ; offset holds the beginning address of Array1
.data
CODE
Array1 db 1,2,3,4
.code
Main proc moving AX into DS, (register
addressing only)
; accessing address of data in code
mov AX, @data ; It moves memory location of @DATA into AX (16-bit)
mov DS, AX ; DS gets initialized as Heap memory to access variables
; Printing Array1
mov SI, offset Array1 ; offset holds the beginning address of Array1
mov cx, 4 ; loop to run 4 times
L1:
mov DX , [SI] ;moving value at SI address into DX first element of array
mov ah , 2 [ ] are used to access the value at
int 21h that particular address.
inc SI Ar000 1
loop L1 Ar001 2
; terminating Ar002 3
mov ah, 4ch
Ar003 4
int 21h
main endp
end main
ARITHMATIC OPERATIONS:
The last two kinds of instructions which are used for basic arithmetic operations which are
1. Multiplication
2. Division
5 * 2
Multiplication:
Syntax:
MUL source
One number is considered in source which may be a register or memory location (source) and the
other is in AL for byte multiplication and for word multiplication in AX. For byte multiplication
the 16 bit result is saved in AX and for word multiplication the 32 bit result is saved in DX:AX
(least significant 16 bits in AX and most significant 16 bits in DX). The source cannot be a
constant.
In case of division, there are two components dividend and divisor. By default, dividend is always
stored in AX (16-bits) and divisor can be BL, CL, DL etc.
1. Dividend (AX)
2. Divisor (BL, CL, DL)
The instruction divides 8 bit (or 16 bit) into 16 bits (or 32 bits). The quotient and remainder has
the same size as divisor.
Div bl ;Division
SAMPLE CODE FOR DIVISION :-
EXERCISES:
1. Write an assembly code to add following two arrays into a new array using loop.
Array1 = 1,2,3,4
Array2 = 2,3,4,5
ArraySum = Array1 + Array2
2. Add two numbers and display the result on screen, your program should be capable of
separating the two digits of sum if sum is greater than 9, and should display both digits.
(HINT: Use division). It is necessary to add 30h to result before you display it in order to
get the ASCII equivalent of the result.
3. Write an assembly code for doing addition and subtraction of 3-digit numbers, program
should be capable of separating all the digits of numbers and their results i.e. addition and
subtraction.
4. Perform basic encryption and decryption on the input data for encryption enter an alphabet
and program should return an encrypted alphabet that is 3 positions ahead and for
decryption the program should return the digit that is 3 alphabets back to the entered
character? (HINT: see ASCII table and all alphabet characters in that)
5. Perform multiplication and division of two 2-digit numbers, your program should be
capable of separating all the digits of numbers and their results.
HOME TASKS
6. Perform 16-bit multiplication of two numbers (both composed of 2 digits) in assembly.
7. Perform 16-bit division of two numbers (Dividend 2 digit, Divisor 1 digit) in assembly.