Programming Project #2
Assigned 10/12/95
Due Tuesday, 10/24/95 at 11:59pm
Introduction
The main purpose of this project is to demonstrate how to allocate space for local variables on the stack. In particular, you are to implement a recursive subroutine to compute Fibonnacci numbers. This is a classic example of used to illustrate recursive subroutines and one that is relatively simple.
Fibonnacci Numbers
The Fibonnacci numbers are those that follow the sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... where each number, except the first and second, is the sum of the previous two numbers. This can be expressed by the following expressions:
fib(1) = 1
fib(2) = 1
fib(n) = fib(n-1) + fib(n-2) for n > 2
Assignment int fib(int n)
Write a function fib in MIPS assembly language that returns the nth Fibonnacci number. Your function should take one integer argument n, passed in register $a0, and return the nth Fibonnacci number in register $v0.
Fib should be called recursively. That is, to get the previous Fibonnacci number you should call fib to get the (n-1)th and (n-2)th Fibonnacci numbers, and then add these values. You should not use any global variables in your function, and each recursive call of fib should allocate any local space it might need on the stack. You must also be careful that each time fib is called, you save all registers whose value must be maintained across subroutine calls.
As in Project #1, you will need to also write a main routine that gets an integer n from the terminal, calls fib to compute the nth Fibonnacci number and then prints out the result on the terminal. You should be able to easily modify your main program from Project #1 to do this.
Put this problem in its own file named fib.s in the directory elec320/lab2 in your home directory. Also, to prove that your function works correctly, execute your function with the numbers 1, 2, 5, 10, and 30, and place the number and the computed Fibonnacci numbers in a file called fib.out. Make sure you comment this problem in the same manner specified for Project #1.
Your function must be written to work under XSPIM on the owlnet SUN Sparc computers. We will check them for correctness in that environment. You may develop your program on other platforms, but in that case, you should test it on a SUN before turning it in for grading.