CS 2734 Computer Organization II


Laboratory 6 [Oct 4, 6; Oct 11] Implementing a recursive function in MIPS (raise to a power)

This laboratory asks you to implement a different 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 should use the standard input parameters $a0, $a1 as well as $v0 for the returned value. Because these are recursive calls, you must save and store registers on the stack. 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.

You can simply select and copy the C code for your program above, and then compile and run it to see what the output should look like.

For checkoff:


Revision Date: 10/1/99