MIPS programming with MIPS simulator, QtSPIM or Mars, for the following two ques
ID: 3600539 • Letter: M
Question
MIPS programming with MIPS simulator, QtSPIM or Mars, for the following two questions. Submit the source code of the program. Make sure that your program is well documented. 1. Greatest Common Divisor (GCD): the greatest common divisor of two integers is the largest integer that will evenly divide both integers. The GCD algorithm involves integer division in a loop, described by the following code: int GCD (int x, int y) l x = abs(x) ; // absolute value y = abs (y) ; do t int n = x % y; // n = remainder of dividing x by y x=y; y=n; while (y > 0) return x; Write a MIPS assembly language program that does the following: Ask the user to enter two integers x and y, compute and display the GCD, then ask the user whether he wants to repeat the program. Use the divu instruction to do the unsigned division and the mfhi instruction to move the remainder of the division to a general-purpose register. You can also use the remu pseudo-instruction that will compute the remainder of unsigned divisionExplanation / Answer
.data
input_integerx: .asciiz " Please input x "
input_integery: .asciiz " Please input y "
repeat: .asciiz " Enter 0 to repeat y "
.text ## Assembly language instructions go in text segment
main: ## Start of code section
li $v0, 4 # system call code for printing string = 4
la $a0, input_integerx # load address of string asking to input x
syscall # call operating system to perform operation
li $v0, 5 # system call code for input interger = 5
syscall # call operating system to perform operation
add $t1, $v0, $zero #Copy the value of input integer stored in register $v0 to register $t1 holding x, $zero is register containing zero
li $v0, 4 # system call code for printing string = 4
la $a0, input_integery # load address of string asking to input y
syscall # call operating system to perform operation
li $v0, 5 # system call code for input interger = 5
syscall # call operating system to perform operation
add $t2, $v0, $zero #Copy the value of input integer stored in register $v0 to register $t2 holding y, $zero is register containing zero
loop1: divu $t1, $t2 #perform the division x/y
mfhi $t3 #remainder of division stored in H register will be copied to $t3
add $t1,$t2,$zero #x will be equal to y for next iteration
add $t2,$t3,$zero #y will be equal to x%y for next iteration
bgtz $t2, loop1 #brach again to loop1 for next iteration if $t2 is greater than zero
li $v0, 1 # service 1 is print integer
add $a0, $t1, $zero # load desired value into argument register $a0, which is stored in $t1 i.e. GCD of x and y
syscall # call operating system to perform operation
li $v0, 4 # system call code for printing string = 4
la $a0, repeat # load address of string asking to repeat the procedure
syscall # call operating system to perform operation
li $v0, 5 # system call code for input interger = 5
syscall # call operating system to perform operation
beq $v0,$zero, main #Jump again to the begining of program if user input 0 which means he want to repeat
li $v0, 10 # system call code for exit = 10
syscall # call operating system to perform operation
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.