Silver_Smoulder

[ASM][Func]Project 5 Pythagorean Theorem

May 26th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #do the pythagorean theorem using the stack and functions calling functions
  2.  
  3. .data
  4.  
  5. prompt_A:
  6. .asciiz "Please enter side A: "
  7.  
  8. prompt_B:
  9. .asciiz "\n Please enter side B: "
  10.  
  11. prompt_C:
  12. .asciiz "\n Please enter side C: "
  13.  
  14. comma:
  15. .asciiz ", "
  16.  
  17. yes_right:
  18. .asciiz " - is a right triangle."
  19.  
  20. no_right:
  21. .asciiz " - is not a right triangle."
  22.  
  23. infinite_loop:
  24. .asciiz "\n \n THERE IS AN INFINITE LOOP!!! YOU SCREWED UP!!! \n\n"
  25.  
  26.  
  27. .globl main
  28. .text
  29.  
  30. #the outline is as follows:
  31. #1) Go to main and activate allwork.
  32. #2) Store the address of allwork on the stack, then jump to input_numbers
  33. #3) Store the address of input_numbers, prompt user for input, jump to evaluate
  34. #4) Store the address of evaluate, and then test to see which of the numbers is the hypotenuse, jump to power
  35. #5) Store the address of power, and square all of the values, jump to calculate
  36. #6) Store the address of calculate, subtract the sum of the two non-hypotenuse squares (the legs), if they're zero, jump to yes, else no.
  37. #7a) Store address of yes, output that it is a right angle, jump to exit
  38. #7b) Store address of no, output that it is not a right angle, jump to exit.
  39. #8a) Clear the addresses from the stack, exit the program.
  40.  
  41. main:
  42. jal allwork
  43. li $v0,10
  44. syscall
  45.  
  46. allwork:
  47. addi $sp, $sp, -4 #position -4
  48. sw $ra, ($sp)
  49. jal input_numbers
  50.  
  51. input_numbers:
  52. addi $sp, $sp, -4 #position -8
  53. sw $ra, ($sp)
  54.  
  55. li $v0, 4 #store 1st number
  56. la $a0, prompt_A
  57. syscall
  58.  
  59. li $v0, 5 #prep for user input
  60. syscall
  61. move $s0, $v0
  62.  
  63. li $v0, 4 #store 2nd number
  64. la $a0, prompt_B
  65. syscall
  66.  
  67. li $v0, 5 #prep for user input
  68. syscall
  69. move $s1, $v0
  70.  
  71. li $v0, 4 #store 3rd number
  72. la $a0, prompt_C
  73. syscall
  74.  
  75. li $v0, 5 #prep for user input
  76. syscall
  77. move $s2, $v0
  78.  
  79. jal evaluate
  80.  
  81. evaluate:
  82. #we are going to compare three numbers and see which is the largest
  83. #we will compare s0 to s1, then s0 to s2, then s1 to s2, and place the greatest number into s4, and the other two into s5 and s6
  84.  
  85. #if any of the numbers are equal to each other, we can hard_exit, since they cannot be a right triangle
  86. beq $s0, $s1, hard_exit
  87. beq $s1, $s2, hard_exit
  88.  
  89. addi $sp, $sp, -4 #position -12
  90. sw $ra, ($sp)
  91.  
  92. move $s4, $s0 #store s0 in the hypotenuse slot
  93. bgt $s4, $s1, evaluate_s0_gt_s2 #if s0>s1, go check s0 and s2
  94. j evaluate_s1_gt_s2 #if not, then s1 is greater than s0
  95.  
  96. evaluate_s0_gt_s2:
  97. bgt $s4, $s2, s0_grt #if s0 > s2 then s0 is the greatest
  98. j s2_grt #if not, then s2 is the greatest
  99.  
  100. evaluate_s1_gt_s2:
  101. move $s4, $s1 #since s1 was bigger than s0, we place s1 into s5
  102. bgt $s4, $s2, s1_grt #if s1 > s2 then s1 is the greatest
  103. j s2_grt #if not, then s2 is the greatest
  104.  
  105. s0_grt:
  106. #s0 turned out to be the greatest number, so it is our hypotenuse
  107. move $s4, $s0
  108. move $s5, $s1
  109. move $s6, $s2
  110.  
  111. jal power
  112.  
  113. s1_grt:
  114. #s1 turned out to be the greatest number, so it is our hypotenuse
  115. move $s4, $s1
  116. move $s5, $s0
  117. move $s6, $s2
  118.  
  119. jal power
  120.  
  121. s2_grt:
  122. #s2 turned out to be the greatest number, so it is our hypotenuse
  123. move $s4, $s2
  124. move $s5, $s0
  125. move $s6, $s1
  126.  
  127. jal power
  128.  
  129. hard_exit:
  130. #this only triggers if any of the numbers are equal to each other
  131. lw $ra, ($sp)
  132. addi $sp, $sp, 8
  133. jr $ra #jump back and exit
  134.  
  135. power:
  136. addi $sp, $sp, -4 #position 16
  137. sw $ra, ($sp)
  138. li $t3, 0 #our power incrementor
  139.  
  140. power_loop:
  141. add $t3, $t3, 1 #increment out power inrcrementor
  142. beq $t3, 2, calculate #our incrementor is also the power to which we raise our values. in this case it's 2 but we could change it to other stuff
  143.  
  144. mul $s4, $s4, $s4 #square the hypotenuse
  145. mul $s5, $s5, $s5, #square one leg
  146. mul $s6, $s6, $s6 #square the other
  147.  
  148. jal calculate
  149.  
  150. calculate:
  151. addi $sp, $sp, -4 #position -20
  152. sw $ra, ($sp)
  153.  
  154. #set the sum of the two legs to s7. if s4 and s7 are equal, then it's a right triangle, if they aren't, then it isn't
  155. add $s7, $s5, $s6
  156.  
  157. beq $s4, $s7, yes
  158. j no
  159.  
  160. yes:
  161. #output all the numbers and commas
  162. move $a0, $s0 #output the first integer, then a comma space
  163. li $v0, 1
  164. syscall
  165.  
  166. li $v0, 4
  167. la $a0, comma
  168. syscall
  169.  
  170. move $a0, $s1 #output the second integer, then a comma space
  171. li $v0, 1
  172. syscall
  173.  
  174. li $v0, 4
  175. la $a0, comma
  176. syscall
  177.  
  178. move $a0, $s2 #output the third integer, then a comma space
  179. li $v0, 1
  180. syscall
  181.  
  182. li $v0, 4
  183. la $a0, yes_right
  184. syscall
  185.  
  186. addi $sp, $sp, 16
  187. lw $ra, ($sp)
  188. jr $ra #jump back and exit
  189.  
  190. no:
  191. #output all the numbers and commas
  192. move $a0, $s0 #output the first integer, then a comma space
  193. li $v0, 1
  194. syscall
  195.  
  196. li $v0, 4
  197. la $a0, comma
  198. syscall
  199.  
  200. move $a0, $s1 #output the second integer, then a comma space
  201. li $v0, 1
  202. syscall
  203.  
  204. li $v0, 4
  205. la $a0, comma
  206. syscall
  207.  
  208. move $a0, $s2 #output the third integer, then a comma space
  209. li $v0, 1
  210. syscall
  211.  
  212. li $v0, 4
  213. la $a0, no_right
  214. syscall
  215.  
  216. addi $sp, $sp, 16
  217. lw $ra, ($sp)
  218. jr $ra #jump back and exit
Advertisement
Add Comment
Please, Sign In to add comment