CMPS 350 HW 10: Implementing Subprograms

Click 'check' on each question or your score will not be recorded.

resources:
hw10.c

Note: the term 'activation record' and 'call frame' refer to the same thing. For the purpose of this class assume 'dynamic pointer' and 'frame pointer' are the same thing and that the frame pointer points to the top of the previous call frame.


01. Which statements are true concerning static activation records (SARs)? 
    A. recursion is not possible with SARs 
    B. fixed-size SARs are loaded onto the runtime stack
    C. SARs are stored in the data segment in the executable 
    D. most modern languages use SARs
A B C D
02. Select the true statement concerning the dynamic link (frame pointer) in
    in a stack frame.
    A. The dynamic link points to the top of the activation record of the 
       previous procedure on the stack. 
    B. The dynamic link points to the top of the activation record of the 
       current procedure on the stack. 
    C. The dynamic link points to the location of the local dynamic variables
       of the current procedure on the stack.
A B C
03. When is a dynamic link (frame pointer) to the previous call frame necessary?
    A. if the size of the current call frame changes during execution
    B. if the size of the current call frame is not known at compile time
    C. if call frames do not have a default size
    D. if you wish to pop call frames out-of-order.
A B C D
04. The call chain of the stack (the chain of dynamic links) traces
    A. back through the activation records of each function called, starting 
       with the most recent function and ending in the main function.
    B. forward through the activation records of each function called, starting 
       with the main function and ending with the most recent function.
A B
05. What happens when foobar is called in a static-scoped language that does 
    not support nested subroutines? 
      void foobar () {
         X = 10;   // a non-local reference 
      } 
    A. the reference to X is bound to the instance of X on the runtime stack
       that corresponds to foobar's closest static ancestor.  
    B. the reference to X is bound to the closest activation record to foobar
       on the runtime stack that contains an instance of X.
    C. the reference to X is bound to an instance of X by the compiler/linker.
A B C

The next questions refer to this GNU C code and gdb trace for the call fac(3). In gdb, $sp points to the top of the stack and $fp points to the bottom of the current call frame (i.e., the top of the previous call frame).

   int fac ( int n ) {
      if ( n <= 1 ) return 1;
      else return n * fac ( n - 1 );
   }
   (gdb) break fac
   (gdb) run
   Breakpoint 2, fac (n=3) at hw10.c:120
   (gdb) p &n
   $7 = (int *) 0xffbefa04
   (gdb) cont
   Breakpoint 2, fac (n=2) at hw10.c:120
   (gdb) p &n
   $8 = (int *) 0xffbef98c
   (gdb) cont
   Breakpoint 2, fac (n=1) at hw10.c:120
   (gdb) info stack
   #0  fac (n=1) at hw10.c:120
   #1  0x0001090c in fac (n=2) at hw10.c:121
   #2  0x0001090c in fac (n=3) at hw10.c:121
   #3  0x000108bc in main () at hw10.c:114
   (gdb) x/i 0x0001090c    # i displays memory in instruction code format
   0x1090c :       mov  %o0, %g1
   (gdb) p $sp
   $9 = (void *) 0xffbef858
   (gdb) p $fp
   $10 = (void *) 0xffbef8d0
   (gdb) info frame
   Stack level 0, frame at 0xffbef8d0:
    pc = 0x108d8 in fac (hw10.c:120); saved pc 0x1090c
    source language c.
   (gdb) quit
06. As you move from stack frame #3 to stack frame #0, what happens to memory
    addresses in the stack?
    A. addresses are decreasing         B. addresses are increasing
A B
07. From the above information, what is the size of the activation record 
    (stack frame) for fac?
    A. 0x68 bytes      B. 0x78 bytes
A B
08. Select the true statements regarding this line in the debugger output. 

         #1  0x0001090c in fac (n=2) at hw10.c:121

    A. 0x0001090c is an address in the code section of the executable.
    B. 0x0001090c is the address of the call statement for fac.
    C. Frame #1 is in lower memory than frame #0. 
    D. 0x0001090c is an address in the data section of the executable.
A B C D
09. What is the size of the current frame?
    A. $fp - $sp            B. $sp - $fp   
A B
10. When is static chaining necessary for a function call to foobar? 
        void foobar () {
            X = 10;
        }
    A. If the language uses static scoping and X is a static variable.
    B. If the language uses static scoping and foobar is a nested function. 
    C. both statements are true 
A B C

