#
# gcd procedure;it finds the greatest common divisor of two nonnegative integer 
# 
   
gcd :      addi $sp, $sp, -4     # create a 4-word-long stack frame
           sw   $ra, 0($sp)      # save the return address
           beqz $a1, exit_gdc    # if $a1=0 go to exit_gdc 
           div  $a0, $a1         # Lo=$a0/$a1 ;Hi=$a0 mod $a1
           mfhi $t1              # $t1=Hi
           move $a0,$a1          # $a0=$a1
           move $a1,$t1          # $a1=$t1
           jal  gcd              # go to gcd

exit_gcd:  move $v0, $a0         # $v0=$a0
           lw   $ra, 0($sp)      # restore the return address 
           addi $sp, $sp, 4      # adjust stack pointer
           jr   $ra              #

#
# end gcd procedure
#

         .data
str1:    .asciiz "Insert two nonnegative integers, one per line: \n"
str2:    .asciiz "The gcd of "
str3:    .asciiz " and "
str4:    .asciiz " is: "
str5:    .asciiz "\n"        

         .text

main:    addi $sp, $sp, -4     # create a stack frame 
         sw   $ra, 0($sp)      # save the return address

again:   la   $a0, str1        #
         li   $v0, 4           # print of str1 
         syscall               #
        
         li   $v0, 5           # get the first number
         syscall               # and put it in $v0
         move $s0, $v0         # $s0=$v0
         bltz $s0, again       # if $s0<=0 go to again     
        
         li   $v0, 5           # get the second number
         syscall               # and put it in $v0
         move $s1, $v0         # $s1=$v0
         bltz $s1, again       # if $s1<=0 go to again
         
         move $a0, $s0         # $a0=$s0
         move $a1, $s1         # $a1=$s1
         jal  gcd              # go to gcd
         
         move $t0, $v0         # $t0=$v0
         
         la   $a0, str2        #  
         li   $v0, 4           # print of str2
         syscall               #
        
         li   $v0, 1           #
         move $a0, $s0         # $a0=$s0
         syscall               # print the first number
        
         la   $a0, str3        #
         li   $v0, 4           # print of str3
         syscall               #
        
         li   $v0, 1           #
         move $a0, $s1         # $a0=$s1 
         syscall               # print of the second number
         
         la   $a0, str4        #
         li   $v0, 4           # print of str4
         syscall               #
         
         li   $v0, 1           # 
         move $a0, $t0         # $a0=$t0
         syscall               # print of the gcd
         
         la   $a0, str5        #
         li   $v0, 4           # print of str5
         syscall               #
         
         lw   $ra, 0($sp)      # restore the return address 
         addi $sp, $sp, 4      # eliminate the stack frame
         jr   $ra              # 

#
# end main
#                   
