CS225,  Fall 2006,  Monday Oct 30, Day 28 Hit reload! After class

  Read  about Switch, p. 295-8.  Understand .ADDRSS dot command, BR Jtabl, x.

HW Day 28, Due Wed Day 29 
<>Please hand in the Listing from each program.   And draw those arrows from each Branch to its target!
A)  Think about data structures, variables needed for TicTacToe project, come in ready to break down into main& procedures.
B)  What code does this PEP "program" produce?  (Be sure you understand why.)
      
George: .equate 0x00AB
       Martha:  .ADDRSS George
                     Stop
                     .end
C)  What does C++ do if you feed a Switch statement an out-of-bounds case number (and you don't have a Default line).  Stop and tell you? Crash in an ugly way? Does nothing and goes on?   ??     How would you program for that result in PEP?    

Program 8, Assigned Day 27, Due Wed. Day 29, Nov. 1
Do #27, p. 323, in Warford.  Note hint on next page, which may or may not be useful, depending on how you choose to do it.

Program 9, assigned Day 28, Due Fri. Day 30, Nov. 3
 Interactive program.  Have the user input a number  0, 1, or 2, and print out a "fortune" for each case.  You may ignore the problem of screening for wrong responses.  Use a Jump table and BR with x mode.   EXTRA CREDIT:  Program a Default line (like p. 325 #34 has.)

Amnesty: 
Any  old "Programs" handed in  by Monday at 3pm will not be counted as late.  By Wed. at 3 pm will be only 1 day late.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

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  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

How do we find the offset?  suppose arr[r][c], r rows and c columns
Index  of arr[j][k]; j rows down and k columns over; starting to count with 0.
 j *c +k  will  get us to the right box.  (This easy formula is another reason why C++ starts counting at 0)
Multiply by the size of each element to get the offset.
Offset = 2*( j *c +k ) for 2-byte elements. 

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.)

.ADDRSS Symbol  puts the number that Symbol is, into memory at that place.  Specifically used for storing addresses.
0100 SwitchT:   .ADDRSS Case0  ; puts 00A0, the number matching the symbol Case 0 (= address for Case 0) in this place    
0102                 
.ADDRSS Case1  ; puts 00A8,   address for Case 1
0104                 
.ADDRSS Case2  ; puts 00B0,   address for Case 2
0106                  .ADDRSS Case3  ; puts 00BF,   address for Case 3
Produces identical code to the above.

Example p. 295-6 Fig. 6.40
If the choices for Case are 2 thru 5, they have to be re-calibrated to start at 0...


Group project it is:  CHOICES:
  TicTacToe or Chomp(stupid machine) in PEP
   


  

Fancier Addressing modes:  Investigate n (indirect),  sf, sx, sfx, "cold" using RTL (p. A4)



To Sievers Home Page
CS225-Fall06/Daya28.htm 
4pm, 10/30/06
This page belongs to Sally Sievers who is solely responsible for its content. Please see our statement of responsibility.