Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Greatest Common Divisor (GCD): the greatest common divisor of two integers is

ID: 3602404 • Letter: 1

Question

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) 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 division.

Explanation / Answer

#Define text section

.text

        #Define main

     .globl main

  

#Define main method

main:

        #Push to stack

     sub $sp,$sp,12

  

     #Store return address

     sw   $ra,4($sp)

       #String output

     li   $v0,4    

  

     #Load address

     la   $a0,pow

  

     #Display string

     syscall    

  

     #Integer input

     li $v0,5     

  

     #system call

     syscall      

  

     #Move data into register

     move $t2,$v0

       

     #Load value

     li   $v0,4   

  

     #load address

     la   $a0,bas  

  

     #Display string

     syscall      

  

     #Load

     li $v0,5     

  

     #System call

     syscall      

  

     #Move value to register

     move $a0,$t2

  

     #Move value to register

     move $a1,$v0

  

     #Jump

     jal euc

  

     #Store

     sw   $v0,8($sp)

     #Load

     li   $v0,4

  

     #Load value

     la   $a0,str

  

     #System call

     syscall

       #Load value

     li   $v0,1

  

     #Load value

     lw   $a0,8($sp)

  

     #System call

     syscall

  

     #Restore address

     lw   $ra,4($sp)

  

     #Pop stack

     add $sp,$sp,12

  

     #Jump

     jr   $ra

        #Define data

     .data

  

#Define label

str:      

        #Display message        

     .asciiz "value = "

  

#Define label   

bas:       

           #Display message       

     .asciiz "second integer = "

  

#Define label

pow:      

        #Display message        

     .asciiz "first integer = "

  

     #Define text

     .text

#Define label

euc:

       #Push stack

     sub $sp,$sp,8

  

     #Store address

     sw   $ra,4($sp)

       #Exit if b is not 0

     bne $a1, $zero, L1

  

     #Return a0

     add $v0,$zero,$a0

     #Pop stack

     add $sp,$sp,8

     #Return

     jr   $ra

#Define L1

L1:

       #Set c to b

     move $t4,$a1       

     #Compute remainder

     rem $a1,$a0,$a1    

     #Set a

     move $a0,$t4

  

     #Call recursively      

     jal euc         

        #Load

     lw   $ra,4($sp)

     #Move value to $v0

     move $v0,$a0

        #Pop stack

     add $sp,$sp,

     #Return

     jr   $ra