Primary:
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Sets:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Miscellaneous:
[2001S]
[98F]
[SamR]
[Glimmer Labs]
Summary:
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
pc or gpc compiler on erdos. You should
use the -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.
%ebp.
$24.
.local var can be
referenced by name.
(%reg); that
is, the memory location stored in the given register.
num(%reg) or
%reg(num).
%esp is the stack pointer.
%ebp is the beginning of frame pointer.
%eax through %edx are general purpose registers.
The following are some of the key instructions you'll encounter.
movl source, destinationpushl regpopl regcall labelretHere'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 inval and result1 stored?
b. Where is val stored?
c. Where is the return value from increment stored?
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 increment or decrement
and determien where those variables are stored.
Add a new function (and function call), zebra, that
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
zebra.
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 zebra?
d. How does stripes access the local variables of zebra?
Primary:
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Sets:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Miscellaneous:
[2001S]
[98F]
[SamR]
[Glimmer Labs]
Disclaimer:
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 http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2002F/Labs/lab.10.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby