Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. # euclids.asm
  2. # A MIPS implementation of the Euclidean algorithm
  3. #
  4.  
  5. .data
  6. larger: .word 0
  7. smaller: .word 0
  8. gcd: .word 0
  9.  
  10. .text
  11. .globl main
  12. main:
  13. li $v0, 5 # Ask the user for the first number (can be smaller/larger)
  14. syscall
  15.  
  16. add $t0, $zero, $v0
  17.  
  18. li $v0, 5 # than the second number here.
  19. syscall
  20.  
  21. add $t1, $zero, $v0
  22. bgt $t0, $t1, euclids # If $t0 is greater than $t1, euclids!
  23.  
  24. add $t2, $zero, $t1 # The second number was larger
  25. add $t1, $zero, $t0 # so we'll swap them
  26. add $t0, $zero, $t2 # but we need another register.
  27.  
  28. add $t2, $zero, $zero # Reset for cleanliness
  29.  
  30. sw $t0, larger # I was going to print these numbers at the
  31. sw $t1, smaller # end but I'm.... lazy?
  32.  
  33. euclids:
  34. div $t3, $t0, $t1 # We divide the larger by the smaller
  35. add $t0, $zero, $t1 # ... copy the value to $t0
  36. mfhi $t4 # ... store the quotient in $t4
  37. mflo $t5 # ... and the remainder in $t5
  38.  
  39. add $t1, $zero, $t4 # $t1 now holds $t4
  40.  
  41. beqz $t4, exit # This value we just calculate was zero
  42. sw $t4, gcd # we only save here when the value isn't zero
  43.  
  44. j euclids # repeat Euclid
  45.  
  46. exit:
  47. li $v0, 1 # Print the GCD of (larger,smaller)
  48. lw $a0, gcd
  49. syscall
  50.  
  51. li $v0, 10 # exit()
  52. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement