The greatest common divisor (GCD) of two integers is the largest integer that wi
ID: 3712466 • Letter: T
Question
The greatest common divisor (GCD) 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 pseudocode:
int GCD (int x, int y){
x = abs(x) // absolute value
y = abs(y)
do{
int n = x % y
x = y
y = n
}while (y > 0)
return x
}
Implement this function in assembly language. Then go online find its recursive version and implement it in assembly language too. Then write a test program that calls your two GCD procedure five times each, using the following pairs of integers (5, 20), (24, 18)_, (11, 7), (432, 226), and (26, 13). After each procedure call, display the GCD
CArvine Examples Project32 VS2015 Project32 VS20151DebugvProject.exe -x eatest common divisor of (5.20) calculated by loop is: 5 alculated by recursion is: 5 Greatest common divioor of (24.18) calculated by loop is: 6 calculated by recuroion io: 6 Greatest common divioor of (11.7) calculated by loop is:1 alculated by recuroion is: 1 Greatest common divisor of (438.226) calculated by loop is: 2 alculated by recursion is: 2 Greatest common divioor of (26,13) calculated by loop is: 13 caleulated by recuroion is: 13 Press any key to continueExplanation / Answer
Mips assembly code:
.text
#global
.globl main
#main
main:
sub $sp,$sp,12
#store address
sw $ra,4($sp)
#load instruction
li $v0,4
la $a0,pval
#sys call
syscall
#load v0 valu
li $v0,5
syscall
#Move
move $t2,$v0
li $v0,4
la $a0,integer
syscall
#load
li $v0,5
#sys call
syscall
#move data
move $a0,$t2
move $a1,$v0
jal exec
sw $v0,8($sp)
li $v0,4
la $a0,stri
syscall
#Load value
li $v0,1
#Load value
lw $a0,8($sp)
syscall
lw $ra,4($sp)
add $sp,$sp,12
#Jump
jr $ra
.data
stri:
#Display message
.asciiz "value = "
integer:
#enter the integer value
.asciiz "second value = "
#Define label
pval:
#enter first integer value
.asciiz "first value= "
.text
exec:
sub $sp,$sp,8
#Store
sw $ra,4($sp)
#Exit if b is not 0
bne $a1, $zero, Loop1
add $v0,$zero,$a0
add $sp,$sp,8
#Return
jr $ra
Loop1:
move $t4,$a1
rem $a1,$a0,$a1
#Set a
move $a0,$t4
jal exec
lw $ra,4($sp)
move $v0,$a0
add $sp,$sp,8
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.