Notes: ARRAYS: See day 26, ,
day 27
In PEP: Use indexed addressing mode (x).
Oprnd = Mem[Oprndspec + X]
A label (symbol) labels the address of the
first byte of the first item, the Index register holds the
offset.
Double arrays
short arr[5][4] is best thought of as 5 rows, each with
4
elements. 5*4= 20 elements.
Each element takes 2 bytes. 40 bytes total.
INDEX (OFFSET) CONTENTS
(contents randomly chosen numbers)
| 0 |
1 |
2 |
3 |
|
| 0 |
0 0
155 |
1 2
135 |
2 4
126 |
3 6
134 |
| 1 |
4 8
136 |
5 10
190 |
6 12
146 |
7 14
144 |
| 2 |
8 16
180 |
9 18
180 |
10
20 167 |
11 22
143 |
| 3 |
12 24
180 |
13 26
188 |
14 28
176 |
15 30
125 |
| 4 |
16 32
197 |
17 34
178 |
18 36
166 |
19 38
135 |
Switch (Case) statement (remaining from Flow of control handout)
Switch (ans) {
Case 0 : <code0>
; break;
Case 1: <code1> ;
break;
Case 2: <code2> ;
break;
} // break is the literal
equivalent of unconditional BR to the end of the Switch structure.
How do we know where to branch TO?
In PEP, the symbols Case0, Case1, Case2 would stand for the
addresses of their code segments,
and we could program as a series of if's:
LDA ans, d
CPA 2, i
BREQ Case2, i :If ans = 2, Branch to Case 2.
But we're supposed to be able to calculate
where to jump to. How?
If the code segments were all equally long,
like 8 bytes apart, ans = 0, 1, 2
00A0 Case0: <code0>
BR EndSw
00A8 Case1: <code1>
BR EndSw
00B0 Case2: <code2>
BR EndSw
we could treat the cases like an array:
Calculate the address for the case corresponding to ans as
Case0+8*ans. BR (Case0+8*ans), i.
But our assembly language doesn't allow calculations in
the Oprndspec place.
Calculate it in A, Store it in memory with STA AdrCase, d,
and do BR AdrCase, d. Branch to the address which is
stored in location AdrCase.
But our assembly language doesn't
allow d mode with BR.
BUT if X
contains 0, BR AdrCase, x is identical with BR AdrCase, d.
So we can do that.
BUT usually the code for the
cases is not the same length.
00BF Case3: <code3>
But use the idea of branching to an
address, where the address is stored in memory.
JUMP TABLE: an array of addresses
in memory.
Addr symbol contents
0100 SwitchT: 00A0 ; address for Case
0
0102
00A8 ; address for Case 1
0104
00B0 ; address for Case 2
0106
00BF ; address for Case 3
SwitchT is the symbol for 0100, the memory
location which holds the address for Case 0
LDX ans, d
ASLX ; to make word size offset for switch.
BR SwitchT, x ; SwitchT is the
base of the array, X holds the offset, we branch to the address in
memory in that location.
How do we put addresses into memory
locations? (especially since we don't know what they are till we've
assembled.)
| To Sievers Home Page |
CS225-Fall06/Daya28.htm
|
|