Guest User

Untitled

a guest
Jan 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. .text
  2.  
  3. .ent main
  4. main:
  5. addiu $29,$29,-24 # create stack of minimum size for main
  6. sw $31,20($29) #save return address
  7.  
  8. #announce that we will be doing A+B
  9. li $v0,4 #print string
  10. la $a0, strabadd #address of strabadd
  11. syscall #print it
  12.  
  13. la $4, arrayA #first argument to add function, pointer to array A
  14. la $5, arrayB #second argument, array B
  15.  
  16. jal AddArray #jump to add array
  17.  
  18. #print newline
  19. li $v0,4
  20. la $a0,strnewline
  21. syscall
  22.  
  23. #announce that we will be doing A*A
  24. li $v0, 4
  25. la $a0, straamult
  26. syscall
  27.  
  28. la $4, arrayA
  29. la $5, arrayA
  30.  
  31. jal MultArray
  32.  
  33. li $v0,4
  34. la $a0,strnewline
  35. syscall
  36.  
  37. #announce that we will be doing B*B
  38. li $v0, 4
  39. la $a0, strbbmult
  40. syscall
  41.  
  42. la $4, arrayB
  43. la $5, arrayB
  44. jal MultArray
  45.  
  46. li $v0,4
  47. la $a0,strnewline
  48. syscall
  49.  
  50. #announce that we will be doing A-B
  51. li $v0, 4
  52. la $a0, strabsub
  53. syscall
  54.  
  55. la $4, arrayA
  56. la $5, arrayB
  57. jal SubArray
  58.  
  59. li $v0,4
  60. la $a0,strnewline
  61. syscall
  62.  
  63. #announce that we will be doing A/B - read on discussion posts that we apparently don't need to
  64. #print the remainder, so I left that out; I hope I didn't need to!
  65. li $v0,4
  66. la $a0,strabdiv
  67. syscall
  68.  
  69. la $4,arrayA
  70. la $5,arrayB
  71. jal DivArray
  72.  
  73. lw $31,20($29) #load original return address from stack
  74. jr $31 #return - main
  75. .end main
  76.  
  77. .ent DivArray
  78. DivArray:
  79. addu $8, $0, $4 #initialize $8 to address of A
  80. addu $9, $0, $5 #initialize $9 to the address of B
  81.  
  82. #$10 will be the counter
  83. addiu $10,$0,0
  84. #$11 will be the value of A at position i
  85. #$12 will be the value of B at position i
  86. #$13 will be the result of A/B
  87.  
  88. $loopd:
  89. #if counter < 5... else, branch to done
  90. beq $10,5,$doned
  91. #load value of A at position
  92. lw $11, 0($8)
  93. #load value of B at position
  94. lw $12, 0($9)
  95. #perform their addition
  96. divu $11, $12
  97. mflo $14 #move LO into $14; with the given integers, values never exceed what is available in LO, so only need LO
  98. #print result followed by a space
  99. li $2,1
  100. la $4,($14)
  101. syscall
  102. li $2,4
  103. la $4, strspace
  104. syscall
  105. #increment counter and positions
  106. addiu $8,$8,4
  107. addiu $9,$9,4
  108. addiu $10,$10,1
  109.  
  110. #call loopd
  111. b $loopd
  112. $doned:
  113. jr $31 #return - multarray
  114. .end DivArray
  115.  
  116. .ent SubArray
  117. SubArray:
  118. addu $8,$0,$4 #initialize $8 to address of A
  119. addu $9,$0,$5 #initialize $9 to address of B
  120.  
  121. # $10 will be the counter
  122. addiu $10,$0,0
  123. # $11 will be value of A at position
  124. # $12 will be value of B at position
  125. # $13 will be the result of A - B
  126.  
  127. $loops:
  128. #if counter < 5... else, branch to done
  129. beq $10,5,$dones
  130. #load value of A at position
  131. lw $11, 0($8)
  132. #load value of B at position
  133. lw $12, 0($9)
  134. #perform their addition
  135. subu $13, $11, $12
  136. #print result followed by a space
  137. li $2,1
  138. la $4,($13)
  139. syscall
  140. li $2,4
  141. la $4, strspace
  142. syscall
  143. #increment counter and positions
  144. addiu $8,$8,4
  145. addiu $9,$9,4
  146. addiu $10,$10,1
  147.  
  148. #call loops
  149. b $loops
  150. $dones:
  151. jr $31 #return - AddArray
  152. .end SubArray
  153.  
  154. .ent MultArray
  155. MultArray:
  156. addu $8, $0, $4 #initialize $8 to address of A
  157. addu $9, $0, $5 #initialize $9 to the address of B
  158.  
  159. #$10 will be the counter
  160. addiu $10,$0,0
  161. #$11 will be the value of A at position i
  162. #$12 will be the value of B at position i
  163. #$13 will be the result of A*B
  164.  
  165. $loopm:
  166. #if counter < 5... else, branch to done
  167. beq $10,5,$donem
  168. #load value of A at position
  169. lw $11, 0($8)
  170. #load value of B at position
  171. lw $12, 0($9)
  172. #perform their addition
  173. multu $11, $12
  174. mflo $14 #move LO into $14; with the given integers, values never exceed what is available in LO, so only need LO
  175. #print result followed by a space
  176. li $2,1
  177. la $4,($14)
  178. syscall
  179. li $2,4
  180. la $4, strspace
  181. syscall
  182. #increment counter and positions
  183. addiu $8,$8,4
  184. addiu $9,$9,4
  185. addiu $10,$10,1
  186.  
  187. #call loopm
  188. b $loopm
  189. $donem:
  190. jr $31 #return - multarray
  191. .end MultArray
  192.  
  193. .ent AddArray
  194. AddArray:
  195. addu $8,$0,$4 #initialize $8 to address of A
  196. addu $9,$0,$5 #initialize $9 to address of B
  197.  
  198. # $10 will be the counter
  199. addiu $10,$0,0
  200. # $11 will be value of A at position
  201. # $12 will be value of B at position
  202. # $13 will be the result of A + B
  203.  
  204. $loopa:
  205. #if counter < 5... else, branch to done
  206. beq $10,5,$donea
  207. #load value of A at position
  208. lw $11, 0($8)
  209. #load value of B at position
  210. lw $12, 0($9)
  211. #perform their addition
  212. addu $13, $11, $12
  213. #print result followed by a space
  214. li $2,1
  215. la $4,($13)
  216. syscall
  217. li $2,4
  218. la $4, strspace
  219. syscall
  220. #increment counter and positions
  221. addiu $8,$8,4
  222. addiu $9,$9,4
  223. addiu $10,$10,1
  224.  
  225. #call loopa
  226. b $loopa
  227. $donea:
  228. jr $31 #return - AddArray
  229. .end AddArray
  230.  
  231. .data
  232. arrayA: .word 11,13,15,17,19 # create array A
  233. arrayB: .word 10,12,14,16,18 # create array B
  234.  
  235. strabadd: .asciiz "A+B: "
  236. straamult: .asciiz "A*A: "
  237. strbbmult: .asciiz "B*B: "
  238. strabsub: .asciiz "A-B: "
  239. strabdiv: .asciiz "A/B (integer portion): "
  240. strnewline: .asciiz "\n"
  241. strspace: .asciiz " "
Add Comment
Please, Sign In to add comment