INDIRECT ADDRESSING
Computer Organization and
Assembly Language
Computer Science Department
National University of Computer and Emerging
Sciences Islamabad
LITTLE ENDIAN ORDER
Little endian order refers to the way Intel stores
integers in memory.
Multi-byte integers are stored in reverse order,
with the least significant byte stored at the lowest
address
For example, the doubleword 12345678h would be
stored as:
Byte Offset
When integers are loaded from memory
78 0000 into registers, the bytes are
automatically re-reversed into their
56 0001
correct positions.
34 0002
12 0003
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 2
BIG ENDIAN ORDER
The Most significant byte stored at the lowest
address
For example, the doubleword 12345678h would be
stored as:
Byte Offset
12 0000
34 0001
56 0002
78 0003
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 3
DIRECT MEMORY OPERANDS
A direct memory operand is a named reference to
storage in memory
The named reference (label) is automatically
dereferenced by the assembler
.data
var1 BYTE 10h
.code
mov al,var1 ; AL = 10h
mov al,[00010400] ; if var1 at offset 10400h
mov al,[var1] ; AL = 10h
alternate format
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 4
INDIRECT ADDRESSING
Data Related Operators
OFFSET Operator
PTR Operator
TYPE Operator
LENGTHOF Operator
SIZEOF Operator
LABEL Directive
Indirect Operands
Array Sum Example
Indexed Operands
Pointers
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 5
OFFSET OPERATOR
OFFSET returns the distance in bytes, of a label from
the beginning of its enclosing segment
offset
data segment:
myByte
The Protected-mode programs we write only have a single segment
(we use the flat memory model).
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 6
OFFSET EXAMPLES
Let's assume that the first element is at offset 00404000h:
.data
bVal BYTE ?
wVal WORD ?
dVal DWORD ?
dVal2 DWORD ?
.code
mov esi,OFFSET bVal
mov esi,OFFSET wVal
mov esi,OFFSET dVal
mov esi,OFFSET dVal2
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 7
RELATING TO C/C++
The value returned by OFFSET is a pointer. Compare the following code
written for both C++ and assembly language:
; C++ version:
char array[1000];
char * p = array;
.data
array BYTE 1000 DUP(?)
.code
mov esi,OFFSET array ; ESI is p
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 8
PTR OPERATOR
Overrides the default type of a label (variable). Provides the flexibility to
access part of a variable.
.data
myDouble DWORD 12345678h
.code
mov ax,myDouble ; error – why?
mov ah,BYTE PTR myDouble
mov WORD PTR myDouble,4321h
Recall that little endian order is used when storing data in memory
(see Section 3.4.9).
9
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 9
PTR OPERATOR EXAMPLES
.data
myDouble DWORD 12345678h
doubleword word byte offset
12345678 5678 78 0000 myDouble
56 0001 myDouble + 1
1234 34 0002 myDouble + 2
12 0003 myDouble + 3
mov al,BYTE PTR myDouble
mov al,BYTE PTR [myDouble+1]
mov al,BYTE PTR [myDouble+2]
mov ax,WORD PTR myDouble
mov ax,WORD PTR [myDouble+2]
10
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 10
PTR OPERATOR (CONT)
PTR can also be used to combine elements of a smaller data type and
move them into a larger operand. The CPU will automatically reverse
the bytes.
.data
myBytes BYTE 12h,34h,56h,78h
.code
mov ax,WORD PTR [myBytes]
mov ax,WORD PTR [myBytes+2]
mov eax,DWORD PTR myBytes
11
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 11
YOUR TURN . . .
Write down the value of each destination operand:
.data
varB BYTE 65h,31h,02h,05h
varW WORD 6543h,1202h
varD DWORD 12345678h
.code
mov ax,WORD PTR [varB+1] ; a.
mov bl,BYTE PTR varD ; b.
mov bl,BYTE PTR [varW+1] ; c.
mov ax,WORD PTR [varD+3] ; d.
12
mov eax,DWORD PTR varW ; e.
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 12
LENGTHOF OPERATOR
The LENGTHOF operator counts the number of
elements in a single data declaration.
.data LENGTHOF
byte1 BYTE 10,20,30 ; 3
array1 WORD 30 DUP(?),0,0 ; 32
array2 WORD 5 DUP(3 DUP(?)) ; 15
array3 DWORD 1,2,3,4 ; 4
digitStr BYTE "12345678",0 ; 9
.code
mov ecx,LENGTHOF array1 ; 32
13
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 13
TYPE OPERATOR
The TYPE operator returns the size, in bytes, of a
single element of a data declaration.
.data
var1 BYTE ?
var2 WORD ?
var3 DWORD ?
var4 QWORD ?
.code
mov eax,TYPE var1 ; 1
mov eax,TYPE var2 ; 2
mov eax,TYPE var3 ; 4
mov eax,TYPE var4 ; 8
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 14
SIZEOF OPERATOR
The SIZEOF operator returns a value that is equivalent to multiplying
LENGTHOF by TYPE.
.data SIZEOF
byte1 BYTE 10,20,30 ; 3
array1 WORD 30 DUP(?),0,0 ; 64
array2 WORD 5 DUP(3 DUP(?)) ; 30
array3 DWORD 1,2,3,4 ; 16
digitStr BYTE "12345678",0 ; 9
.code
mov ecx,SIZEOF array1 ; 64
15
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 15
SPANNING MULTIPLE LINES (1 OF 2)
A data declaration spans multiple lines if each line (except the last) ends
with a comma. The LENGTHOF and SIZEOF operators include all lines
belonging to the declaration:
.data
array WORD 10,20,
30,40,
50,60
.code
mov eax,LENGTHOF array ; 6
mov ebx,SIZEOF array ; 12
16
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 16
SPANNING MULTIPLE LINES (2 OF 2)
In the following example, array identifies only the first WORD
declaration. Compare the values returned by LENGTHOF and SIZEOF
here to those in the previous slide:
.data
array WORD 10,20
WORD 30,40
WORD 50,60
.code
mov eax,LENGTHOF array ; 2
mov ebx,SIZEOF array ; 4
17
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 17
LABEL DIRECTIVE
Assigns an alternate label name and type to an
existing storage location
LABEL does not allocate any storage of its own
Removes the need for the PTR operator
.data
dwList LABEL DWORD
wordList LABEL WORD
intList BYTE 00h,10h,00h,20h
.code
mov eax,dwList ; 20001000h
mov cx,wordList ; 1000h
mov dl,intList ; 00h
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 18
INDIRECT OPERANDS
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] ; dereference ESI (AL = 10h)
inc esi
mov al,[esi] ; AL = 20h
inc esi
mov al,[esi] ; AL = 30h
19
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 19
INDIRECT OPERANDS (CONT.)
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
Should PTR be used here? yes, because [esi] could
add [esi],20 point to a byte, word, or
doubleword 20
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 20
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
21
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 21
POINTERS
You can declare a pointer variable that contains the offset of another
variable.
.data
arrayW WORD 1000h,2000h,3000h
ptrW DWORD arrayW
.code
mov esi,ptrW
mov ax,[esi] ; AX = 1000h
Alternate format:
ptrW DWORD OFFSET arrayW
22
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 22
TWO-DIMENSIONAL ARRAYS
IA32 has two operand types which are suited to
array applications
Base-Index Operands
Base-Index Displacement
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 23
BASE-INDEX OPERAND
A base-index operand adds the values of two registers
(called base and index), producing an effective
address. Any two 32-bit general-purpose registers may
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. )
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 24
EXAMPLE: SUM OF A PARTICULAR ROW
mov cx, NumCols
mov bx, OFFSET table
add bx, RowSize
mov si, 0
mox ax, 0 ; sum = 0
mov dx, 0 ; hold current element
L1: mov dl, [bx+si]
add ax, dx
inc si
loop L1
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 27
BASE-INDEX-DISPLACEMENT OPERAND
A base-index-displacement operand adds base and
index registers to a constant, producing an effective
address. Any two 32-bit general-purpose registers may
be used.
Common formats:
[ base + index + displacement ]
displacement [ base + index ]
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 28
TWO-DIMENSIONAL TABLE EXAMPLE
Imagine a table with three rows and five columns. The
data can be arranged in any format on the page:
table BYTE 10h, 20h, 30h, 40h, 50h
BYTE 60h, 70h, 80h, 90h, 0A0h
BYTE 0B0h, 0C0h, 0D0h, 0E0h, 0F0h
NumCols = 5
Alternative format:
table BYTE 10h,20h,30h,40h,50h,60h,70h,
80h,90h,0A0h,
0B0h,0C0h,0D0h,
0E0h,0F0h
NumCols = 5
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 29
TWO-DIMENSIONAL TABLE EXAMPLE
The following code loads the table element stored in row
1, column 2:
RowNumber = 1
ColumnNumber = 2
mov ebx, RowSize
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 table[ebx] table[ebx + esi]
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 30
REFERENCE
Assembly Language for x86 Processors
Sec 4.1,4.2,4.3,4.4
Chapter#4
Sec 10.1 (Structure Example)
Chapter#10
Kip R. Irvine
7th edition
Indirect Addressing Computer Organization and Assembly Language – NUCES slide 31