



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




In lecture, we have talked about how to write functions. Unlike in C, calling a function in MIPS requires more than just invoking it. Before calling a function, the function making the call (known as the caller ) needs to save values of registers that the function may use and overwrite (these are known as caller-saved registers). The called function (known as the callee) also needs to save values of some registers, which are appropriately named callee-saved registers.
Following the register-saving convention becomes even more critical when using recursive func- tions, which is the topic of this discussion.
A recursive function is a function that calls itself. To ensure that the self-calling eventually terminates, there are two parts to every recursive function, the base case and the recursive case. The base case defines the condition that terminates the recursion. The recursive case calls the function recursively, but with different input(s) that come progressively closer to the base case condition. Reaching the base case results in termination of the recursion and unwinding of the stack of recursive calls.
Let’s write a recursive function that does simple math. Given two integers n and k, where n ≤ k, find the sum of integers from n to k, inclusive (e.g. if n = 2 and k = 4, our program will add 2 + 3 + 4).
First, let’s try to write this function in our favorite C-like language. There are two inputs to this function, so the signature is:
int mySum(int n, int k);
The function proceeds to count numbers from n up to k by incrementing n by 1 each time. The recursive case performs the addition using a recursive call:
return n + mySum (n + 1, k);
The base case of the algorithm occurs when n = k. Note that we’re assuming that n ≤ k in the beginning.
if(n == k) return n;
So, here’s the complete function:
int mySum (int n, int k) { if (n == k) return n; return n + mySum (n + 1, k); }
It is short and elegant, which is one of the appeals of recursive functions.
Now let’s write the MIPS code, doing tiny steps, eventually arriving at the correct solution.
mySum: bne $a0, $a1, recurse move $v0, $a jr $ra
Note that, since the base case does not have any function calls, it is not necessary to save/restore any values to the stack. The recursive case (line 4 of C code) is more complex and requires multiple steps:
recurse: sub $sp, $sp, 12 # allocate stack space: 3 values * 4 bytes each sw $ra, 0($sp) sw $a0, 4($sp) sw $a1, 8($sp) # add this just for completeness
addi $a0, $a0, 1 jal mySum
lw $a0, 4($sp) add $v0, $v0, $a
In this problem, you will convert MIPS code into a high-level language like C.
loop: ... lw $t0, 0($a0) add $a0, $a0, 4 ... j loop
should be translated as:
while (...) { /* or, for(...) / ... t0 = a0[i]; / index array element / i++; / increment index */ ... }
and must not be translated as:
while (...) { /* or, for(...) */ ... t0 = a0; / dereference the pointer / a0 = a0 + 4; / increment the pointer */ ... }