CS225,  Fall 2006,  Wednesday Oct 18, Day 23 Hit reload!

(Re) Read   2.3, C++ functions, ReRead  6.3 to p. 261, procedure with no parameters, Continue  to p.267.  Errata p. 267 in  pictures of stack for program on p. 266: (a):  all values are input to main by now, so the values should be the same as on p. 264 (only "upside down": i=1, value=13, numPts=12. ) Note there were already corrected errata 264 & 267, correcting n & value to 13, and correcting retAddr(p.267) to 0046.

HW Day 23, Due Fri Day 24 
<>Please hand in the Listing from each program.   And draw those arrows from each Branch to its target!

A.  Run, trace, and understand the program at the bottom of Day 22.  Modify it so it prints out your name.  Hand in the Assembler Listing (remember you can save from the listing pane and use Word to open and print that listing.  You can also print the listing directly, but it comes out Courier 14, big.).
Remember, Pep8beta8 is the best version for tracing stack activity, because the memory updates automatically.

(B.  Think about how to multiply, in binary.  Come in with a pseudocode or flowcharted algorithm.  If you already did it, great!)
 
C.  Write a procedure that gets 2 numbers from  (batch) input, adds them, and outputs the sum.  And a main program that just calls the procedure, twice in a row (with a carriage return between the two calls).  What is the total length of the stack frame (call frame) of your procedure?
D.  Do Exercise in addressing modes (handout)
The stack-pointer moving , stack-relative ones are especially tricky.  Just follow the RTL carefully.

 Program 6, Assigned Wed. Day 23, Due Monday Day 25, Oct. 21
Create a program with procedures, no parameters, no local variables, no returned values .  Some procedure should call another (so you have calls nested 2 deep), and some  procedure should be called twice.  It should be a little different from the one on Day 22. Hand in the listing, output,  and a memory trace, showing which ops move the stack pointer, and what is on the stack inside/after each procedure call.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Notes:  Hand in exams
Program 5; any surprises?

How to multiply whole numbers?   (Algorithms ready?  on board next time.)

Overview of procedures, Day 21

Procedure (function) call: CALL  address:  In calling program.
      Pushes PC (address of next instruction) on stack.   In RTL
                       SP<--SP-2
                       mem[SP]<--PC
      Branches to address
                       PC<--address
  
"CALL address", with no addressing mode, == "CALL address, i".  The OprndSpec=Operand is what gets put into the PC.

Return from Procedure  RET0    (unary) At End of Procedure.
     Pops PC from stack (returns control to calling program, right after where it left calling program.)
                        PC<--mem[SP]
                        SP<--SP+2
  (We also have RET1....RET7; we'll look at them today.)

Examples:  Program, Fig 6.18 , p. 259
Tracing:  Set Break points by putting checks in boxes in Assembler listing, to run through uninteresting code, switch to single step at interesting points.
       (0012 =last * in PrintTri())

The chunk of memory on the stack "for" a particular procedure call is the call frame or the stack frame (or the activation record).
    
It's better to think of the stack frames as being the things following "stack rules."  Inside a stack frame, we can mess around any old way.
Within a stack frame, we refer to memory locations using SP as the "base" and use an offset system.  The stack frame is created in chunks, and deallocated in chunks.  Chunks are LIFO (last in first out).

Nested procedure calls, no parameters & no return value: Example Day 22.

Procedure with no return value (void), no parameters, but local variables: (handout)
- - - - - - - - - - - -
;void function, no parameters, 2 local variables
;Put "cop & driver" into a procedure: (uses x for cop)
br main
;--------void--copdrive()
cop: .equate 2
driver: .equate 0
; return address is the word at 4
copdrive: subsp 4,i
ldx 0, i
stx cop, s
lda 40, i
sta driver, s
do: addx 25, i ;incr cop
adda 20, i ;incr driver
sta driver, s ;store for compare
cpx driver, s
brlt do ;end do
stx cop, s ;store for output
deco cop, s
RET4 ;Combines Addsp4, i and RET0
;
;-----------main()
main: call copdrive
charo '!', i
stop
.end

- - - - - - - - - - - - -
   Who does what?  Calling program   Procedure
(Compiler manages all this for us in C++. )
STACK FRAME:
Top:         Local variables  (all done by procedure)
                Return
address   (CALL, in calling program, puts it on stack, RETn, in procedure, pops.)
               Base (FBCF)
+ + + + + +
In class:  Start HW C:  Write a procedure that gets 2 numbers from  (batch) input, adds them, and outputs the sum.  And a main program that just calls the procedure, twice in a row (with a carriage return between the two calls).
& & & & & &
Start Exercise in addressing modes.
= = = = = = = = = =
Next:  Pass-by-value parameters:  copies of values go on the stack where?  Below the return address. (other side of  (handout)  Pasteable Example



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