Programming Assignment 5

Due Thursday, November 15
15 points


Introduction

For this assignment you will write a MAL subprogram for computing the greatest common divisor of two integers. Also, you will write a short main program that will be used in conjunction with xspim and spim to test the subprogram.

The subprogram gcd

The gcd subprogram should mimic the following C function:

/** gcd(n1, n2) returns the greatest common divisor of n1 and n2.
 */
int gcd(int n1, int n2) {
    if (n2 == 0) {
	return n1;
    }
    return gcd(n2, n1%n2);
}
    

You must implement this function recursively to get full credit. See MIPS Subprograms for instructions on writing recursive subprograms. If you take care with planning the use of registers the code won't be too difficult.

The main program

The main program should prompt for two integers, print their greatest common divisor, and repeat until the user enters a negative number for one of the integers.

Caution

Do not use "m" or "M" as names of memory variables. This will result in unpredictable behavior, especially with regard to input.

Commenting

You may lose points if your code is not well commented. Your gcd function comments should clearly document its calling convention and its use of registers. See Assembly Language Comments for suggestions on writing comments.

You will not get debugging help from the TA or myself if you do not have good comments. I recommend writing the function comments before writing the code. You will likely make fewer mistakes and have an easier time debugging if you do.

What to hand in

Turn in a typescript showing the contents of your MAL code file along with a spim session that shows the execution of your testing code. In this session, you should enter the following pairs of integers: