Advertisement
Ollii

grsgf

Dec 19th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. .data
  2.  
  3. asknumber1: .asciiz "Please insert the first number: "
  4. asknumber2: .asciiz "Please insert the second number: "
  5.  
  6. .text
  7.  
  8. main:
  9. la $a0, asknumber1 # load this variable to be able to print the string
  10. li $v0, 4
  11. syscall # syscall code 4 to print a string
  12.  
  13. li $v0, 5
  14. syscall # syscall code 5 to read an integer
  15.  
  16. move $s0, $v0 # store a copy of $v0 into $s0
  17.  
  18. la $a0, asknumber2 # load this variable to be able to print the string
  19. li $v0, 4
  20. syscall # syscall code 4 to print a string
  21.  
  22. li $v0, 5
  23. syscall # syscall code 5 to read an integer
  24.  
  25. move $s1, $v0 # store the argument in $s1
  26. move $v0, $s0 # restore $v0 to $s0
  27.  
  28. jal gcd # jump and link to the gcd procedure
  29.  
  30. remainder:
  31. move $t0, $s0 # store the argument into $t0
  32. subu $t0, $t0, $s1 # prediction whether the subtraction will go sub-zero
  33. blt $t0, 0, finish_remainder # while a is not below 0
  34. subu $v0, $v0, $s1 # subtract a with b
  35. move $s0, $v0 # update $s0
  36. j remainder # repeat the loop
  37.  
  38. finish_remainder:
  39. move $s0, $s1 # y becomes x
  40. move $s1, $v0 # store the result in y , now: (y, remainder(x,y))
  41. j gcd
  42.  
  43. gcd:
  44. addi, $sp, $sp, -4 # allocate the stack pointer
  45. sw $ra, 0($sp) # store the return address
  46. beq $s1, 0, basecase # base case
  47. blt $s0, $s1, finish_gcd # in case x >= y, continue loop
  48. jal remainder # jump and link to the remainder procedure
  49.  
  50. jal gcd # jump and link to gcd (recursive)
  51.  
  52. basecase:
  53. move $v0, $s0 # keep the result in $v0
  54.  
  55. finish_gcd:
  56. move $a0, $v0 # store the result (x), in $a0 for printing
  57. li $v0, 1
  58. syscall # syscall code 1 to print an integer
  59.  
  60. lw $ra, 0($sp) # load the return address
  61.  
  62. exit:
  63. li $v0, 10
  64. syscall # syscall code 10 for exit
  65. # end of oef2.asm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement