Silver_Smoulder

[ASM] Convert to Function Euclid

Oct 30th, 2019
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. THIS IS REALLY USEFUL - http://user.it.uu.se/~justin/Teaching/NewDarkSlides/lecture5.pdf
  2.  
  3. #euclid's algorithm
  4.  
  5. #gcd (a,b) = gcd (b,c) if a mod b = c
  6.  
  7. .text
  8. .globl main
  9.  
  10. main:
  11. li $v0, 4 #prompt user for first number and store
  12. la $a0, prompt_1
  13. syscall
  14.  
  15. li $v0, 5 #read in the number
  16. syscall
  17.  
  18. move $t0, $v0 #store value in t0
  19.  
  20. li $v0, 4 #prompt user for second number and store
  21. la $a0, prompt_2
  22. syscall
  23.  
  24. li $v0, 5 #read in the number
  25. syscall
  26.  
  27. move $t1, $v0 #store value in t0
  28.  
  29. loop:
  30. #rem $t3, $t2, $t3 ##so this causes a break to be randomly added into the execution
  31. div $t0, $t1 #do the modulo thing
  32. mfhi $t2
  33. beqz $t2, output #if the remainder is 0, don't do anything else, go to output
  34.  
  35. move $t0, $t1 #make a = b
  36. move $t1, $t2 #make b = c
  37.  
  38. j loop
  39.  
  40. output:
  41. li $v0, 4
  42. la $a0, result
  43. syscall
  44.  
  45. move $a0, $t1
  46. li $v0, 1
  47. syscall
  48.  
  49. li $v0, 10
  50. syscall
  51.  
  52. .data
  53.  
  54. prompt_1:
  55. .asciiz "Please enter your first number: "
  56.  
  57. prompt_2:
  58. .asciiz "Please enter your second number: "
  59.  
  60. result:
  61. .asciiz "The gcd of the two numbers is: "
Advertisement
Add Comment
Please, Sign In to add comment