# Class 40: Handling Special Variables

Back to The Run-Time Stack. On to Wrapup.

This outline is also available in PDF.

Held: Monday, 5 December 2011

Summary: We consider some of the complexities in dealing with procedure parameters.

Related Pages:

Notes:

• The weekend was much less productive than I had planned. I'll leave it at that.
• Are there questions on Phase 6?

Overview:

• Background: What are we worrying about?
• Dealing with Reference Parameters.
• Dealing with Arrays.

## Background

• So far, we've been dealing with simple variables and parameters - integers, reals, and such.
• Structured types introduce some complexities.
• Arrays
• Records
• Variable parameters (aka pass by reference)
• What are the special issues?
• How do we determine how much space is required?
• How do we pass parameters?
• How do we deal with the parameters within the procedures?

## Reference Variables

• Consider the following
```procedure increment (var x: integer);
begin
x := x + 1;
end;
```
• Note that this is similar to the C code
```void
increment (int *x)
{
*x = *x + 1;
}
```
• What STAC code will we generate for this Pascal code? That requires us to think about a number of issues.
• Where do we store `x`? It's a parameter, so it should still be in the stack frame. Let's say it's at `%sp(-12)`.
• What kind of value do we store for `x` in the stack frame?
• An address.
• How do we get `x`'s value (not the pointer value, but the integer variable)?
• We'll need to dereference, but it's not clear that there's an appropriate addressing mode.
• I would use
```IMOV %r0 %sp(-12)       # Store the address in r0
IMOV %sp(-16) %r0(0)    # Copy the value in that address to a temp.
```
• How do we assign to `x`?
• That's an interesting question. We need to grab the address and then use the address.
```IMOV %r0 %sp(-12)
IMOV %r0(0) %sp(-20)
```
• When we call `increment`, what do we do? E.g.,
```var
y: integer;
begin
y := 2;
increment(y);
end.
```

## Arrays

• Our program to consider
```type
tenints: array[1..10] of integer;
function sum(vals: tenints): integer
var
s: integer;
i: integer;
begin
s := 0;
for i := 1 to 10 do
begin
s := s + vals[i];
end;
sum := s;
end;
```
• What is on the stack?
• How do we get `vals[i]`?
• What if the indices are not 1..10?
• What if we're dealing with multi-dimensional arrays?
• Row-major or column-major?

Back to The Run-Time Stack. On to Wrapup.

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 Wed Dec 7 10:53:46 2011.
The source to the document was last modified on Fri Aug 26 13:03:12 2011.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2011F/Outlines/outline.40.html`.

Samuel A. Rebelsky, rebelsky@grinnell.edu