Compilers (CS362 2002F)

Stack Frames

Preliminaries

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.

Background

In order to complete today's lab, you'll need some guidance in reading GNU x86 assembly. Here are some key points.

The Basics

Addressing

Registers

Instructions

The following are some of the key instructions you'll encounter.

movl source, destination
Copy information from source to destination.
pushl reg
Push the contents of the register on the stack.
popl reg
Pop the top of the stack and store it in reg.
call label
Call a procedure.
ret
Return from a procedure.

Exercises

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.

Exercise 1: Stack Frames and Function Calls

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?

Exercise 2: Variable Locations

a. Where are inval and result1 stored?

b. Where is val stored?

c. Where is the return value from increment stored?

Exercise 3: Variable Parameters

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.

Exercise 4: Local Variables

Add some local variables to increment or decrement and determien where those variables are stored.

Exercise 5: Compound Expressions

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.

Exercise 6: Local Functions

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?

 

History

Tuesday, 12 November 2002

  • Created.
  • Some notes on gas86 taken from the previous course.

 

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 ; Valid CSS! ; Check with Bobby

Glimmer Labs: The Grinnell Laboratory for Interactive Multimedia Experimentation & Research
glimmer@grinnell.edu