EE109 – Computer Organization & Assembly Language
Lecture 23- Indirect Addressing 14th Oct, 2009 4 , 9 Humaira Ehsan
2
Humaira, Fall 09 EE109-L23
Indirect Addressing g
• • • • Indirect Operands Array Sum Example y p Indexed Operands Pointers
3
Humaira, Fall 09 EE109-L23
Indirect Operands p
An indirect operand holds the address of a variable, usually an array or string. It can be dereferenced (just like a pointer). .data val1 BYTE 10h,20h,30h .code mov esi,OFFSET val1 mov al,[esi] l [ i] inc esi mov al,[esi] ,[ ] inc esi mov al,[esi]
; d dereference ESI (AL = 10h) f
; AL = 20h
; AL = 30h
4
Humaira, Fall 09 EE109-L23
Indirect Operands p
.data myCount WORD 0 .code mov esi,OFFSET myCount inc [esi] inc WORD PTR [esi]
(cont.)
Use PTR to clarify the size attribute of a memory operand.
; error: ambiguous ; ok
Should PTR be used here?
add [esi],20
yes, because [esi] could point to a byte, word, or doubleword
5
Humaira, Fall 09 EE109-L23
Array Sum Example
Indirect operands are ideal for traversing an array. N I di d id l f i Note that the register h h i in brackets must be incremented by a value that matches the array type. .data arrayW .code mov mov add add add add WORD 1000h,2000h,3000h esi,OFFSET arrayW ax [esi] ax,[esi] esi,2 ax,[esi] esi,2 ax,[esi]
; or: add esi,TYPE arrayW
; AX = sum of the array
ToDo: Modif this e ample for an arra of double ords Modify example array doublewords.
6
Humaira, Fall 09 EE109-L23
Indexed Operands
An indexed operand adds a constant to a register to generate an effective address. There are two notational forms: [label + reg] label[reg] .data arrayW WORD 1000h,2000h,3000h .code mov esi,0 mov ax,[arrayW + esi] [ W i] mov ax,arrayW[esi] add esi,2 add ax,[arrayW + esi] ,[ y ] etc.
; AX = 1000h ; alternate format
ToDo: Modify this example for an array of doublewords doublewords.
7
Humaira, Fall 09 EE109-L23
Index Scaling
You can scale an indirect or indexed operand to the offset of an array element. This is done by multiplying the index by the array's TYPE: .data arrayB BYTE 0 1 2 3 4 5 0,1,2,3,4,5 arrayW WORD 0,1,2,3,4,5 arrayD DWORD 0,1,2,3,4,5 .code mov esi,4 mov al,arrayB[esi*TYPE arrayB] l B[ i*TYPE B] mov bx,arrayW[esi*TYPE arrayW] mov edx,arrayD[esi*TYPE arrayD]
; 04 ; 0004 ; 00000004
8
Humaira, Fall 09 EE109-L23
Pointers
You can declare a pointer variable that contains the offset of another variable. .data arrayW WORD 1000h 2000h 3000h 1000h,2000h,3000h ptrW DWORD arrayW .code mov esi,ptrW mov ax,[esi] ; AX = 1000h
Alternate format: Alt t f t
ptrW DWORD OFFSET arrayW
9
Humaira, Fall 09 EE109-L23
Two-Dimensional Arrays y
• IA32 has two operand types which are suited to array applications
▫ Base-Index Operands ▫ Base-Index Displacement
10
Humaira, Fall 09 EE109-L23
Base-Index Operand
• A base-index operand adds the values of two registers (called base and index), producing an effective address. A t dd Any two 32-bit general-purpose registers may 32 bit l i t be used. • Common formats:
[ base + index ]
• Base-index operands are great for accessing arrays of structures. (A structure groups together data under a single name ) name.
11
Humaira, Fall 09 EE109-L23
Structure Application S A li i
A common application of base-index addressing has to do with addressing arrays of structures (Chapter 10). The following defines a structure named COORD containing X and Y screen coordinates:
COORD STRUCT X WORD ? Y WORD ? COORD ENDS ; offset 00 ; offset 02
Then we can define an array of COORD objects: y j
.data setOfCoordinates COORD 10 DUP(<>)
12
Humaira, Fall 09 EE109-L23
Structure A li ti St t Application
The following code loops through the array and displays each Y-coordinate:
mov mov mov L1:mov call ll add loop ebx,OFFSET setOfCoordinates esi,2 ; offset of Y value ecx,10 ax,[ebx+esi] WriteDec W it D ebx,SIZEOF COORD L1
13
Humaira, Fall 09 EE109-L23
Base-Index-Displacement Operand p p
• A base-index-displacement operand adds base and index registers to a constant, p g producing an g effective address. Any two 32-bit general-purpose registers may be used. • Common formats:
[ base + index + displacement ] displacement [ base + index ]
14
Humaira, Fall 09 EE109-L23
Two-Dimensional Table Example
Imagine a table with three ro s and fi e columns The ith rows five columns. data can be arranged in any format on the page:
BYTE 10h 10h, 20h 20h, 30h 30h, 40h 40h, 50h BYTE 60h, 70h, 80h, 90h, 0A0h BYTE 0B0h, 0C0h, 0D0h, 0E0h, 0F0h NumCols = 5
Alternative format:
table
table
BYTE
10h,20h,30h,40h,50h,60h,70h, 80h,90h,0A0h, 0B0h,0C0h,0D0h, 0E0h,0F0h
NumCols = 5
15
Humaira, Fall 09 EE109-L23
Two-Dimensional Table Example
The following code loads the table element stored in row 1, column 2:
RowNumber = 1 ColumnNumber = 2 mov ebx,NumCols * RowNumber mov esi,ColumnNumber , mov al,table[ebx + esi]
150 155 157
10
20
30
40
50
60
70
80
90
A0
B0
C0
D0
E0
F0
table t bl
table[ebx] t bl [ b ]
table[ebx t bl [ b + esi] i]
16
Humaira, Fall 09 EE109-L23
Example: Sum of a Particular Row p
mov ecx, NumCols mov ebx, OFFSET table add ebx, (NumCols*RowNumber) mov esi, 0 mox ax, 0 ; sum = 0 mov dx, 0 ; hold current element L1: mov dl [ebx+esi] dl, add ax, dx inc esi loop L1
17
Humaira, Fall 09 EE109-L23
Problem # 2
• Write a procedure to multiply the two matrices and store the result in third matrix The matrix. procedure must take three arguments: first matrix, second matrix and a matrix for result. The algorithm for multiplication of matrices is given here.
18
Humaira, Fall 09 EE109-L23
void mm_mul (int A[N][N], int B[N][N], int C[N][N]) { int i, j, k; int sum; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) f (j 0 j j ) { sum = 0; for (k = 0; k < N; k++) f (k 0 k N k ) { sum += A[i][k] * B[k][j]; } C[i][j] = sum; } } }
19
Humaira, Fall 09 EE109-L23
Data Declaration
sum word 0 matrix1 byte 3 DUP(3 DUP(0)) matrix2 byte 3 DUP(3 DUP(0)) result word 3 DUP(3 DUP(0))
20
Humaira, Fall 09 EE109-L23
Problem 3
• Write a procedure to evaluate an arithmetic expression. expression The procedure takes the expression as string input from the user and displays the result on the screen. User can enter maximum of 60 character string. • User Input: {(4+3)*((5+7) (3+2))}