Advertisement
Guest User

dynamicking

a guest
Feb 28th, 2015
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. ###########################################################
  2. # Part A:
  3. # #Declare a static array of 10 words
  4. # Read values into the static array, then print the array
  5. #
  6. # Part B:
  7. # Read an array length from the console, then allocate
  8. # a dynamic array of the given length
  9. # Read values into the dynamic array, then print the array
  10. #
  11. # Note: Specifications for argument registers ($a0, $a1)
  12. # for read_array and print_array ARE DIFFERENT!
  13. # You MAY NOT repurpose them
  14. #
  15. ###########################################################
  16. # Register Usage
  17. # $t0 Dynamic Length (Part B)
  18. # $t1
  19. # $t2
  20. # $t3
  21. # $t4
  22. # $t5
  23. # $t6
  24. # $t7
  25. # $t8
  26. # $t9
  27. ###########################################################
  28. .data
  29. myArray: .word 0:10 #static array for part A
  30. aIntro_p: .asciiz "This is Part A. Please enter 10 integers:\n";
  31. bIntro_p: .asciiz "\n\nThis is Part B. Please enter an integer for the length of the array.\nLength: ";
  32. bEnter_p: .asciiz "\nEnter your integers:\n"
  33. End_p: .asciiz "\nThe array contains:\n";
  34. ###########################################################
  35. .text
  36. main:
  37.  
  38. ### Part A ###
  39. # Note argument registers! Attention to detail is critical
  40. # Use read_array to fill the static array
  41. # Use print_array to print the static array
  42.  
  43. la $a0, aIntro_p
  44. li $v0, 4
  45. syscall
  46.  
  47. li $a0, 10 #array length IN $a0
  48. la $a1, myArray #since static, base address loaded as such
  49.  
  50. jal read_array
  51.  
  52. la $a0, End_p
  53. li $v0, 4
  54. syscall
  55.  
  56. la $a0, myArray #base address of static
  57. li $a1, 10
  58. jal print_array
  59.  
  60. ### Part B ###
  61. # Allocate a dynamic array
  62. # Note argument registers! Attention to detail is critical
  63. # Use read_array to fill the dynamic array
  64. # Use print_array to prin the dynamic array
  65.  
  66. la $a0, bIntro_p
  67. li $v0, 4
  68. syscall
  69.  
  70. li $a0, 0 #reset
  71. li $a1, 0 #reset
  72.  
  73. li $v0, 5
  74. syscall
  75. move $t0, $v0 #moves length into $t0 for allocation, keeps length there
  76.  
  77. li $v0, 9 #allocation, sets base address into $v0
  78. move $a0, $t0
  79. syscall #base address is now in $v0
  80. move $t1, $v0 #base now in $t1
  81.  
  82. move $a0, $t0 #length ($t0) goes into $a0 before reading
  83. move $a1, $t1 #base address ($t1) goes into $a1 before reading
  84.  
  85. jal read_array
  86.  
  87. la $a0, End_p
  88. li $v0, 4
  89. syscall
  90.  
  91. #move $a0, $a1 #first moves base address out of $a1 and into $a0 for the print subprogram
  92. #move $a1, $t1 #moves the length of the array back out of the temp and into and argument register
  93. #jal print_array
  94.  
  95. li $v0, 10
  96. syscall # Halt
  97. ###########################################################
  98.  
  99. ###########################################################
  100. # Subprogram Description
  101. # Prints an array of words
  102.  
  103. ###########################################################
  104. # Arguments In and Out of subprogram
  105. #
  106. # $a0 Base address
  107. # $a1 Array length
  108. # $a2
  109. # $a3
  110. # $v0
  111. # $v1
  112. # $sp
  113. # $sp+4
  114. # $sp+8
  115. # $sp+12
  116. ###########################################################
  117. # Register Usage
  118. # $t0 Base Address
  119. # $t1 Length
  120. # $t2 Constant 4
  121. # $t3 Count
  122. # $t4
  123. # $t5 Current Word
  124. # $t6
  125. # $t7
  126. # $t8
  127. # $t9
  128. ###########################################################
  129. .data
  130.  
  131. ###########################################################
  132. .text
  133. print_array:
  134. # Print words from the array, separated by a space
  135.  
  136. move $t0, $a0 #load base
  137. move $t1, $a1 #load length
  138. li $t2, 4 #load constant 4
  139. li $t3, 0 #set count variable to zero
  140.  
  141. #i = b + s * n
  142.  
  143. paFor:
  144. bge $t3, $t1, paEndFor #checking count
  145. mul $t4, $t2, $t3
  146. add $t4,$t0, $t4
  147.  
  148. lw $t5, 0($t4) #load the word from the array
  149.  
  150. move $a0, $t5
  151. li $v0, 1 #print int from loaded
  152. syscall
  153.  
  154. li $v0, 1
  155. move $a0, $t5
  156.  
  157. li $v0, 11
  158. li $a0, 32 #ascii value for space
  159. syscall
  160.  
  161. addiu $t3, $t3, 1
  162. b paFor
  163.  
  164. paEndFor:
  165. jr $ra
  166. ###########################################################
  167.  
  168. ###########################################################
  169. # Subprogram Description
  170. # Reads words into an array
  171.  
  172. ###########################################################
  173. # Arguments In and Out of subprogram
  174. #
  175. # $a0 Array length
  176. # $a1 Base address
  177. # $a2
  178. # $a3
  179. # $v0
  180. # $v1
  181. # $sp
  182. # $sp+4
  183. # $sp+8
  184. # $sp+12
  185. ###########################################################
  186. # Register Usage
  187. # $t0 Length
  188. # $t1 Index
  189. # $t2
  190. # $t3
  191. # $t4
  192. # $t5
  193. # $t6
  194. # $t7
  195. # $t8
  196. # $t9 Count
  197. ###########################################################
  198. .data
  199. myBaseHolder: .word 0 #declare word to hold base
  200. ###########################################################
  201. .text
  202. read_array:
  203. # Read words from the console, store them in
  204. # the array until the array is full
  205. li $t0, 0
  206. li $t1, 0
  207.  
  208. move $t0, $a0 #length
  209. move $t1, $a1 #base address
  210. li $t9, 0 #makes sure count is reset before engaging
  211. sw $t1, myBaseHolder #save the base address into the holder word
  212.  
  213. rWhile:
  214. bge $t9, $t0, endR #branch to end if count > length
  215.  
  216. li $v0, 5 #call for an int from console
  217. syscall
  218.  
  219. sw $v0, 0($t1) #saves the word from the console into the array
  220.  
  221. addiu $t9, $t9, 1 #count++
  222. addiu $t1, $t1, 4 #increments the address
  223. b rWhile
  224.  
  225. endR:
  226. jr $ra
  227. ###########################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement