16 Microprocessor Systems Lecture No 16 Indirect Addressing
16 Microprocessor Systems Lecture No 16 Indirect Addressing
Systems
Lecture No 16 Indirect Addressing
By Nasir Mahmood
This Lecture
Indirect Addressing
Indirect Operands
Arrays
Indexed Operands
Pointers
inc esi
mov al,[esi] ; AL = 20h
inc esi
mov al,[esi] ; AL = 30h
3
Indirect Operands (2 of 2)
Use PTR to clarify the size attribute of a memory operand.
.data
myCount WORD 0
.code
mov esi,OFFSET myCount
inc [esi] ; error: ambiguous
inc WORD PTR [esi] ; ok
4
Array Sum Example
Indirect operands are ideal for traversing an array. Note that
the register in brackets must be incremented by a value that
matches the array type.
.data
arrayW WORD 1000h,2000h,3000h
.code
mov esi,OFFSET arrayW
mov ax,[esi]
add esi,2 ; or: add esi,TYPE arrayW
add ax,[esi]
add esi,2
add ax,[esi] ; AX = sum of the array
5
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] ; AX = 1000h
mov ax,arrayW[esi] ; alternate format
add esi,2
add ax,[arrayW + esi]
etc.
6
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
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] ; 04
mov bx,arrayW[esi*TYPE arrayW] ; 0004
mov edx,arrayD[esi*TYPE arrayD] ; 00000004
7
Pointers
• You can declare a pointer variable that contains the offset of
another variable.
• Pointers are a great tool for manipulating arrays and data
structures, and they make dynamic memory allocation possible
8
Pointers : Example
.data
arrayW WORD 1000h,2000h,3000h
ptrW DWORD arrayW
.code
mov esi,ptrW
mov ax,[esi] ; AX = 1000h
Alternate format:
9
Using the TYPEDEF Operator
The TYPEDEF operator lets you create a user-defined type that has
all the status of a built-in type when defining variables. TYPEDEF is
ideal for creating pointer variables.
For example, the following declaration creates a new data type
PBYTE that is a pointer to bytes:
PBYTE TYPEDEF PTR BYTE
This declaration would usually be placed near the beginning of a
program, before the data segment. Then, variables could be defined
using PBYTE:
.data
arrayB BYTE 10h,20h,30h,40h
ptr1 PBYTE ? ; uninitialized
ptr2 PBYTE arrayB ; points to an array
THE END