Vous êtes sur la page 1sur 26

Arrays, Structures and Stack

Thorne: 35 83 131 Thorne : 3.5, 8.3, 13.1


(Irvine Edition IV : Section 4.4)
SYSC3006 1
The Power of Indirect Addressing The Power of Indirect Addressing
Wheres the advantage ?
MOV AX, var versus LEA BX, var
MOV AX, [BX]
The power of indirect addressing lies in its support for data structures
Arrays: A collection of elements all of the same type
int array[10];
array[0] =1; array[2] =5;
High level language selector is [ ]
y[ ] y[ ]
Recordsor Structures: A collection of elements of different types
struct {
char name[80];
High level
[ ];
int number;
} student;
student.number =123456;
language selector
is .
SYSC3006 2
student.number 123456;
SI and DI are index registers. Hmmm
Arrays are Indexable Data Structures
Access to each element is given by an address calculation g y
- Address of array[i] =start address of the array +i * sizeOfElement
- Offset depends on the size of the element
AVector
A2 D Matrix (4x3) A3 D Matrix (4x4x4)
p
The first element of a vector is associated with the index 0. Why ?
A Vector
A 2-D Matrix (4x3) A 3-D Matrix (4x4x4)
0
1
0
1
0 1 2
1
2
3
1
2
3
SYSC3006 3
ASMProgramming of Arrays is all about Addresses ASM Programming of Arrays is all about Addresses
Memory Map
.data
a1 db11h, 22h, 33h, 44h, 55h
DS:00
DS:01
DS:02
11h
22h
33h
a2 db 01h
db 02h
DS:02
DS:04
DS:03
DS:05
33h
55h
44h
01h
db 03h
a3db 2dup(0FFh)
DS:07
DS:06
DS:08
03h
02h
FFh
a3 db 2 dup(0FFh)
DS:0A
DS:0B
DS:09
??
??
FFh
C equivalent :
byte a1[5]; a1[0] =17;
b t 2[3] 2[1] 2
SYSC3006 4
byte a2[3]; a2[1] =2;
byte a3[2]; a3[1] =255;
ASMProgramming of Arrays is all about Addresses ASM Programming of Arrays is all about Addresses
Memory Map
.data
a1 dw11h, 22h, 33h, 44h
2d 01h
DS:00
DS:01
DS:02
11
00
22
a2 dw 01h
dw 02h
d 03h
DS:02
DS:04
DS:03
DS:05
22
33
00
00
dw 03h
a3 dw 2 dup(0FFh)
DS:07
DS:06
DS:08
00
44
01
DS:0A
DS:0B
DS:09
02
00
00
03 DS:0C
C equivalent :
int a1[4]; a1[0] =17;
int a2[3]; a2[1] =2;
SYSC3006 5
03 DS:0C

int a3[2]; a3[1] =255;


ASMProgramming of Arrays is all about Addresses ASM Programming of Arrays is all about Addresses
Memory Map
.data
a1 db01h, 05h
DS:00
DS:01
DS:02
01
05
02
db 02h, 06h
db 03h, 07h
DS:02
DS:04
DS:03
DS:05
02
03
06
07
a2 db 2*2 dup(00)
DS:07
DS:06
DS:08
00
00
00
DS:0A
DS:0B
DS:09
??
?? DS:0C
00
??
C equivalent :
byte a1[3][2]; a1[0][1] =5;
byte a2[2][2]; a2[1][1] =0;
SYSC3006 6
?? DS:0C
Structures
A group of related variables that can be accessed through a
common name.
Each item within a structure has its own data type, which can be
different.
struct catalog_tag {
char author [40];
char title [40];
To access :
card.author[0]
char pub [40];
unsigned int date;
unsigned char rev;
card.date
card.rev
g ;
} card;
where, the variablecard is of typecatalog tag.
SYSC3006 7
, yp g_ g
ASMProgramming of Structures is all about Addresses ASM Programming of Structures is all about Addresses
Memory Map
.data
card db 40 dup ($)
DS:00

DS:27
$

$
Author
db 40 dup ($)
db 40 dup ($)
DS:27

DS:28
DS:4F
$

$
$
Title
dw?
db ?

DS:50
DS:77

$
$
Pub
db ?
DS:79
DS:7A
DS:78
?
?
?
Date
Rev
SYSC3006 8
Example : Using Register Indirect for Array Programming Example : Using Register Indirect for Array Programming
Suppose we have array of integers declared:
X DW ; 1st element of array
Each element is 2 bytes long
X DW ; 1st element of array
DW ; 2nd element of array
. . . etc.
numX DW ; number of elementsinX numX DW ; number of elements in X
Write a program that sums the contents of the array into AX
i t t t l 0 i nt t ot al = 0;
f or ( i nt i =0; i <numX; i ++)
{
l [ i ] t ot al += x[ i ] ;
}
Use DI to hold the address of the current element.
SYSC3006 9
ie. It plays the role of x[i]
M M Memory Map
DS:00 00 x
AX = t ot al
[DI]
DS:01
DS:02
DS:03
0D
00
0F
CX = l oop count er i
dd f [ i ] DS:04
DS:05
DS:07
DS:06
01
40
D4
03
DI = addr ess of x[ i ]
DS:07
DS:08
DS:0A
DS:09
D4
00
00
2A
S:0
DS:0B
00
56

