# CS 110, Architecture and Operating Systems Fibonacci Project Version 1.00 15 Points

Due April 5th, 2002, at 9 PM

## Introduction

Write two assembly language programs, each with different subprograms that both compute the Fibonacci function:

```    F(n) = 0                  if n = 0
F(n) = 1                  if n = 1
F(n) = F(n-1)+ F(n-2)     otherwise

F(n) = undefined          if n < 0
```

The first program/subprogram is to be recursive, following the above definition exactly. The second is to be iterative, avoiding recursion entirely by using a loop.

For each subprogram, determine the size of the stack frame and identify the purpose of each word in the frame. Note: depending on your implementation, this may be nothing more than room for the registers, etc. You can determine this information by comparing the `sp` and `fp` and using the `bt` command of gdb. Use `script` to capture the execution.

This is an exploratory exercise. You will have to go through many cycles of `gdb` as you learn how subprograms actually execute.

Do not pass parameters in the global registers.

## What to submit:

• Submit your code in the usual fashion using `cs110submit` or `cs110submitall`.

## What to turn in:

On paper, turn in:

• A printout of the stack and registers at the call and return of each subprogram (use F(3) or F(4)), with descriptions of each stack entry (if any). Note the overlap of register sets. Indicate them by using a value in a shared register that is incremented in each recursive call. Also, highlight the return addresses, stack address and frame address, add comments to show what they are and their relationship to each other.

You can use a highlighter, or just annotate the printout with pen or pencil.

## Where to turn in:

To the plastic bin outside Professor Kuenning's office.

## Notes:

• The word "subprograms" is an attempt to be generic about what is being requested.
• Another way to look at the exercise is to view it as a stack trace for recursion vs. iteration. Creating 2 programs with a subprogram in each makes it easier for us to grade.

## FAQ:

• What should the output look like?
See the sample (not annotated). This shows only the recursive run.
• For the iterative Fibonacci program/subprogram, if we're supposed to work everything into a loop, then why would we using a stack at all?
The project asks you to write a subprogram, so there's just one stack frame for its invocation. In the iterative case, that's all you'd have.
• How many Fibonacci numbers should we compute?
Again, you're asked for a subprogram that is passed a parameter specifying which Fibonacci number to return. It should work for all values. You'll write an encapsulating routine to test and demonstrate your subprograms; it's enough to compute F(3) or F(4).
• Do you want us to store variables in the subprograms in local registers, i.e. %l0, or should we load them and store them from memory?
Use registers whenever you can; it's easier.
• What are the exact commands you would like us to print and turn in for the gdb run? (If you say print the stack using bt, when do you put it in?) Also, when can you just say call subprogram(3) in gdb?
You don't call the subprogram from gdb, you write a wrapper program that makes the call, and then you watch the execution of the wrapper using gdb. It is OK to hardwire the initial values into the wrapper (`main`) program. Part of the project is to stop gdb at the right time(s) and explore the stack.
• What should we use for input?
Use constants to input the various values This is an exploratory exercise. You will have to go through many cycles of gdb as you learn how subprograms actually execute.
• How do I print things from the stack?
The easiest way to print the stack is to use the "examine" command. "`x/8lx \$sp`" will print 8 longwords starting at the address currently pointed to by the stack pointer.

We reserve the right to change the problem statement when someone demonstrates the ambiguity of said problem statement.