In today's lab, you will explore the ways in which our Pascal compiler
stores parameters and local variables. In recent classes, we've discussed
the ways in which different kinds of
variables (global vs. local
vs. semi-local vs. parameter vs. return value; primitive vs. compound)
can be stored.
Collaboration: Feel free to work on this lab in pairs or trios.
Turning It In: Save your answers in a plain text file and submit it using the ECA.
Grading: I expect that you will gain more from doing this lab than from me grading this lab. I may simply scan through your answers to see if you had any particularly valuable insights.
Compiling Pascal Programs:
In order to compile Pascal programs, you will need to use the
gpc compiler on erdos. You should
-S flag to generate assembly code.
In order to complete today's lab, you'll need some guidance in reading GNU x86 assembly. Here are some key points.
.local varcan be referenced by name.
(%reg); that is, the memory location stored in the given register.
%espis the stack pointer.
%ebpis the beginning of frame pointer.
%edxare general purpose registers.
The following are some of the key instructions you'll encounter.
movl source, destination
Here's a simple Pascal program that reads a value, applies a function to that value, and prints out the result.
program example(input,output); var inval: integer; result1,result2: integer; function increment(val: integer): integer; begin val := val + 5; increment := val; end; function decrement(val: integer; delta: integer): integer; begin decrement := val - delta; end; begin readln(inval); result1 := increment(inval); result2 := decrement(inval,10); writeln(inval); writeln(result1); writeln(result2); end.
a. What steps are typically done before the call to a function?
b. What steps are typically done after the function returns?
c. What steps are done at the beginning and end of each function?
d. What do your previous answers tell you about stack frames in this compiler?
a. Where are
b. Where is
c. Where is the return value from
a. Compile your program to an executable and run it with input of 5.
b. Change the header for
increment to read
function increment(var val: integer): integer
What change do you expect this to have to the output of the program?
c. Verify your results experimentally.
d. Examine the assembly code for the modified program to see what the compiler has done differently.
Add some local variables to
and determien where those variables are stored.
Add a new function (and function call),
includes some interesting expression involving multiple subexpressions.
Determine where the intermediate results are stored. Make sure that
zebra has at least one local variable.
Add another function,
stripes, within the scope of
zebra. Make sure that it has at least one local variable
and that it uses both the parameters and local variables from
a. Where are the parameters to
stripes stored in a call?
b. Where are the local variables stored?
c. How does
stripes access the parameters of
d. How does
stripes access the local variables of
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Dec 10 08:53:33 2002.
The source to the document was last modified on Tue Nov 12 08:57:26 2002.
This document may be found at