Lab 3
Lab 3
Lab 3
Lab Number. 3
CLO 1,2
Task 0 2 4 8 10
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:
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.
3- Take an array and enter the elements into another array in reverse order (BONUS TASK)