SYSC3006 10
Code Fragment Example:
MOV AX, 0 ; i ni t i al i ze sum
LEA DI, x ; i ni t i al i ze ar r ay i ndex
MOV CX, numX ; get # of el ement s
CheckForDone:
CMP CX, 0 ; any el ement s l ef t t o sum?
J E D J E Done
ADD AX, [ DI ] ; sumi
t h
el ement
ADD DI , 2 ; adj ust i ndex ( of f set )
SUB CX, 1 ; one l ess el ement
J MP CheckForDone
Why 2?
Done:
Register indirect addressing mode
SYSC3006 11
Code Fragment Example:
MOV AX, 0 ; i ni t i al i ze sum
MOV DI, 0 ; i ni t i al i ze i ndex of f set
MOV CX, numX ; get # of el ement s
CheckForDone:
CMP CX, 0 ; any el ement s l ef t t o sum?
J E D J E Done
start address of X is static dynamic!
DI holds
offset to
ADD AX, [ DI + X ] ; sumi
t h
el ement
( =ADD AX, x[ DI ] )
ADD DI , 2 ; adj ust i ndex ( of f set )
element
, ; j ( )
SUB CX, 1 ; one l ess el ement
J MP CheckForDone
I di t i d d dd i d
SYSC3006 12
Done:
Indirect indexed addressing mode
Code Fragment Example:
MOV AX, 0 ; i ni t i al i ze sum
LEA BX, x ; i ni t i al i ze base
MOV SI, 0 ; i ni t i al i ze i ndex of f set
MOV CX, numX ; get # of el ement s
CheckForDone:
CMP CX 0 l t l f t t ? CMP CX, 0 ; any el ement s l ef t t o sum?
J E Done
ADD AX [ BX + SI ] ; sumi
t h
el ement ADD AX, [ BX + SI ] ; sumi el ement
ADD SI , 2 ; adj ust i ndex ( of f set )
SUB CX, 1 ; one l ess el ement , ;
J MP CheckForDone
Done:
I di t b i d d dd i d
SYSC3006 13
Indirect base-indexed addressing mode
More Powerful Example of Base-Index Mode : 2-D Arrays.
0 1(column)
a1 db 01h, 05h
db02h, 06h
0 1 (column)
0
1 (row) db 02h, 06h
db 03h, 07h
C equivalent :
bytea1[3][2];
( )
2
MOV BX, offset a1
nextRow:
MOV SI, 0
byte a1[3][2];
for (int i=0;i<3;i++)
for (int j=0;j<2;j++)
a1[i][j] =10;
thisRow:
MOV [BX][SI], 10
ADD SI, 1
a1[i][j] 10;
BX: row index
CMP SI, 2*1
J B thisRow
ADD BX, 2*1
SI: column index
SYSC3006 14
CMP BX, a1+3*2
J B nextRow
Example : Using indexed mode on a structure
Assume we have the previous structure
definition :
Memory Map
struct catalog_tag {
char author [40];
char title[40];
DS:00

DS:27
$

$
Author
char title [40];
char pub [40];
unsigned int date;
unsignedchar rev;

DS:28
DS:4F
DS:50

$
$
$
Title
unsigned char rev;
} card;
Writeacodefragment toclear all fieldsof

DS:50
DS:77
DS:78

$
$
?
Pub
Write a code fragment to clear all fields of
the structure
Make all strings empty
Makeall datafieldsnull
DS:79
DS:7A
DS:78
?
?
?
Date
Rev
SYSC3006 15
Make all data fields null.
Structure Example:
AUTHOR EQU 0
TI TLE EQU AUTHOR+40
PUBLI SHER EQU TI TLE 40
Memory Map
card is memory
variabledefined
PUBLI SHER EQU TI TLE+40
DATE EQU PUBLI SHER+40
REVI SI ON EQU DATE+2
. dat a
d db 40 d ( ?) A t h
DS:00 $
Author
variable defined
in p8.
car d db 40 dup( ?) ; Aut hor
db 40 dup( ?) ; Ti t l e
db 40 dup( ?) ; Publ i sher
dw ? ; Dat e
db ? R i i

DS:27

DS:28

$
Title
db ? ; Revi si on
. code
MOV BX, of f set car d
constant is the
BX =constant

DS:4F

DS:50

