COMP 144 Programming Language Concepts
Spring 2003


Assignment 7: Calling Methods and Run-time Stack

Calling methods

1. (problem 8.7, p. 487) 
   Write a procedure or function (in the language of your choice) 
   that will have 4 different effects, depending on whether arguments 
   are passed by-value, by-reference, by-value-result, or by-name.

2. (problem 8.2, p. 486)
   Using your favorite language and compiler, write a program that 
   will determine the order in which subroutine/function arguments 
   are evaluated.

Run-time Stack


3. Consider the following code fragment.  The language uses lexical
   scoping (static scoping).  We have call-by-value parameters and
   call-by-reference parameters, indicated with byval and byref in 
   the parameter list of the procedure declarations.

      int g1 = 8;
      real g2 = 0;
      bool g3 = false;

      proc first (byref x: int, byval y: real) {
         real ff = 2.2;
   
         y = ff * (cast real) second(y);
      }

      proc second (byref z: int) {
         int k = 5;

         if (z < 2) {
            z ++;
            second(z);
         } else {        
            g3 = true;  /* point A */
         }
         return z*k;
      }

      main {
         first(g1,g2);
      }


   a) Draw in detail a stack frame (activiation record) for procedure
      second.  Label its contents (i.e., show the static chain pointer, 
      dynamic chain pointer, arguments, return value, return address, 
      local variable, etc.).

   b) Draw the runtime stack and show its contents at point "A" in 
      execution.  At this point what has happened is "first" has been
      called from "main", then "second" was called from "first", then
      "second" was recursively called twice until it is called with an
      argument having value greater than 2, and this triggers the "else"
      part of the body of the "if" in "second".  It is this point in
      execution that I want to see the stack.

      Your stack will show which procedures/functions have been
      called, in what order, and what the different lexical nestings are
      (via the static chains), as well as local variable values, global
      variable values, and also showing pointers for variable passed 
      by-reference in the stack frames.