Sie sind auf Seite 1von 13

Arrays

CS031 TA Lecture
October 17, 2011

Motivation
Wed i!e to see "o# to re$resent various
"i%"&eve an%ua%e constructs in M'(S)
One o* t"e si+$er constructs is an array)
Wed i!e to $ro%ra+ in M'(S #it" arrays
and understand "o# our "i%" eve code
+a$s to asse+by an%ua%e)

Arrays
,e+ents o* an array #i be stored in
conti%uous boc!s o* +e+ory o* an
a$$ro$riate si-e)
.o# do #e create arrays in +e+ory, and
"o# do #e access t"eir ee+ents/
W"at do #e do about 20 arrays/
'n #"at #ays can #e use arrays as
ar%u+ents *or our $rocedures/

0ecarin% Arrays in )data 10
,1a+$es
c"arArr2 )byte 023
intArr2 )#ord 1210
W"at is t"e o**set o* c"arArr456 *ro+ t"e
abe c"arArr/
Ans2 5 7 si-eo*8byte9 : 5
W"at is t"e o**set o* intArr436 *ro+ t"e abe
intArr/
Ans2 3 7 si-eo*8#ord9 : 12

Accessin% Arrays in )data 10
.o# do #e access c"arArr456/
i ;t0, 5 <o**set o* c"arArr456
b ;t1, c"arArr8;t09 <oad t"is byte
W"at about intArr436/
i ;t0, 12 <o**set o* intArr436
# ;t1, intArr8;t09 <oad t"is #ord
W"at "a$$ens i* #e try to access intArr4&26/
W"at about intArr4116/

0ecarin% Arrays in )data 20
Su$$ose #e #ant to create a 3 ro# by 5
cou+n array o* ints)
.o# +any #ords do #e #ant/
Ans2 315 : 15
We can use t"is decaration2
s2 )#ord 0215
=y convention, #e use ro# +a>or order2
T"e o**set o* s4ro#64co6 is ? 7 85 7 ro# @ co9
5 is t"e nu+ber o* cou+ns in s, and ? is t"e
si-e o* eac" ee+ent)

0ecarin% Arrays in )data 20
.ere are t#o di**erent #ays to vie# ro#
+a>or order2
T"e o**set o*
s4ro#6406 is
a#ays
divisibe by
t"e nu+ber
o* cou+nsA

More Arrays in )data
Accessin% 20 arrays is t"e sa+e as
accessin% 10 arrays once you "ave t"e
o**set)
.o# do #e use t"ese arrays in
$rocedures/ 0o #e need to $ass t"e+
as ar%u+ents/
Arrays decared in )data are %oba
variabes) We dont need to $ass
t"e+Bbut %oba variabes s"oud be
avoided 8Cead t"e Conventions DuideA9

Arrays on t"e Stac!
'nstead, #e can use t"e stac! *or our arrays)
0ecide "o# +uc" s$ace t"e array needs)
s$ace : ro#s 7 cos 7 si-eO*,e+ent
Aocate s$ace on t"e stac!) .ere, ro#s 7
cos is in ;t0 and eac" ee+ents si-e is
in ;t1) T"e array address #i %o in ;s0)
+ut ;t2, ;t0, ;t1
sub ;s$, ;s$, ;t2
add ;s0, ;s$, ?
=e sure to set eac" ee+ent to -eroA

Arrays on t"e Stac!
We can cacuate o**sets i!e be*ore)
W"en our arrays #ere decared in t"e
)data section, #e used reative inde1in%
i!e intArr8;t09)
'n t"is a$$roac", our arrays address is in
a re%ister, so #e "ave to add t"e o**set)
'* ;s0 is t"e arrays address and ;t0 is t"e
o**set, #e access t"e ee+ent i!e t"is2
add ;t1, ;s0, ;t0
# ;s1, 8;t19 <or b

Arrays on t"e Stac!
.o# do #e $ass t"ese arrays as ar%u+ents
to $rocedures/
We can $ass by re*erence) (ass t"e
address o* t"e array to t"e $rocedure)
We can $ass by vaue) (ass t"e si-e o*
t"e array and t"e contents o* t"e array
on t"e stac!)
'n your assi%n+ents, you be $assin% by
re*erence)

An ,1a+$e
Diven a 20 array o* inte%ers, t"e nu+ber o*
ro#s, and nu+ber o* cou+ns, *i a
ee+ents #"ere ro# : co #it" 1)

An ,1a+$e

Das könnte Ihnen auch gefallen