CS 363 Homework #3 Due: April 12 classtime 1. Consider the program below: program main(); var A,B: integer; procedure new(B,C: integer) begin C := 5; B := B + C; write(A,B,C); end begin A := 3, B := 4; new(A,B); write(A,B); end What five values are printed if the parameter passing mechanism is * Pass-by-value? * Pass-by-reference? * Pass-by-value/result? 2. Consider a program in a statically scoped language that allows procedures to be nested inside other procedures. The variables declared local to each inner scope are given as parameters. ----------------------------------------------- |Program Main | | int x,y,z,a; | | ------------------------------------ | | procedure C(int a, int b, int c) | | | | -------------------------------- | | | | | procedure A(int x, int y) | | | | | | body of A | | | | | -------------------------------- | | | | -------------------------------- | | | | | procedure B(int r, int s) | | | | | | body of B | | | | | -------------------------------- | | | | body of C | | | ------------------------------------- | | ------------------------------------- | | | procedure E (int x, int y) | | | | | | | | -------------------------------- | | | | | procedure D (int x, int y) | | | | | | body of D | | | | | -------------------------------- | | | | | | | | body of E | | | ------------------------------------- | | body of Main | | | |end Main | ----------------------------------------------- * For the body of each procedure (Main, A - E), list the referencing environment along with where each visible variable is declared. * At runtime, each procedure will need an activation record. What is the purpose of an activation record? What fields would the activation record for procedure E contain? * Consider the following chain of procedure calls (each call represented by -->): Main --> E --> C --> A --> B --> B --> E Draw the runtime stack for this sequence of calls. Obviously many of the fields of the individual activation records will have to be left blank because you don't have enough information. However, be sure to include values for both the static and dynamic links. ----------------------------------------------- |Program Main | | int x,y,z,a; | | ------------------------------------ | | procedure C(int a, int b, int c) | | | | -------------------------------- | | | | | procedure A(int x, int y) | | | | | | B; | | | | | -------------------------------- | | | | -------------------------------- | | | | | procedure B(int r, int s) | | | | | | B; | | | | | | E; | | | | | -------------------------------- | | | | A; | | | ------------------------------------- | | ------------------------------------- | | | procedure E (int x, int y) | | | | | | | | -------------------------------- | | | | | procedure D (int x, int y) | | | | | | | | | | | -------------------------------- | | | | | | | | C; | | | ------------------------------------- | | E; | | | |end Main | -----------------------------------------------