#euclid's algorithm
#gcd (a,b) = gcd (b,c) if a mod b = c
.text
.globl main
main:
jal get_num #goto first function
jal loop #goto second function
jal output #goto third and last function
li $v0, 10
syscall
get_num:
li $v0, 4 #prompt user for first number and store
la $a0, prompt_1
syscall
li $v0, 5 #read in the number
syscall
move $t0, $v0 #store value in t0
li $v0, 4 #prompt user for second number and store
la $a0, prompt_2
syscall
li $v0, 5 #read in the number
syscall
move $t1, $v0 #store value in t0
jr $ra #go back and execute the second function (get_num_2)
loop:
#rem $t3, $t2, $t3 ##so this causes a break to be randomly added into the execution
div $t0, $t1 #do the modulo thing
mfhi $t2
beqz $t2, back #if the remainder is 0, don't do anything else, go to output
move $t0, $t1 #make a = b
move $t1, $t2 #make b = c
j loop
back:
jr $ra #jump back to the main function
output:
li $v0, 4
la $a0, result
syscall
move $a0, $t1
li $v0, 1
syscall
jr $ra #go back to the main and execute the final command (the exit clause)
.data
prompt_1:
.asciiz "Please enter your first number: "
prompt_2:
.asciiz "Please enter your second number: "
result:
.asciiz "The gcd of the two numbers is: "
Comments