MIPS Leaf Functions
For this assignment you will write a program in MIPS assembly that contains a leaf function which determins if one integer is divisible by another.
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. For now, just use different temporary registers in each section. Once we see how to use the stack we can do it the proper way using saved registers, but for now you do not need to use the stack for anything.
Submission
Write your program in the provided file is_divisible.s
and push your submission to git-keeper.