CS225, Fall 2006, Monday Oct. 2, Day 17 Hit
reload! After class
(Re) Read Chapter 6.1. The user (run-time) stack, and
stack relative (s) addressing.
HW Day 17, Due
Wednesday
Day 18
<> Please
email me by 4 pm Tuesday 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 Wednesday's work on the basis of that.
Revised HW:
Do and hand in the HW from Day 16. (Yes, type in and run and
debug if necessary the ones we did in class. Plus.... Hand
in the listings, any input/output.
Also:
A. Step through Programs A, B, C below, examining the stack.
(Use Pep8beta8 version for watching the stack update as you
step.) Now to program C, add the instruction ADDSP5, i at the
end, run it, and make sure the stack pointer returns to FBCF.
Compare the code with program D1 on the backside of the handout, and
see that they are the same except for comments. Hand in your
listing to the modified C.
(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.)
(from Day 15--no programming)
A. Linearize the flowchart
on your IfThenElse handout. (Redraw it so all the boxes are in one
column.)
B. Run the C++ program of Figure 6.14 (p. 250) Verify that the
loop counter continues to exist and is one bigger than the last value
in the loop, after the loop is done. In a highly structured
language you might expect that it should disappear after the counted
loop is finished. Write down your opinion on using the
value in the loop counter after the loop is finished, when working in a
structured environment.
C. Make a detailed pseudocode analysis or a flowchart
for this problem: 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. (Don't code it!)
D. Read about little
and big endian in Null/Lobur (outside my door) , sec. 5.2.2 pp.
201-203.
a) Give an example contrasting them.
Did I get it right in class?
[I said: AB12 stored as PEP does is Big
endian (most significant "first" in lower-addressed bytes)]
b) Make a table Little
endian
Big endian and put these
into the correct columns:
INTEL (PC chip maker) , MOTOROLA (traditional Macintosh
chip maker), Unix, GIF, JPEG, MacPaint, PCPaintbrush (is this Paint?)
c) Give one advantage and one disadvantage of each
method.
Postpone all of these:
A. (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. Tracingtemplates.xls
You know almost enough to do
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.
Revisit Program 4 (which will be
re-assigned.) You know enough to do this one now (you knew enough
before today's class).
D??not yet...).
Think about how to multiply, in
binary. Come in with a
pseudocode or flowcharted algorithm.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = =
Notes: Put up p. 231, #25, #27 on
the board. Day 16
(Adapted from Day 12 )
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)
STA -4, s stores the contents of register A in the
word just above the word above the stack pointer If SP contains
FBCF then it stores in FBCB. (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
Three programs that accomplish the same
thing: (Handout)
- - - - - - -
;A. Push two numbers and a character on the stack
;Move SP (top) each time, after storing
BR Main
num1: .word 0x0A0B
num2: .word 0x0C0D
char: .byte 0x5A ;'Z'
;
;----------Main()
Main: LDA num1, d
STA -2, s
SUBSP 2, i
LDA num2, d
STA -2, s
SUBSP 2, i
LDBYTEA char, d
STBYTEA -1, s
SUBSP 1, i ;SP is at FBCF-5 = FBCA
STOP
.end
- - - - - - - - - - - - -
;B. Push two numbers and a character on the stack
;Move SP (top), only once, after storing all characters
BR Main
num1: .word 0x0A0B
num2: .word 0x0C0D
char: .byte 0x5A ;'Z'
;
;----------Main()
Main: LDA num1, d
STA -2, s
LDA
num2, d
STA -4, s
LDBYTEA char, d
STBYTEA -5, s
SUBSP 5, i ; SP is at FBCF-5 = FBCA
STOP
.end
- - - - - - - - - - - - - - - -
;C. Push two numbers and a character on the stack
;Move SP (top), only once, BEFORE storing all characters
BR Main
num1: .word 0x0A0B
num2: .word 0x0C0D
char: .byte 0x5A ;'Z'
;
;----------Main()
Main: SUBSP 5, i ;SP is at FBCF-5 =
FBCA
LDA
num1, d
STA 3, s
LDA
num2, d
STA 1, s
LDBYTEA
char, d
STBYTEA 0, s
STOP
.end
- - - - - - - - - - - - - - - - - - -
What do we do to pop everything at once (deallocate the stack
space)?
ADDSP 5, i
- - - - - - - - - - - - - - -
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.
Extra time? Think about multiplication.
;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.