Sample Homework Assignment #7 [0 pts]


Overview

This is a sample assignment, which is exactly like all the practice problems posted in this module. Just in case you wanted more practice, here you go.

Solutions

Solutions are available as a plain ASCII file.


Exercise #1: “Draw” a stack [0 pts]

Consider the following C code fragment:

...
f(4,3,8);
...

and the following two C function definitions:

f(int x, int y, int z) {
      int a = y+x;
      int stuff = 13;
      g(a, x-z);
}

g(int a, int b) {
      int z, y;
      z = a*b;
      b = 10;
      // HERE
}

Draw a picture of the stack at the point where the program reaches the comment “// HERE”. You don’t have to show any stack content pushed before the arguments to f().

Be as complete as possible, indicating values on the stack if known (e.g., “12”) or, if unknown, the name of the parameter/variable (e.g., “x = ?”). When indicating return addresses on the stack, whenever possible include the function returned to (e.g., “Return @ to f” as opposed to “Return @”). Assume that only the EBP register is saved by all the functions, and that the C calling convention is respected. You can assume that local variables are stored on the stack in the order in which they appear in the code (not in reverse order). Finally, show the stack as growing “downwards”, as we done in class.

Hint: There should be 13 elements on the stack.

Here is an example to give you an idea of what’s expected, but which doesn’t make much sense:

a = 12
x = ?
Saved EBP
b = 4
Return @ to h
y = 12
Saved EBP
p = ?
z = 42
Return @ to foo

Exercise #2: “Draw” another stack [0 pts]

Consider the following C code fragment:

...
g(2,8)
...

and the following C function definition:

int g(int n, int count) {
        int z, y= 10 + n;

        if (count <= 3) {
             // HERE
             return -1;
        }
 
        z = g(n-2, n + count/2);
        z += (y * count - n);
        return 2*z;  
}

Same question and assumptions as for Exercise #1.

Hint: There should be 18 elements on the stack.