Starting from:

$20

Assignment 2 MIPS instructions


Programming Assignment II 
Problem Statement:
Transform the following code into MIPS instructions. Your programs should run correctly on the
QtSPIM simulator. Your submission should include a single *.s file containing the neatly
written/organized MIPS code. Important: Your program should follow the C code with the same
procedure call structure. You should use comments (‘#’ followed by text) in order to make your
programs more readable and follow the programming style suggested by the examples from the
discussion sections.
Note, you can assume the product of two input values is a 32-bit integer. In other words, the
program should work with two inputs whose product is less than or equal to the number that can be
represented by 32-bit. However, 10 % extra credit will be given if your program can handle any 32-bit
input value of ‘n1’ and ‘n2’ that may produce an LCM bigger than a 32-bit integer. Also, assume two
inputs are positive integers except 0.
Code Snippet { Greatest Common Divisor (GCD), and Least Common Multiple (LCM) }:
// Calculates the greatest common divisor
int gcd(int number1, int number2) {
int remainder = number1 % number2;
if (remainder == 0) {
return number2;
} else {
return gcd(number2, remainder);
}
}
// Calculates the least common multiple
int lcm(int number1, int number2) {
return number1*number2/gcd(number1, number2);
}
int main()
{
int n1, n2;
printf("Enter first integer n1: ");
scanf("%d", &n1);
printf("Enter second integer n2: ");
scanf("%d", &n2);
printf("The greatest common divisor of n1 and n2 is %d\n", gcd(n1, n2));
printf("The least common multiple of n1 and n2 is %d\n", lcm(n1, n2));
return 0;
}

More products