CS 2734 Computer Organization II -- Fall 2001
Laboratory 7 [Oct 8, 10, 15, 17]:
Implementing a recursive function in MIPS
(raise to a power)

Notice: Due 5pm, Wednesday, 24 October 2001.
(We are delaying this lab for one week.)
Each lab is due with the lab instructor
no later than 5 pm on the Wed after the lab.

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

Here is that program in C:

     #include <stdio.h>

     int Power(int , int );

     void main()
     {  int b, e, r; /* base, exponent, 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;
     }

Along with sample output:

     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 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.

For checkoff:


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