Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # euclids.asm
- # A MIPS implementation of the Euclidean algorithm
- #
- .data
- larger: .word 0
- smaller: .word 0
- gcd: .word 0
- .text
- .globl main
- main:
- li $v0, 5 # Ask the user for the first number (can be smaller/larger)
- syscall
- add $t0, $zero, $v0
- li $v0, 5 # than the second number here.
- syscall
- add $t1, $zero, $v0
- bgt $t0, $t1, euclids # If $t0 is greater than $t1, euclids!
- add $t2, $zero, $t1 # The second number was larger
- add $t1, $zero, $t0 # so we'll swap them
- add $t0, $zero, $t2 # but we need another register.
- add $t2, $zero, $zero # Reset for cleanliness
- sw $t0, larger # I was going to print these numbers at the
- sw $t1, smaller # end but I'm.... lazy?
- euclids:
- div $t3, $t0, $t1 # We divide the larger by the smaller
- add $t0, $zero, $t1 # ... copy the value to $t0
- mfhi $t4 # ... store the quotient in $t4
- mflo $t5 # ... and the remainder in $t5
- add $t1, $zero, $t4 # $t1 now holds $t4
- beqz $t4, exit # This value we just calculate was zero
- sw $t4, gcd # we only save here when the value isn't zero
- j euclids # repeat Euclid
- exit:
- li $v0, 1 # Print the GCD of (larger,smaller)
- lw $a0, gcd
- syscall
- li $v0, 10 # exit()
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement