Given the following Pascal-like program:
program A;
var u : integer;
procedure B (x : integer);
var v : integer;
begin {B}
v := 2 * x;
end; {B}
procedure C (var y : integer);
var w : integer;
procedure D (var z : integer);
begin {D}
z := y + 2;
{ DRAW STATE OF SYSTEM AT THIS POINT }
. . .
end; {D}
begin {C}
B( y );
D( w );
end; {C}
begin {A}
u := 1;
C( u )
end. {A}
draw the stack and the activation records it contains after the execution of the first line of procedure D. Fill in the static links, dynamic links, local variables, and parameter fields. Include space in the activation record for storing the instruction pointer (IP). Use arrows to represent the static and dynamic links. Put the names of the formal parameters and local variables in their corresponding slots in the activation records, and indicate the contents of these fields using actual values (for value fields) or by drawing arrows to the corresponding actual parameters (for reference parameters). Include the stack and environment pointers (SP and EP).
Do the same exercise but using a display instead of static links. Be sure you also show the storage locations for previous values that were stored in the display, which would be needed to restore the display on the return from a subroutine.
Recall that, in Pascal, declaring a parameter with var indicates that the parameter is to be passed by reference.
Also recall that
{ A comment. }
is a comment in Pascal. (The alternative notation
(* A comment. *)
may also be used.)
You can use the p2c Pascal-to-C translator to (indirectly) compile the Pascal program, as follows:
p2c activ_records.pas cc -o activ_records activ_records.c -l p2c