2) For the program shown below, draw a picture of the run-time environment at th
ID: 3919286 • Letter: 2
Question
2) For the program shown below, draw a picture of the run-time environment at the point marked "HERE", and explain how variables x, y, and z, are accessed, for each of the following two techniques:
a) Static Links
b) Displays program
bar();
var b: integer;
procedure foo(var x: integer);
var x1: integer;
procedure baz(y: integer);
procedure zab(z: integer);
begin if z = 1 then zab(0) else x := y + z; // HERE
end:("zab") begin("baz") zab(1);
end("baz") begin("foo") z1 := 3;
baz(x1);
end:("foo") begin("main") foo(b);
end
Explanation / Answer
In this program if we consider the solution for static links at the run time environment can be described as:-
In statically-scoped languages variables are "non-local" in the nested sub-program that accesses them. These variables are stored in the activation record of the sub-program that declares them. When they are used as non-local variables, that activation record is found at runtime either using access static links or a display
bar();
var b: integer;
procedure foo(var x: integer);
var x1: integer;
procedure baz(y: integer);
procedure zab(z: integer);
begin if z = 1 then zab(0) else x := y + z; // HERE
end:("zab") begin("baz") zab(1);
end("baz") begin("foo") z1 := 3;
baz(x1);
end:("foo") begin("main") foo(b);
end
At the HERE instruction acivation record of zab procedure is checked for the value of z.
If the value is 1, zab(0) will be called with the value of z updated to 0.
else the activation records of foo, baz and zab are accessed to get the values of x, y and z and the value of x is updated as the sum of y and z in the activation record of foo.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.