Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following program written in Ada syntax with the execution stack sh

ID: 3823700 • Letter: C

Question

Consider the following program written in Ada syntax with the execution stack shown on the side. The line numbers are used to refer to the code and are not part of the code.

01

procedure env is

02

x_env: integer;

03

04

procedure a is

env

|_______|

1

05

x_a: integer;

a

|_______|

2

06

procedure b

is

d

|_______|

3

07

x_b: integer;

b

|_______|

4

08

procedure c

is

c

|_______|

5

09

x_c: integer;

a

|_______|

6

10

begin

d

|_______|

7

11

x_env = x_b +

x_a;

b

|_______|

8

12

a;

c

|_______|

9

13

end c;

a

|_______|

10

14

begin

d

|_______|

11

15

x_b = x_a;

b

|_______|

12

16

c;

17

end b;

18

19

procedure d is

20

begin

21

b;

22

end d;

23

begin

24

d;

25

end a;

26

begin

27

a;

28    end env;

Explain how the access links of activation records 6, 7, and 8 are found.

Explain how the address of x_a in line 11 is calculated. Also, give the formula for the address.

Explain how the address of x_a in line 15 is calculated. Also, give the formula for the address.

01

procedure env is

02

x_env: integer;

03

04

procedure a is

env

|_______|

1

05

x_a: integer;

a

|_______|

2

06

procedure b

is

d

|_______|

3

07

x_b: integer;

b

|_______|

4

08

procedure c

is

c

|_______|

5

09

x_c: integer;

a

|_______|

6

10

begin

d

|_______|

7

11

x_env = x_b +

x_a;

b

|_______|

8

12

a;

c

|_______|

9

13

end c;

a

|_______|

10

14

begin

d

|_______|

11

15

x_b = x_a;

b

|_______|

12

16

c;

17

end b;

18

19

procedure d is

20

begin

21

b;

22

end d;

23

begin

Explanation / Answer

Activation Record:

Activation is an execution of a subprogram. Generally in most of the languages, local variables are allocated when this execution begins (activation time). The storage required for activation is organized as an activation record (or frame).

In a language with recursion, each simultaneous activation of a recursive subprogram can have different parameters, different values for local variables, return a different result ...

• each activation needs its own activation record

• the number of activation records needed isn’t known at compile time

• allocation of activation records is dynamic, and local variables are stack dynamic (unless declared static)

In Ada the allocation of activation record can be on the stack.

Static links are set in the following way. Here we need to find the most recent occurrence of an ARI of the static parent of the subprogram being called. This could be done by following the dynamic links from one ARI to the next checking for some piece of data that uniquely identifies the ARI. However, there is a simpler method using the nesting depth of the subprograms. The compiler can determine this depth because it has had to analyze the nesting of subprograms.

The actual access to non-locals starts with the static link, since this is the static parent, but if there are references to non-locals outside this scope, then they must be searched for by following further static links.

In the activation records 6,7 and 8 links executes when program was loaded and running. We can easily access them while the program is being executed.

This can be done when the address of these records. The address of a variable can be calculated using address attribute. The Attribute 'Address returns the address of a variable.

To print or display using Address Attribute:

            with System.Address_Image;

            i: Integer;

            ...

            put_line("Address of i: " &System.Address_Image(i'address));

           

We will convert a value of an access type into a type that we can print.