Computer
Organization and Design
(Textbook. Click for information.)

 CS 2731, Spring 2004
 Computer Organization II

 Recitation 7: Feb 23, 25, Mar 1, 3
 Implementing a recursive function
 in MIPS
(raise to a power)
    MW   02:00-02:50 pm, SB 3.01.04
 Due: 2004-03-10 23:59:59

Recitation 7 must be submitted following directions at: submissions on or before
  • 2004-03-10  23:59:59 (that's Wednesday, 10 March 2004, 11:59:59 pm) for full credit.
  • 2004-03-14  23:59:59 (that's Sunday, 14 March 2004, 11:59:59 pm) for 75% credit.

Outline: This laboratory asks you to implement a recursive function in the MIPS assembler language.

Here is that program in C:

Fourth Program (C) C Run and Output
#include <stdio.h>
int Power(int , int );

void main() {
   int b, e, r; /* base, exp, result */
   printf("Enter b << ");
   scanf("%d", &b);
   printf("Enter e << ");
   scanf("%d", &e); 
   r = Power(b, e);
   printf("Power (b^e) = %d\n", r);
   return;
}

int Power(int b, int e) {
   int r;
   printf("Start of Power: e = %d\n", e);
   if (e == 1) return b;
   r = b * Power(b, e-1);
   printf("End of Power: result = %d\n", r);
   return r;
}
Enter b: 3
Enter e: 5
Start of Power: e = 5
Start of Power: e = 4
Start of Power: e = 3
Start of Power: e = 2
Start of Power: e = 1
End of Power: result = 9
End of Power: result = 27
End of Power: result = 81
End of Power: result = 243
Power (b^e) = 243

In implementing this program in MIPS, you can either use the standard input parameters $a0, $a1 as well as $v0 for the returned value, or you can follow the form of the factorial function passed out in class (the one that used the stack more extensively). Because these are recursive calls, you must save and store registers on the stack, including the return address $ra. In particular, after you have made a recursive call, if you have changed some $ai for the call, then you need to load it again from the stack if you want to use it again.

Your MIPS program should have all the output shown, including the temporary output at the beginning and at the end of the call to the recursive function.


What you should submit: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Recitation Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate number 1-2.

  Contents of submission for Recitation 7:

Last Name, First Name; Course Number; Recitation Number (7).

  1. MIPS source code for the program.

  2. Log of runs of the program, using spim and the input data shown above for the separate runs, namely, 3 followed by 5.


Revision date: 2004-02-10. (Please use ISO 8601, the International Standard.)