$
Pub
offset of element
within structure.
BX =constant
start address of
structure.
DS:77
DS:79
DS:78
$
?
?
Date
MOV BYTE PTR [ BX+AUTHOR] , $
MOV BYTE PTR [ BX+TI TLE] , $
MOV BYTE PTR [ BX+PUBLI SHER] , $
MOV WORD PTR [ BX+DATE] 0
DS:7A ?
Rev
SYSC3006 16
MOV WORD PTR [ BX+DATE] , 0
MOV BYTE PTR [ BX+REVI SI ON] , 0
How about Arrays of Structures ?
typedef struct catalog tag{ typedef struct catalog_tag {
char author [40];
char title [40];
char pub[40]; char pub [40];
unsigned int date;
unsigned char rev;
}; };
catalog_tag library[100];
Writeacodefragment to
MOV BX, offset library
MOV CX 100
Write a code fragment to
clear all fields of all elements.
MOV CX, 100
nextBook:
MOV BYTE PTR [BX+AUTHOR], $
MOV BYTE PTR [BX+TITLE], $
MOV BYTE PTR [BX+PUBLISHER] $ MOV BYTE PTR [BX+PUBLISHER], $
MOV WORD PTR [BX+DATE], 0
MOV BYTE PTR [BX+REVISION], 0
ADD BX, 40+40+40+2+1
LOOPnextBook
SYSC3006 17
LOOP nextBook
Stack
Definition : A stack is a data structure with LIFObehaviour, that is
often used to hold values temporarily p y
Concept : A stack is a pile of things such that one thing is on topand
the rest follow beneath sequentially.
Example : A stack of papers
Last-In-First-Out Behaviour :
Each new thing is added to the top; the new thing is now on
top.
Change
State
Things are removed only from the top, and then the thing that
was below it in the pile is now on top.
Things below the top can be looked at if you know the position
f th thi l ti t th t
State
Read
State
of the thing relativeto the top.
Example : Look at the 2
nd
thing from the top.
State
SYSC3006 18
Stack
Existing Stack
Add from var1 Remove to var2 Read 1
st
to var3
C still exists!
A
B
A
B
C
A
B
C
A
B
C
Pointer
C var2 C var1 ?? var3
A! (in 1
st
)
(B in 0
th
)
Data can be read not only from the top if the address is known.
SYSC3006 19
y p
A Stack Implementation
A stack is implemented by reserving (1) a block of memory to hold
values and (2) a pointer to point to the value on top.
unused locations
next location for
adding a value
top pointer
item on top
next item
itemsinstack
g
What does that
arrow represent
?
last item
items in stack
SYSC3006 20
last item
Issues in a Stack Implementation
Should the stack grow from high-to-low addresses (as drawn
in picture), or vice versa ?
Conceptually thereisnodifference Conceptually, there is no difference.
By convention : stacks typically grows high-to-low
Upon initialization, when the stack is empty, what should the
toppointer point to? top pointer point to ?
Upon initialization : Usually top points just outside the
reserved block so that the next add will adjust pointer
beforecopyingvalueintothelocation before copying value into the location.
With a full stack (no space to add more items), what should
happen if an item is added ? Stack overflow
Wi h k h h ldh if i i With an empty stack, what should happen if an item is
removed ? Stack underflow
SYSC3006 21
The Hardware or Runtime Stack
P h b ilt i t k t ll dth ti t k Processor has built-in stack support called the runtime stack.
The top is maintained by dedicated pointer registers :
SS:SP
SS:SP points to a stack that holds 16-bit values.
The runtime stack grows down in memory (high-to-low
addresses)
Some instructions use the runtime stack implicitly (use/alter
SS:SP)
A program must initialize SP before using any stack operations. p g g y p
.stacksize
The assembler reserved specified number of bytes as block
of memorytousefor stack of memory to use for stack
The directive in translated into instruction to loader to
initialize SS and SP !!
SP pointsat bytejust above(highaddress!) last byte
SYSC3006 22
SP points at byte just above (high address!) last byte
reserved for stack
Intel Stack Instructions
PUSH operand Addsanewitemat topof stack PUSH operand Adds a new item at top of stack
Must specify 16-bit source operand
The operand may be register or memory
Stack grows down (to lower addresses):
SP := SP 2 // adjust pointer
mem[SP] :=operand // copy value to top [ ] p py p
POP operand Removes item from top of stack
Must specify16 bit destinationoperand Must specify 16-bit destination operand
The operand may be register or memory
operand :=mem[SP] // copy value to top
SYSC3006 23
SP := SP + 2 // adjust pointer
Intel Stack Instructions
To read an value in the stack, you need to index from the top
which is given by SS:SP
Natural solution : MOV AX, [SP +constant] u sou o : OV , [S co s ]
But recall the limitations on the indirect addressing
modes :
YoucanonlyuseBP BX SI DI You can only use BP, BX, SI, DI
But SS is default segment register for indirect
memory access using BP (Now well see why !)
Necessarysolution Necessary solution
MOV BP, SP
MOV AX, [BP+constant]
SYSC3006 24
Stack as a Storage Buffer Stack as a Storage Buffer
The stack can be used to temporarily hold data
Example: Suppose we need to save registers AX, BX and CX
PUSH AX
PUSH BX
PUSH CX
CX value
SP
PUSH CX
AX value
BX value
SP
SYSC3006 25
Stack as a Storage Buffer Stack as a Storage Buffer
Example: Suppose we now need to access the saved value of AX.
We could POP off the values off until AX is reached.
or
SP
We could index into the stack.
BX value
CX value BP
MOV BP, SP
MOV CX, [ BP +4 ] ; read saved AX
AX value
SP
BX value
CX value BP
+2
SYSC3006 26
AX value +4

Vous aimerez peut-être aussi