You are on page 1of 11

Faculty Dated: ________________

Member:____________________

Semester:_____________ Section: ________________

Department of Electrical Engineering and Computer Science

EE-222 Microprocessor Systems


Lab4: ARRAY, ARRAY PRINTING AND ARITHMATIC OPERATIONS
MULTIPLICATION AND DIVISION
Name Reg. Viva / Quiz / Analysis of Modern Ethics Individual Total
No. Lab data in Lab Tool and and Team
Performance Report usage Safety Work

5 Marks 5 Marks 5 Marks 5 Marks 5 Marks 25 Marks


EXPERIMENT 04
ARRAY, ARRAY DECLARATION AND
ARITHMATIC OPERATIONS ADDITION AND SUBTRACTION

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,…

In assembly language Value is called as Initializer and Datasize is called as Initializer


Directive.

For example, an array is defined as follows

Array1 db 1,2,3,4 ; ASCII value of 49 is assigned to Var1


Array2 db ‘a’,’b’,’c’ ; no value is assigned to Var2, we can assign it inside CS
Array3 db ‘ABC’ ; Indirectly ASCII of A is assign to Var3
Array4 db ‘a’,’a’,’a’ ; same characters in array
Array5 db ?,?,?,? ; unassigned characters in array just space is reserved

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.

Array4 db 3 dup(‘a’) ; 3 is total number of characters having value ‘a’


Array5 db 4 dup(?) ; unassigned 4 characters inside array

ArrayName should not be a reserved op-code or operand e.g AL, BL, CL, DL, Sub, Add etc.

Initializer DB Define Byte 1 byte, 8 bits


Directive
DW Define Word 2 bytes, 16 bits

DD Define Double Word 4 bytes, 32 bits

DQ Define Quad Word 8 bytes, 64 bits

DT Define Ten Bytes 10 bytes, 80 bits


ACCESSING AN ARRAY:
Following two commands are used to translate the name @DATA into a number and moving into
a segment register.

MOV AX, @DATA

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.

MOV SI, OFFSET ARRAY1

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.

MOV DX, [SI]


SAMPLE CODE: -

; TITLE ARRAY ACCESSING AND PRINTING


.model SMALL
.stack 100h DATA

.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 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.

mov DX, [SI+1] ;2nd element Ar000 1


;OR Ar001 2
;Inc SI Ar002 3
int 21h
Ar003 4
; terminating
mov ah, 4ch
int 21h
main endp
end main
SAMPLE CODE FOR PRINTING ARRAY USING LOOP :-

;TITLE ARRAY ACCESSING AND PRINTING USING LOOP


.model SMALL
.stack 100h DATA

.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:

In case of multiplication, there are two components Multiplicand Multiplier


multiplicand and multiplier. If multiplier is of 8 bits
than the multiplicand will be of same bits i.e. 8 bits as 8-bits AL BL, CL, DL
shown in fig (3).
16-bits AX BX, CX, DX
MUL instruction is used for multiplication.

Syntax:

MUL multiplier Figure 3: shows multiplication process

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.

Example for 8-bit multiplication is 8-bits 8-bits 8-bits

Mov al, 3 ;moving 3 (multiplicand) into al AL X BL(a) = AX


Mov bl, 4 ;moving 4 (multiplier) into bl (a)
16-bits 16-bits 16-bits : 16-bits
Mul bl ;multiplying

Now where the result is stored?


AX X BX(b) = DX : AX
(b)
Result of two 8 bits number will be stored in AX.
Figure 4:(a) Shows 8-bit multiplication and (b) shows 16-
bit multiplication.
AAM (ASCII Adjust after multiplication)

This command will divide the AX into AH and AL.

Figure 5: Show ASCII adjust after multiplication result.


SAMPLE CODE FOR MULTIPLICATION: -

;TITLE 8-BIT MULTIPLICATION


.model SMALL
.stack 100h
.data
u db ? ; unit’s digit
t db ? ; ten’s digit
.code
Main proc
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
mov AX, 0
mov al, 3 ; moving multiplicand into al
mov bl, 2 ; moving multiplier into bl
mul bl ; multiplying and this will move result of multiplication into AX
AAM ; divides AX into unit and tens digit
mov u, al ; moving al into unit digit
mov t, ah ; moving ah into ten digit
mov dl, t ; first displaying ten’s digit
mov ah, 2 ; moving 2 in ah for single character printing
int 21h ; invoking interrupt
mov dl, u ; than displaying unit’s digit
int 21h
mov ah, 4ch ; terminating condition
int 21h
main endp
end main
Division:

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.

For division we need

1. Dividend (AX)
2. Divisor (BL, CL, DL)

After division the result is composed of two


components

1. Quotient (AL) Figure 6: Shows division process.


2. Remainder (AH)

DIV command is used for division.

Syntax: DIV divisor

The instruction divides 8 bit (or 16 bit) into 16 bits (or 32 bits). The quotient and remainder has
the same size as divisor.

BYTE FORM: In this form divisor is an 8-bit


register or memory location. The 16-bit
dividend is stored in AX, after division, the 8
bit quotient is in AL and remainder in AH.

Figure 7: Shows 8-bit division.

WORD FORM: Divisor is 16-bit register or


memory location. The 32-bit dividend is in
DX:AX. The quotient is in AX and remainder is
in DX (both are 16 bits)

Figure 8: Shows 16 -bit division.


Example of an 8-bit division

Mov Ax, 26 ;moving 26 (dividend) into Ax

Mov bl, 5 ;moving 5 (divisor) into bl

Div bl ;Division
SAMPLE CODE FOR DIVISION :-

;TITLE 8-BIT DIVISION


.model SMALL
.stack 100h
.data
q db ? ; quotient (single digit)
r db ? ; remainder (single digit)
.code
Main proc
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
mov ax, 0
mov ax, 26 ; moving dividend into al
mov bl, 5 ; moving divisor into bl
div bl ; dividing and this will move result of division into AX
mov q, al ; moving al into quotient
mov r, ah ; moving ah into remainder
mov dl, q ; first displaying quotient digit
mov ah, 2 ; moving 2 in ah for single character printing
int 21h ; invoking interrupt
mov dl, r ; than displaying remainder
int 21h
mov ah, 4ch ; terminating condition
int 21h
main endp
end main
PROCEDURE:
 As per discussion in class familiarize with defining a string in assembly language,
displaying it on screen.
 Write down the commands which you learnt today about defining a string and addressing
modes.

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.

You might also like