CS225,  Fall 2006,  Wednesday Oct 25, Day 26 Hit reload! After class--sorry for so late

  Read  about Arrays (global) 6.4 pp.282-286.  Understand indexed ( x ) addressing mode.

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

A. Use Egyptian multiplication to multiply 13 x11, in Roman numerals.  In Arabic numerals (dividing and multiplying only by 2).
X.--Do the HW problems A,B,C,D in C++ in the handout  Addresses in C++ handout. 
              Forgotten how?  http://aurora.wells.edu/~ccs/cs131/hints.htm
Postpone B.  a) Understand the program of Fig. 6.34, p. 284
      b) Rewrite it to process an array of 6 numbers
      c) Rewrite it to process an array of 4 characters.

- - - - - - -
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:  Rest ofExams returned? Solutions

What are we to do with our remaining 5 weeks?  Usually we do a joint programming project in Assembler, and I lecture about other interesting things as you work on the project & we plan, track results and critique.  I've taught enough (after arrays) that we can do it, and usually people enjoy it and find it a very worthwhile enterprise (software engineering without a net).  But there's not a lot of actual programming experience here yet.... CHOICES:
    Group project:  TicTacToe or Chomp(stupid machine) in PEP
    Separate projects (program something extensively in PEP; explore another environment at assembly language level)
    No project; more programs, more "book-learning" about other aspects of operating system and machine architecture.
Email me your thoughts/feelings.  Decision  will be announced Monday.  Decision may be partly based on whether I get any Program 7's on Friday.

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            prod
12           9
               4 r1              add 12
24
               2r0           don't add
48
               1r0         don't add
96
               0r1          add 96   
                               total 108
           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    
 
= = = = = = = = = =
Look back:(see day 24, 25)  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).
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.

 
What about call-by-reference parameters?  You have been told (correctly) that these are actually addresses of the variable in the calling program, and that when you use such a parameter inside the procedure, you're actually using the memory location outside the procedure.  So the Datum doesn't go into the stack frame, the Address of the datum does. (by Warford's convention, between the Return value and the call-by-value parameters. 
But we don't know how to get an address from a memory location and then get the data at that address yet.  (Another set of addressing modes.)

We can avoid this at the Asmb level by using global variables for any call-by-reference parameters.  (Won't work with recursive procedures, but we can live with that for the moment.)

So we'll look at ARRAYS next.  (Sec. 6.4, pp 282-286)
Declaration in C++: 
    char Msg[6] reserves 6 sequential character-size spaces. Msg[0] is the first, Msg[6] the last.  The bracketed number is the index.
     int Nums[5] reserves 5 sequential integer-size spaces.   
At Asmb level:  Put a label/symbol on the first byte of the first item in the array. That's your Base address. The addresses of the others can be found by using the index to calculate the offset to the base.
Index:  number of item at HOL level
Offset:  corresponding number to add to Base at Asmb level.
   This is a useful conceptual distinction which is not always followed in the literature.
This is the way arrays are structured in C++!  And you can get that information, using the facts that:
  --  you can declare a variable as an address (a pointer) using int *ptr (ptr is the address, *ptr is the integer at that address), and
   -- you can find out what the address of an ordinary variable is, because &num is the address of num.
  Addresses in C++ handout.
Start here Friday: 
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.
   Compare to stack relative: 
Oprndspec is the offset to SP.  Indexed: Oprndspec is the base, X the offset.
Examples:  handoutpasteable
    If array elements are one byte (1 address) long, then offset = index.
    If array elements are 2 bytes (2 addresses) long (e.g. PEP numbers), then offset = 2 times index.
    If array elements are 4 bytes (4 addresses) long (e.g. C++ integers), then offset = 4 times index.
Why does C++ start array indexes with 0?  More "civilized" languages allow you to start with 1, or sometimes your choice.  Ease of computation of offset.


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