Lab 2 - MIPS Function Calls

Due: Noon on Wednesday

The purpose of this lab is to write an assembly program that implements a simple function call. I have provided an example program, fact.s, that is based on the example printed in Appendix A of the book. You can copy these over to Sleipnir using either cut and paste or the wget command. To use the wget command, issue the following in the directory where you wish to store the files:

wget http://www.cs.csub.edu/~mdanfor/cs321-w09/code/fact/Makefile
wget http://www.cs.csub.edu/~mdanfor/cs321-w09/code/fact/fact.s
If the directory you are using is not immediately underneath your cs321 directory, edit the Makefile so that the include and SDETOP lines both point to the appropriate files.

The factorial assembly program is equivalent to the following C program:

int main() 
{
  printf("The factorial of 10 is %d\n", fact(10));
}
int fact(int n) 
{
  if(n < 1) return 1;
  else return n * fact(n-1);
}

Assignment

Create a program called fib.s that is equivalent to the following C program:
int main() 
{
  printf("The 5th Fibonacci number is %d\n", fib(5));
}
int fib(int n)
{
  if(n == 0) return 0;
  if(n == 1) return 1;
  return fib(n-1) + fib(n-2);
}
Use fact.s as a template for achieving this. You will need to make two recursive function calls instead of just one. Make sure to save the result of the first function call before making the second function call. You may find it helpful to draw out the stack to see which stack offsets are not being used so that you can save the results of the first function call there. You will also need two if-statement branches since there are two stopping conditions.