You are on page 1of 4

Session-2020 Semester Fall 2021 CS-222L Computer Organization and Assembly Language

Lab Number. 3
CLO 1,2

AIM: Arrays implementation


Objectives:
· Arrays implementation using Indirect addressing in 8086.

Task 0 2 4 8 10

Perform addition Student Student Student take 25 Addition using Output is


of 25 numbers write the understand the numbers one addressing according to
structure of concept of task mode query
code
Perform Student Student Student take 25 Subtraction Output is
subtraction of 25 write the understand the numbers using only one according to
numbers structure of concept of task addressing query
code mode
Reverse the array Student Student Student create Student swaps Output is
write the understand the array properly the elements of according to
structure of concept of task arrays correctly query
code

Theory
REGISTER INDIRECT ADDRESSING IN 8086 ADDRESSING MODES
We have done very elementary data access till now. Assume that the numbers we had were 100
and not just three. This way of adding them will cost us 200 instructions. There must be some
method to do a task repeatedly on data placed in consecutive memory cells. The key to this is the
need for some register that can hold the address of data. So that we can change the address to
access some other cell of memory using the same instruction. In direct addressing mode the
memory cell accessed was fixed inside the instruction. There is another method in which the
address can be placed in a register so that it can be changed. For the following example we will
take 10 instead of 100 numbers but the algorithm is extensible to any size.
There are four registers in iAPX88 architecture that can hold address of data and they are BX,
BP, SI, and DI. There are minute differences in their working which will be discussed later. For
the current example, we will use the EDI register and we will take just three numbers and extend
the concept with more numbers in later examples.
Program1:
; a program to add three numbers using indirect addressing
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
Session-2020 Semester Fall 2021 CS-222L Computer Organization and Assembly Language

.data
num1 dword 5, 10, 15, 0
.code
main PROC
mov ebx, OFFSET num1 ; load address of num1 into destination index
mov eax, [ebx] ; load first number in ax
add ebx, 4 ; advance bx to second number
add eax, [ebx] ; add second number to ax
add ebx, 4 ; advance bx to third number
add eax, [ebx] ; add third number to ax
add ebx, 4 ; advance bx to result
mov [ebx], eax ; store sum at num1+13

INVOKE ExitProcess, 0
main ENDP
END main

OUTPUT:

One thing that we needed in our problem to add hundred numbers was the capability to change
address. The second thing we need is a way to repeat the same instruction and a way to know
that the repetition is done a 100-times, a terminal condition for the repetition. For the task we are
introducing two new instructions that you should read and understand as simple English
language concepts. For simplicity only 10 numbers are added in this example. The algorithm is
extensible to any size.
Program2:
; a program to add ten numbers
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
num1 dword 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
Session-2020 Semester Fall 2021 CS-222L Computer Organization and Assembly Language

.code
main PROC
mov edi, OFFSET num1 ; load address of num1 into destination index
mov ecx, LENGTHOF num1 ; load the number of elements in array in ecx register to use
;as counter
mov eax,0 ; initialize eax by 0
L1:
add eax, [edi] ; load first number in ax
add edi, 4 ; advance bx to second number
sub ecx, 1 ; counter
jnz L1
INVOKE ExitProcess, 0
main ENDP
end main
OUTPUT:

REGISTER + OFFSET ADDRESSING


Direct addressing and indirect addressing using a single register are two basic forms of memory
access. Another possibility is to use different combinations of direct and indirect references. In
the above example we used EDI to access different array elements which were placed
consecutively in memory like an array. We can also place in EDI only the array index and not
the exact address and form the exact address when we are going to access the actual memory.
This way the same register can be used for accessing different arrays and also the register can be
used for index comparison like the following example does.
Program3
; a program to add ten numbers using register + offset addressing
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
num1 dword 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
.code
main PROC
Session-2020 Semester Fall 2021 CS-222L Computer Organization and Assembly Language

mov edi, OFFSET num1 ; load address of num1 into destination index
mov ecx, LENGTHOF num1 ; load the number of elements in array in ecx register to use
;as counter
mov eax,0 ; initialize eax with 0
mov ebx, 0 ; initialize ebx with 0
L1:
add eax, [edi+ebx] ; load first number in ax
add ebx, 4 ; advance bx to second number
sub ecx, 1
jnz L1
INVOKE ExitProcess, 0
main ENDP
end main

Task:
1- Perform addition of 25 numbers using direct and Indirect addressing modes.

2- Perform subtraction of 25 numbers using direct and Indirect addressing modes.

3- Take an array and enter the elements into another array in reverse order (BONUS TASK)

You might also like