CS225,  Fall 2006,  Monday Oct 16, Day 22 Hit reload! after class

(Re) Read   2.3, C++ functions, Read  6.3 to p. 261, procedure with no parameters.  Read ahead, but take a rest at p.267

HW Day 22, Due Wed Day 23  Do well on the exam, due next time!  Also program 5.
<>Please hand in the Listing from each program.   And draw those arrows from each Branch to its target!

Postpone A.  Run, trace, and understand the program below.  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.
<>
Postpone Program 6, Assigned Day 22, due Friday, Day 24, October 20
to be 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 below  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:  HW questions?
On board, in pairs: (Day 19 from Day 13/14) Textbook p. 319  #13 Don't write the program.  Make a flow chart, with a diamond decision for each part of the if. (For example, one diamond for (ch>='A') and another for (ch<='Z') )  Hand in your original flow chart, and a linearized one.  (Notice, no loop.)  (Note: it took a while for a programmer trained in a context without compound decisions like (a &&b) to learn to use them.  It's probably just as hard to figure out how to do without.)

How to multiply whole numbers?   (We will program this.)

Overview of procedures, Day 21

6.3  Procedures.
Recall Run-time stack=user stack.
:  Built in to CPU.
SP stack pointer.  Gives Address of top of stack (latest occupied slot)
Stack grows Backward through memory from FBCF (FBCF is top of "empty" stack.  Our first push will be to (FBCE, pushing a byte,) or FBCD, pushing a word.)

"Function"~~"Procedure"~~"Subroutine" .  Functions usually return a value, Subroutines and Procedures usually only change parameters.  But language is imprecise.

Flow of code:  Insert a chunk of code (written down elsewhere) into a sequence.  Unconditional branch TO the chunk, and BACK to the next instruction after the branch away.  Problem: if you want to re-use this chunk somewhere else, there's no way to branch back to this different place, with our existing instructions. 
Need special Procedure-managing instructions.  Put return address on the stack!

Procedure (function) call: CALL  address:  In calling program.
      Pushes PC (address of next instruction) on stack.
                       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 soon.)

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

Tracing templates: 

These operations allow code to be reused in different places in a program, with control returning to the next instruction after the call!
Nested procedure calls: Example below.

For now, no parameters!  No local variables.  (Can't do much but output, immediate mode)

<>= = = = = = = = = =
;Procedure no params
;Nested calling of procedures. After Fig.6.18
;No parameters, no local variables, no return values.
;Just watch the stack as the procedures are called.
    BR     Main
Linefeed: .EQUATE 0x000A
;
;------void Name()
Name:     STRO Myname, d ;cout<< "Sally" <<endl
    CHARO Linefeed, i
    RET0     ;Must return before the data storage
Myname: .ASCII "Sally\x00"
        ;"constant-like" String storage stays with procedure code, not on stack
;------void Starz()
Starz:     STRO Line1, d
    CHARO Linefeed, i
    STRO Line2, d
    CHARO Linefeed, i
    RET0
Line1:  .ASCII "* * * *\x00"
Line2:  .ASCII " * * * \x00"
;------void Nicename()
Nicename: CALL Starz
    CALL Name
    CALL Starz
    RET0  ; What happens if I leave this off?
;
;------Main()
Main: CALL Name
    CHARO '?', i
    CHARO Linefeed, i
    CALL Nicename
    STOP
    .end
    CHARO


 


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