CS225,  Fall 2006,  Wednesday Sept.20, Day 12  Hit reload!

Homework coming in: Questions?
Read  Ch 2.1, pp. 33-40 (C++, local variables, run-time stack), and Ch 6.1 (stack-relative addressing and run-time stack)

New assignment policy:  "Noncompletion penalties", as percent of possible.  Starting with Day 11 assignment.
Assume for illustration that  any given assignment or program is worth 10 points.  Check + = 10, Check = 9, Check minus = 8, Minus = 6. 
  One class day late:  subtract 1 point (10%)
   2 class days late:  subtract 2 points(20%)
   3 class days (1 week) late:  subtract 4 points (40%)
   4 or more class days late:  Worth 2 points, 20% of possible.  Will not be read, only recorded.
"Day handed in" includes getting it to me before I leave campus for that day.
Extreme extenuating circumstances may be considered, but not the usual pressing circumstances of student life.
 HW Day12, Due  Day 13
A. Run the program of Fig. 6.1 p.237, trace it on the registers template, follow the changes of memory and the stack pointer on the stack-memory changes template.  (Understand stack-relative addressing, SUBSP, ADDSP)  Hand in your traces.
B.  Do the same with the program of Fig. 6.4, p. 240. Tracingtemplates.xls

Program 3, Due Monday, Sept. 25  Prepare a stack frame long enough for two characters and a number, by moving the stack pointer to the top of the frame.    Input two characters followed by a decimal number,  pushing each one onto the stack, below the stack pointer.   Output the second character, then  the number, then the first character.  De-allocate the stack space by moving the stack pointer back to its original place.
Example input:  ab 100 produces output b100a.
Submit the listing with your input and output, and a memory changes template showing the changes in the stack pointer and the memory for all instructions that affect them.

Notes: 
Global variables
get assigned space as we have been doing it; that is absolute memory addresses.  But everything (almost) in C++ is done with local variables, since even "main" is a function (procedure)  [Procedure: function without return value]. 
How are procedures/functions handled?
A stack frame  is created:  a chunk of space on the "run time" = "user" stack.   The procedure operates in this space, when it finishes, the stack frame is deallocated (the  whole space is "popped")

Canonical Stack data structure:  Push one element, stack pointer moves up.  Pop one element, stack pointer moves down.  Illegal to reach down below the top of the stack for anything.  But:  the so-called stack in our computer  is just a bunch of sequential memory.  The stack pointer just identifies a particular memory location.  You have been told that the rule for stacks is LIFO.  But there is nothing preventing us from using other memory locations above or below the stack pointer, if we can label them.
Stack frame:  What gets pushed and popped is a chunk of space; within the frame we can access individual elements.  (3rd item down, etc.)

PEP8: User stack grows "up" from FBCF:  SP= Stack Pointer points to top item on stack.  So FBCF is the position of the pointer when the stack is empty.  "Next" byte: is FBCE=FBCF-1, next is FBCD=FBCF-2, etc.  To push a word onto the empty stack, would store at FBCF-2.
Stack-relative addressing: (s).  Operand specifier is the "offset" from present stack pointer.  Oprnd = mem[SP + OprndSpec]
    STA  -2, s 
stores the contents of register A in the word just above the stack pointer.  (It doesn't  move the stack pointer.)  (say  push 0A0B)
    STA -4, s stores the contents of register A in the word just above the word above the stack pointer.  (say push 0C0D)

Moving the stack pointer:  SUBSP 4, i    will subtract 4 from the stack pointer.
 ( If we have just pushed 2 words, this will leave the SP   at the word we last stored.)  ADDSP 4, i will add 4 to the stack pointer, returning it to "base."

              -4     0C0D       0 <---After SUBSP 4, i
              -2     0A0B       2
empty--> 0      xxxx         4

Rather than move the stack pointer after each push or pop, we move it all at once, either before or after.  (Just be careful.)
Example programs:  Fig. 6.1, Fig. 6.4  Tracing memory in the stack.
   PEP annoyances:  It doesn't clear out the stack memory on reloading, or even on loading a new program,  so it's hard to see what's happening.  You can copy your program, close and reopen PEP, paste it back.  Or load & run the program below that zeros out 16 bytes.  
Also remember when you store, the operand shown  in the CPU is the value in memory before you store over it.



;Zero: program to zero out 16 bytes of stack space.
;No loop, so a bit tedious.
br main
zero: .equate 0
main: lda zero, i
sta -2, s; trace--"operand" shows what's in memory
sta -4, s ; Before you store 0 over it.
sta -6, s
sta -8, s
sta -10, s
sta -12, s
sta -14, s
sta -16, s
stop
.end




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