CS225, Fall 2006, Wednesday Oct. 2, Day 18 Hit
reload!
(Re) Read Chapter 6.1. The user (run-time) stack, and
stack relative (s) addressing. Read Ch. 6.2, Branching and flow
of control
HW Day 18, Due Fri
Day 19
<> Please
email me by 4 pm Thursday and tell me what you have succeeded in doing,
what you have tried and had trouble with, and what you haven't started
to try. I'll make up Friday's work on the basis of that.
A1. For program D2 from the Stacks handout, write the C++ program it
corresponds to. (I won't be marking on syntax or whether
the #include stuff is right, just on the basic ideas.)
A2. (Finish class work) Write a program
that
gets a decimal number from input, stores it in a
Local variable on the stack, doubles the number, and outputs the
result. The Stack frame must be created at the beginning of main
and
the space de-allocated at the end.
B. Take the Wallspace problem
from Day 16, and rewrite it so that door, and wall, are Local
variables. (Wallspace, if a constant, will stay the same. If you
chose to make it a global, it can remain a global.)
From Day 12HW:
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. (This one
models the creation of the stack frame for main() ) Tracingtemplates.xls
(from Day 15:)Tracing the stack: In the Beta8
version of PEP (Pep8beta8Win folder in the lab) the memory window
updates automatically when you step an instruction (and your position
on the scrollbar stays fixed too). So it's really nice for
tracing the stack. Disadvantages: The only window
you can save as a file is the source code window. And that's
iffy. It said it saved but didn't, multiple times, for me. But:
Copy and paste work in all windows except the Assembler Listing.
You can run the beta version and the 8.03 version in separate windows
and copy/paste the source code from one to the other (if you can keep
track of what you're doing. I suggest cleaning up syntax errors
in 8.03 before going to Beta; 8.03 is more robust that way I think.)
You now can do (and it's not hard): PLEASE DO THIS IF
NOTHING ELSE!
Program 3, Now Due Friday, Oct. 6
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.
From Day 13 HW
B. I wrote Day 13's programs so that the numbers
were stored in an absolute address (the equivalent of a global variable
in C++).
Modify Program B (bottom of Day 13) so that the numbers are stored on
the user stack
(the equivalent of a local variable). (Day 13)
Hand in or email
code, hand in listing.
C. a) A very simple PEP program, no ifs, no loops, you may
use "global" type "variables". Get a number from input, and
multiply it by 8; output that.
Hint: 8 is a
power of 2. Multiplying by 2 can be done how?
b) Modify your program of part a to do
integer division by 8. For example, 16/8 = 2, 19/8 = 2, 26/8 = 3
Hand in or
email
code, hand in listings.
Revisit Program 4 (which will be
re-assigned.) You know enough to do this one now (you knew enough
BEFORE today's class).
Branches on CPare: From Day 15
HW (modified) : Text p. 318 #4 Print
out the program of
figure 6.16.
Put in all the arrows. There are small bits of "dead",
"inaccessible"
code, that never will be executed. Find and mark
these. "Reverse-engineer" this code into a flowchart, not
necessarily linear, with decisions like (n1<n2?). Try to
figure out what the program does from that.
Revisit Program 4 (which will be
re-assigned.) You know enough to do this one now (you knew enough
BEFORE today's class).
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = =
Notes: (stack
stuff Adapted from Day 12 )
Put on board answers to C:
count the number of 1's in the rightmost 4 bits
of the binary representation of a number input from the keyboard.
Output the original number and the number of 1's.
Circulate "Flowchart" linearizations. "Spaghetti code?"
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")
<>PEP8: User stack grows "up" (backward) 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. If SP
contains FBCF then it stores in FBCD. (It
doesn't move the stack pointer.) (say we push
0A0B)
Moving the stack pointer: SUBSP 4, i will
subtract 4 from the stack pointer.
ADDSP 4, i will add 4
to the stack pointer, returning it to "base."
>
Three programs that accomplish the same thing: (Handout), Day 17
- - - - - - - - - - - - - - - - - - -
What do we do to pop everything at once (deallocate the stack
space)?
ADDSP 5, i (Get program D1 on
handout)
- - - - - - - - - - - - - - -
How can we use this to compile, from C++,
the main procedure,
with local variables?
Memory space on the stack is allocated for locals, using offsets
from the Stack Pointer to locate it.
Labels (Symbols) can be used for the offsets.
- - - - - - - - - - - - - -
;D2. Same, with locals’ offsets given as symbols
;Move SP (top), only once, BEFORE storing all characters
BR Main
num1: .word 0x0A0B ;global
num2: .word 0x0C0D ;global
char: .byte 0x5A ;'Z' ;global
;
;----------Main()
n1: .equate 3 ; number
1’s local offset
n2: .equate 1 ;
number 2’s local offset
ch: .equate 0 ;
char’s local offset
;
Main: SUBSP 5, i ;Create Stack frame, SP =
FBCF-5 = FBCA
LDA
num1, d
STA n1, s ; store global in local space
LDA
num2, d
STA n2, s ; store global in local space
LDBYTEA
char, d
STBYTEA ch, s ; store global in local space
ADDSP 5,
i ; Deallocate frame, SP = FBCF
STOP
.end
+ + + + + + + + + ++ + + + + + +
In class: Write a program that gets a decimal number from
input, stores
it in a Local variable on the stack, doubles the number, and outputs
the result. The Stack frame must be created at the beginning of
main and the space de-allocated at the end.
*&*&*&*&*&*&*&*&*&*&*&
Return to IF-Then-Else. Handout, Day 13, notes
;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
This page belongs to Sally Sievers who is solely
responsible
for its content. Please see our statement
of responsibility.