CS225,  Fall 2006,  Monday Oct 23, Day 25 Hit reload! Long after class--sorry!

(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 25, Due Wed Day 26
<>Please hand in the Listing from each program.   And draw those arrows from each Branch to its target!

A.  Again: Reconsider your program that got two numbers and found the sum.  Rewrite the  procedure so that it has the two numbers  as pass-by-value parameters, adds them, and returns the sum.  (int sum(num1, num2))  The main program now gets the two numbers from input and calls the procedure, just once, then outputs the sum. 
What is the total length of the stack frame (call frame) of your procedure?

B.  Write a function that returns the smaller of two parameters, (int smaller(num1, num2)) , using the rule that a <b  is equivalent to a - b < 0.  Write a main program that gets 2 numbers from input and outputs the smaller.  Specify whether you wrote your program for Batch I/O or Terminal I/O.   Create a suitable set of test data and give the results of your testing.

PostponeC. Use Egyptian multiplication to multiply 13 x11, in Roman numerals.  In Arabic numerals (dividing and multiplying only by 2).
- - - - - - -
Program 7, Multiplication   Assigned Friday Day 24, Due Friday. Day 27, Oct. 27.
Program specification:
 
The program should get 2 numbers from Batch input and multiply them, then output the result. 
  The algorithm should be the shift-and-add algorithm (multiplication).  The C++  algorithm is in #24, p. 322 (You don't need to put it in a procedure/function). In fact the text presentation with prod being a pass-by-reference parameter seems unnatural--a function returning the product is more natural.)
Write the program to work for  input numbers > 0 and < 25510.   You do not need to check that the result is out of range (though it could print out as a negative number.  Why?).

You may write it with or without a function/procedure.  You may use global variables (direct addressing) or local (stack-relative), your choice. 
Create a suitable set of test data pairs, and submit the results from these as well as the listing.
 
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Notes:  Exams returned.  Solutions

= = = = = = = = = =
Last time: Pass-by-value parameters:  copies of values go on the stack where?  Below the return address. (other side of  (handout)  Pasteable Example
 Warford's convention.  Store the parameter values first, move the stack pointer after.  (So stores are at SP minus something).
(Homework questions? Day 24)
Now:  Return value:  Need empty space on stack for return value.  Put it LOWEST.  Handout   Pasteable
STACK FRAME:  Callee   Caller
Top:         Local variables  (all done by procedure)
                Return
address   (CALL, in calling program, puts it on stack, RETn, in procedure, pops.)
                Parameters (pass-by-value)
                Return value
               Base (FBCF)
      All of stack frame is addressible in the procedure by offsets from  the Top. 
    Make a Symbol for each Parameter's offset, and for Return value, remembering they're on the other side of the Return address.

        Calling requires
   -- Push (empty) space for return value.
   -- Push actual parameter values  (pass-by-value; copy the values to new memory locations on the stack)
   -- Push the return address (CALL)
   -- Push storage for the local variables ("allocate" them).
This chunk is the stack frame for the procedure.  SP moves to top of all this stuff.
Inside the procedure
Actual parameters and local variables are referenced by their offsets from SP.  "Formal" parameter is the same location as the pushed parameter value, referenced now  as offset from the top of the stack frame.
Returning requires
  -- Pop ("deallocate") local variables (just move SP.  ADDSP, or part of RETn)
  -- Pop return address into PC  (RET), or part of RETn)
  -- Pop actual parameter values (just move SP)  They may have been changed inside the procedure but are not used again.
  -- Pop space that returned value is in.  Value is now at SP -2.  Retrieve it and use it as required in main program.

Leaks?
A Procedure is supposed to encapsulate its work; not affect anything outside.  Nothing outside should affect inside of procedure except passed parameters and global variables, useable by anything.
What "leakiness" has Warford left in with his protocol?*

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
What "leakiness" has Warford left in with his protocol?*
* --Flags are not necessarily left the same.  RETn doesn't alter flags, but we don't have a way of clearing or setting flags directly.
* --A and X registers are (probably) changed.  Design choice:  You can push A, X register values onto stack for safekeeping, restore them afterward.
           We have so few, safer to assume Register is not what it was. (Especially since we are using A for all our pushes).  Notice when we use DECI, DECO, STRO, the registers are unchanged.
     (Registers can also be used explicitly to pass parameters, especially if you have lots of registers and a way to keep track.  Not suitable for this ISA.)

Do this WED
Egyptian multiplication (4000 years old):  Why it's not hard to do multiplication with Roman numerals or similar tally-like systems.
Recall binary algorithm:  12 *9          
 mcand = 12   mpr = 9                                   
 prod = 0  (holds running sum)                                      
While mpr != 0,
    halve mpr, examine remainder
     if remainder = 1
          prod = prod + mcand
   double mcand
That's all.
           mcand                    mpr         rem              prod
          X II                       IIIII IIII
halve                                IIII             I
add?       y                                                            X II
dbl       XX II II
halve                                II             -
add?      n
dbl        XX II II
             XX II II
halve                                  I              -
add?
dbl       XX XX IIII IIII
           XX XX IIII IIII
halve                                    -             I
add? y                                                             XX XX IIII IIII
                                                                       XX XX IIII IIII
dbl  (unnecessary)
                                         Tidy the result      XXXXX XXXXX
                                          108                                IIIII III    
 Yet to discuss:  Parameters: pass by reference.   Arrays.  Both require yet new addressing modes.


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