## Problem set 2

Assigned: Feb. 7.
Due: Feb. 21.

### Problem 1

Consider the following pseudo-code. (The syntax is like C, but with lexical embedding of fuctions.) Assume that the language uses static scoping.
```int I,J;                      // Global variables

void F()                      // F has global scope
{ int I;

void G()                    // G is lexically inside F
{ int K;

void H()                  // H is lexically inside G
{  I = J + K;           // Body of H
print(I);
}                       // end of H

K=3;                     // Body of G
H();
J = J+10;
if (I < 10) F();
}                          // end of G

G();                        // body of F
}                             // end of F

void G()                      // G has global scope.
{ I = I+20;                   // Body of G.
F();
print(I);
}                             // end of G

main() {
I=1;
J=2;
G();
}.
```

A. What does this program print?

B. Show the structure of the stack at the point when it has the maximal number of activation records on it. Be sure to indicate:

• The activation records for the active function calls.
• The local variables for the functions calls.
• The dynamic link (i.e. the stored values of the frame pointer);
• The static link.
```

```

### Problem 2

Consider the following pseudo-code
```int I = 100;
J = 50;

void F()
{  I = I+1;
J = J+1;
print(I,J);
}

void G()
{  int I;
I = 10;
F();
print(I,J);
}

void main() { G(); }
```

A. If variables are statically scoped, what is printed?

B. If variables are dynamically scoped, what is printed?

### Problem 3

Let M be a two-dimensional array of integers. Assume 0-based indexing and row-major ordering. Let Q be a function that contains a reference to (a) M[2,3] and (b) M[I,J]. Describe how the compiled code allocates the space for M and locates the address corresponding to these references in each of the following cases: (You need not describe how variables I and J are located; you can assume that they are simply available, in registers or whatever. You need not worry about bounds checking.)

In all but (E) below, assume that A is a "true" two-dimensional array, and not an array of pointers to arrays. In all but (F), assume that the language uses static scoping.

Note: The answer has 12 parts: Each of M[2,3] and M[I,J] for each of A-F below.

• A. M is a global variable with dimensions 10 x 20.
• B. M is declared local to Q with dimensions 10 x 20.
• C. M is declared local to Q with dimensions A x B, where the values of A and B can be determined at the time that Q is called.
• D. M is declared local to Q, and its size changes dynamically during the execution of Q.
• E. M is an array of pointers to arrays, whose size is set dynamically during the execution of Q.
• F. The language is dynamically scoped. The dimensions of M may change dynamically during the execution of Q.