MIPS Functions

For this assignment you will write two programs in MIPS assembly. One makes use of a leaf function, and the other makes use of a non-leaf function. In the non-leaf function you will need to store items on the stack.

Program 1 (leaf function): is_divisible.s

Write your program in the provided file is_divisible.s.

Program Behavior

Your program must prompt the user to enter the dividend and the divisor, and then print whether or not the dividend is divisible by the divisor. Here are two example runs:

Enter the dividend: 10
Enter the divisor: 5
10 is divisible by 5
Enter the dividend: 10
Enter the divisor: 6
10 is not divisible by 6

Structure

You could write this program without using any functions, but part of the point of the assignment is to see how to write a simple leaf function.

Label your function is_divisible or something equally readable. The function must take two arguments, the dividend and the divisor, which are passed using the registers $a0 and $a1. The function must return 1 if the dividend is divisible by the divisor, and 0 if not. The return value is stored in the register $v0.

The instruction for division in MIPS is div. Like with multiplication, division takes only 2 operands, and the result is stored in the HI and LO registers. We are interested in the HI register, which stores the remainder. If the remainder is 0, your function should return 1. Otherwise, your function should return 0.

Implementing conditionals requires branching. If it is a simple if/else style conditional, you can branch if some condition is true, and for the else execution can simply continue without branching.

The main portion of your program and the is_divisible function will both need to use registers. Ideally we would use the stack, but for this first program, you can just use different temporary registers in each section. In the second program (fibonacci) you’ll practice using the stack.

Program 2 (non-leaf function): fibonacci.s

Write your program in the provided file fibonacci.s.

C Program

The program you write will have the same functionality as the program contained in fibonacci.c, which came with the assignment. It computes the nth fibonnaci number recursively. Do not convert it to an iterative solution, as the purpose of this assignment is to practice with recursive functions. Write your MIPS program in fibonacci.s.

Structure

In the main portion of your MIPS program, ask the user for input with a system call and put the value of n in $a0. Call the function fib which takes one argument. Take the output of the function and print it to the shell.

Label your function fib. The function must take one argument, which is passed using the register $a0. You can create labels for the base cases if n is 0 or 1. Otherwise, you will use the stack to make two recursive calls for smaller input values. Store the result of the first recursive call for the (n-1) fibonacci number in $s0, and the result of the second recursive call for the (n-2) fibonacci number in $s1.

Using the Stack

In the factorial.s program (which you can find under resources), $ra and $a0 are stored on the stack in the factorial function. In fib we also need to store $a0 and $ra. Addditionally, we will want to store $s0 and $s1 – these registers will be used to obtain the output of the recursive call for the (n-1) fibonacci number and (n-2) fibonacci number, respectively.

After fib does its work, it will restore the old values of $ra, $a0, $s0, and $s1 from the stack.

Example Output

Here is what a couple runs of your program should look like:

Enter n: 8
The nth fibonacci number is: 21
Enter n: 11
The nth fibonacci number is: 89

Submission

Push both your programs back to git-keeper. Note that for full credit, your fibonacci.s program must have the same behavior as the program in fibonacci.c and make use of the stack to save and restore used registers.