THIS IS REALLY USEFUL - http://user.it.uu.se/~justin/Teaching/NewDarkSlides/lecture5.pdf
#euclid's algorithm
#gcd (a,b) = gcd (b,c) if a mod b = c
.text
.globl main
main:
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
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, output #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
output:
li $v0, 4
la $a0, result
syscall
move $a0, $t1
li $v0, 1
syscall
li $v0, 10
syscall
.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