CS360: Exam 1: 10/22/97. Answer to Question 3

Here's a description of a(). First, it pushes 4, which means that there's one local variable. Next, it pushes the value 1 on the stack, and then it pushes the value of the fp on the stack. Note it pushes the value of the fp, and not the contents of that stack at the fp. This is the address of the local variable. Then it calls c() and pops the two paramters off the stack.

Next, it pushes the local variable onto the stack and calls b(). Finally, it loads its first parameter into r1 and adds it to the return value of b(). It returns this.

Therefore, a() looks as follows. Note, I can name the local variable and the parameter anything. Moreover, the local variable can be any 4-byte type (i.e. (char *) or float):

int a(int i)
{
  int j;

  c(&j, 1);
  return b(j)+i;
}

Here's a description of main(). First, it pushes 8 on the stack, meaning that there are two local variables. Let's call them i and j, and assume that i is at fp, and j is at fp-4.

First, it sets i to 10. Next, it performs (i+3) and pushes the result on the stack. Then it calls a(), and adds five to the return value. The result is then stored in j. Finally, j is returned.

Therefore, main() looks as follows:

main()
{
  int i, j;

  i = 10;

  j = a(i+3)+5;
  return j;
}