The next question refers to the output of this GNU C program:  
int main(void) {
   int b=4;  
   {
     int b = 5;  
     printf("Where is inner b?: %p\n", &b);
   }
   printf("Where is outer b?: %p\n", &b);
   return 0;
}
Output:
Where is outer b?: ffbef994
Where is inner b?: ffbef990 
11. Based on the output, how does GNU C handle blocks?
    A. Each block has a new activation record.
    B. Each block uses the same activation record as the function uses.
A B
12. Since blocks are executed in strict textual order the variable space for
    a block can sefely overwrite the variable space of a previous block on the 
    stack.
T F
13. If this is the output of gdb on a running C program and the value of 
    identifiers in nested blocks is stored in a stack, what do you know?
       (gdb) info local
        b = 6
        b = 5
        b = 4
A. The reference to b(value 4) is nested inside the reference to b(value 5).
B. A reference to b in the current context will resolve to 6.
C. The reference to b(value 5) is nested inside the reference to b(value 4).
A B C
14. Dynamic scoping is only an issue if the language supports nested subroutines.
T F
15. The two methods of implementing dynamic scoping--deep access and shallow 
    access--support the same semantics for dynamic scoping.
T F

The next questions pertain to the use of static chaining to support nested subroutines in static-scoped languages.


16. The static link in the current stack frame points to
    A. the activation record of the static parent.
    B. the activation record of the calling program unit.
A B
17. Select the true statements concerning nesting depth in a static chain.
    A. Nesting depth is the chain_offset in the chain_offset/local_offset pair.
    B. Nesting depth refers to how many procedures a procedure is nested in.
    C. Nesting depth is 0 if a procedure is not nested.
    D. The nesting depth can be determined by the compiler. 
A B C D
18. Static semantic rules guarantee that all non-static variables that can be 
    referenced are located in an activation record instance on the stack 
    when the reference is made.
T F
 
19. What does the static chain reference (1,2) mean in the stack frame for a
    function named foo?
    A. the variable is the third declared variable in foo's static parent.
    B. the variable is the third declared variable in foo itself.
    C. the variable is the first declared variable in foo's static grandparent.
A B C

The next questions pertain to the use of the deep or shallow access methods to support nested procedures in dynamically scoped languages.


20. Select the true statement concerning the 'deep' vs. 'shallow' access method.
    A. In shallow access each variable has its own stack.
    B. In deep access the length of the dynamic chain is known at compile time.
    C. both A & B
A B C
21. In the deep access method, the dynamic chain links together all subprogram
    activation instances in the reverse order to which they were called. 
T F
22. Which of the following statements is true?
    A. Access to nonlocals is faster with the shallow access method.
    B. The upfront cost is higher with the shallow access method.
    C. both A & B
A B C
23. Given this gdb trace, what can you say about MAX?

      (gdb) p &MAX
      $2 = (int *) 0x20bb4
      (gdb) info frame
      Stack level 0, frame at 0xffbefa60:

      A. MAX is a variable in another stack frame.
      B. MAX is a static variable in the data section of the executable.
A B
24. You execute the following C code and the user types something at the prompt.

    1 void foo ( ) {
    2     printf("Enter size for stack dynamic array:");
    3     scanf ("%d",&size);
    4     int A[size];
    5     A[0] = 0; 
    6}
    The debugger trace at line 2 is:
       (gdb) p $sp
       0xffbef9d8
       (gdb) p $fp
       0xffbefa60
    The debugger trace at line 5 is:
       (gdb) p $sp
       0xffbef848
       (gdb) p $fp
       0xffbefa60

   What can you say about foo's stack frame? 
   A. The top of the stack moved down 0x190 bytes.
   B. The offset to A is known at compile time.
   C. both statements are true
A B C
25. Assume this GNU C program and debugger trace from a break point in foobar:
    int main () {
       int x = 1;
       void foo ( int i ) {
            int y = 1;
            void foobar () {
               x = 5;          
               y = 5;       
           }
           foobar();
      }
      foo(a);
      return 0;
   }
   from main's frame:        $fp = 0xffbefa60  
   from foo's frame:         $fp = 0xffbef9a8
   from foobar's frame:      $fp = 0xffbef930 

   Given the frame pointers above, the references to x and y in foobar are 
   bound to what addresses?  
   A. x is bound to 0xffbefa3c and y is bound to 0xffbef990.
   B. y is bound to 0xffbefa3c and x is bound to 0xffbef990.
A B
26. In a dynamically-scoped language, the dynamic link is used to access 
variables outside the current procedure. The dynamic link points to
    A. the activation record instance of the static parent.
    B. another dynamic link in an activation record on the runtime stack.
    C. the activation record instance of the calling program unit.
A B C

  Right: Wrong: Percent: (Must be 100% for credit)

Errors:

Name (first name only): Sleipnir Username: 3-digit ID: