In this lab, you will write and execute a program that will calculate a specified element of the Fibonacci sequence by creating a recursive function.
The Fibonacci Sequence
The first two elements of the Fibonacci sequence are 0 then 1. The value of every following element is the sum of the preceding two elements. For example, let Fib(n) represent the nth element of the Fibonacci sequence so Fib(0) = 0, Fib(1) = 1, Fib(2) = 1, Fib(3) = 2, etc. Here is a table with a few more values:
High level code for calculating a Fibonacci value Here is some sample C++ code for a recursive procedure which calculates a specified element of the Fibonacci sequence:
int getFibonacci(int num)
Note: don't worry about error handling. You can assume that only integers greater than or equal to zero are passed into the function. Back to Top
Appendix A of the text book contains information about procedure calls in MIPS including conventions for proper use of registers and the stack. You must correctly follow these conventions. Additionally, there is an example of an implementation of the factorial function, which is also recursive. You should use this as an example and a general template. (The factorial example can also be downloaded from here: factorial.spim) Back to Top
Write a program in MIPS that contains a procedure which takes one argument - an integer greater than or equal to zero which specifies which element of the Fibonacci sequence is to be returned. The procedure must be recursive and return the correct Fibonacci value. Also, include a "main" section with at least one call to that function. Back to Top
Turn in your code by emailing it to the TA, Yuly, at ysuvorov@cs.iastate.edu. The subject line of your email should be "ComS 321 Lab 4" and you should paste the assembly code into the message body.
In this lab, you have learned about procedure calls in MIPS by writing a program containing
a recursive procedure to calculate elements of the Fibonacci sequence. This should give you
basic familiarity with procedure calling conventions including register and stack handling.
|