Silver_Smoulder

[ASM] Euclid Algo with Functions

Oct 31st, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #euclid's algorithm
  2.  
  3. #gcd (a,b) = gcd (b,c) if a mod b = c
  4.  
  5. .text
  6. .globl main
  7.  
  8. main:
  9. jal get_num #goto first function
  10. jal loop #goto second function
  11. jal output #goto third and last function
  12.  
  13. li $v0, 10
  14. syscall
  15.  
  16. get_num:
  17.  
  18. li $v0, 4 #prompt user for first number and store
  19. la $a0, prompt_1
  20. syscall
  21.  
  22. li $v0, 5 #read in the number
  23. syscall
  24.  
  25. move $t0, $v0 #store value in t0
  26.  
  27. li $v0, 4 #prompt user for second number and store
  28. la $a0, prompt_2
  29. syscall
  30.  
  31. li $v0, 5 #read in the number
  32. syscall
  33.  
  34. move $t1, $v0 #store value in t0
  35.  
  36. jr $ra #go back and execute the second function (get_num_2)
  37.  
  38. loop:
  39. #rem $t3, $t2, $t3 ##so this causes a break to be randomly added into the execution
  40. div $t0, $t1 #do the modulo thing
  41. mfhi $t2
  42.  
  43. beqz $t2, back #if the remainder is 0, don't do anything else, go to output
  44.  
  45. move $t0, $t1 #make a = b
  46. move $t1, $t2 #make b = c
  47.  
  48. j loop
  49.  
  50. back:
  51. jr $ra #jump back to the main function
  52.  
  53. output:
  54. li $v0, 4
  55. la $a0, result
  56. syscall
  57.  
  58. move $a0, $t1
  59. li $v0, 1
  60. syscall
  61.  
  62. jr $ra #go back to the main and execute the final command (the exit clause)
  63.  
  64. .data
  65.  
  66. prompt_1:
  67. .asciiz "Please enter your first number: "
  68.  
  69. prompt_2:
  70. .asciiz "Please enter your second number: "
  71.  
  72. result:
  73. .asciiz "The gcd of the two numbers is: "
Advertisement
Add Comment
Please, Sign In to add comment