CS360 Midterm -- March 14, 2002. Answer/Grading for Question 2


Answer

This is a grungy piece of code, but there are only two asm statements. The first one stores the value of the frame pointer into ptr (because ptr is the variable stored at [fp]). Then the frame pointer it stored into the global variable hackvar.

The second asm statement restores the frame pointer from ptr.

So, think about the flow of control of the program. When fp() is called, the frame pointer is stored into the global variable hackvar. Then fprec() is called with the same arguments as fp().

Now, fprec() is the exact same as fp() in Question 1, with two major differences:

  1. When *ptr == to, the frame pointer is restored from hackvar and 1 is returned. This means that instead of returning 1 to its recursive callers, fprec() returns 1 directly to the caller of fp(). This is more efficient than simply returning 1 to the caller, that keeps returning 1 down to the original caller of fp().

    It's a horrible hack, but it's kind of cool, no?

  2. When fprec() is called recursively, we don't test its return value. Why? Because it is always zero. Fprec() cannot return 1, because when it would originally return 1, it does that frame-pointer-restoring trick, and doesn't return at all!

If you write code like this in this or any other class, you will deserve the punishment that is meted out to you!!!!!


Grading - 7 points

Breakdown as follows: