Recitation 7 must be submitted
following directions at: submissions on or before
|
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.
Contents of submission
for Recitation 7: Last Name, First Name; Course Number; Recitation Number (7).
|