Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.67 KB | None | 0 0
  1. #anpoulos
  2. ###########################################################
  3. # Program Description
  4. #
  5. # CS315 Lab 7 assignment
  6. #
  7. # Use create_array to create two arrays
  8. # Use inner_product to calculate the inner product of the arrays
  9. #
  10.  
  11. ###########################################################
  12. # Register Usage
  13. # $t0
  14. # $t1
  15. # $t2
  16. # $t3
  17. # $t4
  18. # $t5
  19. # $t6
  20. # $t7
  21. # $t8
  22. # $t9
  23. #
  24. # TODO:
  25. # Add register descriptions
  26. #
  27. ###########################################################
  28. .data
  29.  
  30. intro_p: .asciiz "CS315 Lab 7 - Passing arguments on stack\n\n"
  31.  
  32. arrayALen_p: .asciiz "Enter a length for array A: "
  33. createA_p: .asciiz "\nCreating array A...\n"
  34.  
  35. arrayBLen_p: .asciiz "Enter a length for array B: "
  36. createB_p: .asciiz "\nCreating array B...\n"
  37.  
  38. innerProd_p: .asciiz "\nInner product: "
  39.  
  40. baseA: .word 0 # Base address for array A
  41. lengthA: .word 0 # Length of array A
  42. baseB: .word 0 # Base address for array B
  43. lengthB: .word 0 # Length for array B
  44.  
  45.  
  46. ###########################################################
  47. .text
  48. main:
  49. # printString(intro_p)
  50. li $v0, 4
  51. la $a0, intro_p
  52. syscall
  53.  
  54. # Get and store length for array A
  55. li $v0, 4
  56. la $a0, arrayALen_p
  57. syscall # Print 'enter length'
  58. li $v0, 5
  59. syscall # Read length
  60.  
  61. la $t0, lengthA # Load lengthA address
  62. sw $v0, 0($t0) # Store length at lengthA address
  63.  
  64. # Get and store length for array B
  65. li $v0, 4
  66. la $a0, arrayBLen_p
  67. syscall # Print 'enter length'
  68. li $v0, 5
  69. syscall # Read length
  70.  
  71. la $t0, lengthB
  72. sw $v0, 0($t0) # Store length
  73.  
  74.  
  75. # Create array A
  76. li $v0, 4
  77. la $a0, createA_p
  78. syscall # Print 'creating'
  79.  
  80. # If we needed to back up registers, this is where we
  81. # would do it (i.e. right here, before $ra)
  82. addiu $sp, $sp, -4 # Allocate memory for $ra
  83. sw $ra, 0($sp) # Store $ra
  84. addiu $sp, $sp, -8 # Allocate memory for arguments
  85. la $t0, lengthA # Load address of lengthA
  86. lw $t0, 0($t0) # Read the value from lengthA...
  87. sw $t0, 0($sp) # ...then write that value to the stack
  88.  
  89. jal create_array # Call create_array
  90.  
  91. lw $t0, 4($sp) # Load base address from stack
  92. la $t1, baseA # Load address of baseA
  93. sw $t0, 0($t1) # Store base address at baseA
  94. addiu $sp, $sp, 8 # Deallocate argument memory
  95. lw $ra, 0($sp) # Restore $ra
  96. addiu $sp, $sp, 4 # Deallocate memory for $ra
  97. # If we backed up any registers, this is where we
  98. # would restore them and deallocate the stack memory
  99. # used by the backups
  100.  
  101.  
  102. # Create array B
  103. li $v0, 4
  104. la $a0, createB_p
  105. syscall # Print 'creating'
  106.  
  107. addiu $sp, $sp, -4 # Allocate memory for $ra
  108. sw $ra, 0($sp) # Store $ra
  109. addiu $sp, $sp, -8 # Allocate memory for arguments
  110. la $t0, lengthB # Load address of lengthB
  111. lw $t0, 0($t0) # Read the value from lengthB...
  112. sw $t0, 0($sp) # ...then write that value to the stack
  113.  
  114. jal create_array # Call create_array
  115.  
  116. lw $t0, 4($sp) # Load base address from stack
  117. la $t1, baseB # Load address of baseB
  118. sw $t0, 0($t1) # Store base address at baseB
  119. addiu $sp, $sp, 8 # Deallocate argument memory
  120. lw $ra, 0($sp) # Restore $ra
  121. addiu $sp, $sp, 4 # Deallocate memory for $ra
  122. # If we backed up any registers, this is where we
  123.  
  124.  
  125.  
  126.  
  127. # Load bases and lengths from their variables into $t registers
  128. la $t0, baseA
  129. lw $t0, 0($t0) # t0 <-- mem[baseA]
  130. la $t1, lengthA
  131. lw $t1, 0($t1) # t1 <-- mem[lengthA]
  132. la $t2, baseB
  133. lw $t2, 0($t2) # t2 <-- mem[baseB]
  134. la $t3, lengthB
  135. lw $t3, 0($t3) # t3 <-- mem[lengthB]
  136.  
  137. # TODO: Use inner_product to calculate the inner product
  138. # of arrays A and B
  139.  
  140. addiu $sp, $sp, -24 #reserving values on stack
  141. sw $t0, 0($sp) #address of dyn array A
  142. sw $t1, 4($sp) #lengthA
  143. sw $t2, 8($sp) #address of dyn array B
  144. sw $t3, 12($sp) #lengthB
  145. sw $ra, 16($sp) #ra
  146.  
  147. jal inner_product
  148.  
  149. lw $t0, 20($sp) #loads sum of inner products to t0
  150. lw $ra, 16($sp) #loads ra
  151. addiu $sp, $sp, 24 #deallocates stack
  152.  
  153. la $a0, innerProd_p
  154. li $v0, 4 #prints innerprod string
  155. syscall
  156.  
  157. move $a0, $t0 #loads sum of inner products to a0
  158. li $v0, 1
  159. syscall
  160.  
  161. # TODO: Print inner product of A and B
  162.  
  163. mainEnd:
  164. li $v0, 10
  165. syscall # Halt
  166. ###########################################################
  167.  
  168. ###########################################################
  169. # Subprogram Description
  170. #
  171. # Calculates the inner product of two arrays
  172. #
  173. # Inner product:
  174. # For arrays A and B of equal length n, the inner product
  175. # is calculated as:
  176. # A[0]*B[0] + A[1]*B[1] + A[2]*B[2] + ... + A[n-1]*B[n-1]
  177. #
  178. # http://en.wikipedia.org/wiki/Dot_product
  179. #
  180. # If array lengths do not match or either length is less
  181. # than or equal to 0, print an error and return 0
  182. #
  183.  
  184. ###########################################################
  185. # Arguments In and Out of subprogram
  186. #
  187. # $a0
  188. # $a1
  189. # $a2
  190. # $a3
  191. # $v0
  192. # $v1
  193. # $sp+0 Base address of array A (IN)
  194. # $sp+4 Length of array A (IN)
  195. # $sp+8 Base address of array B (IN)
  196. # $sp+12 Length of array B (IN)
  197. # $sp+16 RA from main (IN)
  198. # $sp+20 Sum of inner products (OUT)
  199. ###########################################################
  200. # Register Usage
  201. # $t0 - address A
  202. # $t1 - length A
  203. # $t2 - address B
  204. # $t3 - length B/counter
  205. # $t4 - sum of inner
  206. # $t5 - word from A
  207. # $t6 - word from B
  208. # $t7 -
  209. # $t8 - sum of word A and word B
  210. # $t9 -
  211. ###########################################################
  212. .data
  213. lengthError_p: .asciiz "\nERROR: Invalid array length(s)\n"
  214.  
  215. ###########################################################
  216. .text
  217. inner_product:
  218.  
  219. # TODO: Write this subprogram
  220.  
  221. # Note: This subprogram may require the use of
  222. # many $t registers
  223.  
  224. lw $t0, 0($sp) #address array A
  225. lw $t1, 4($sp) #length array A
  226. lw $t2, 8($sp) #address array B
  227. lw $t3, 12($sp) #length array B
  228. li $t4, 0 #sum of inners
  229.  
  230.  
  231. beq $t1, $t3, continue #if they are equal, ingore the error
  232.  
  233. error:
  234. la $a0, lengthError_p
  235. li $v0, 4 #prints error if lengths are not equal and exits
  236. syscall
  237. b ipEnd
  238.  
  239. continue:
  240. li $t3, 0 #counter
  241. addiu $t0, -4 #address A -4
  242. addiu $t2, -4 #address B -4
  243.  
  244. loop:
  245. beq $t3, $t1, ipEnd
  246.  
  247. addiu $t0, $t0, 4 #increment address A by 4
  248. addiu $t2, $t2, 4 #increment address B by 4
  249.  
  250. lw $t5, 0($t0) #loads word from array A at index i
  251. lw $t6, 0($t2) #loads word from array B at index i
  252.  
  253. add $t8, $t5, $t6 #adds words together, stores in t8
  254. add $t4, $t8, $t4 #t4 += t8
  255.  
  256. addiu $t3, 1 #t3 += 1
  257. b loop
  258.  
  259.  
  260. ipEnd:
  261. sw $t4, 20($sp) #sum of inners
  262. jr $ra # Return
  263. ###########################################################
  264.  
  265. ###########################################################
  266. # Subprogram Description
  267. #
  268. # Dynamically allocate array of words
  269. # Use read_array to fill entire array
  270. #
  271.  
  272. ###########################################################
  273. # Arguments In and Out of subprogram
  274. #
  275. # $a0
  276. # $a1
  277. # $a2
  278. # $a3
  279. # $v0
  280. # $v1
  281. # $sp+0 Array length (IN)
  282. # $sp+4 Base address (OUT)
  283. # $sp+8
  284. # $sp+12
  285. ###########################################################
  286. # Register Usage
  287. # $t0 Length
  288. # $t1 4
  289. # $t2 dynamic array address
  290. # $t3
  291. # $t4
  292. # $t5
  293. # $t6
  294. # $t7
  295. # $t8
  296. # $t9
  297. ###########################################################
  298. .data
  299.  
  300. ###########################################################
  301. .text
  302. create_array:
  303. lw $t0, 0($sp) # Load length
  304.  
  305. sw $0, 4($sp) # Initialize address to 0
  306. blez $t0, caEnd # End if length <= 0
  307.  
  308. # TODO: Finish this subprogram
  309.  
  310. # Allocate array
  311. li $t1, 4 #loads t1 with 4, offset for each word created in dynamic array
  312.  
  313. mult $t0, $t1 #generates number of bytes required for dyn array
  314. mflo $a0 #numb of bytes
  315.  
  316. li $v0, 9
  317. syscall #reserves numb of bytes in a0, stores address in v0
  318.  
  319. sw $v0, 4($sp) #stores array address on stack
  320.  
  321.  
  322. addiu $sp, $sp, -4 #reserve
  323. sw $ra, 0($sp) #stores ra on stack
  324.  
  325.  
  326. jal read_array
  327.  
  328. # Call read_array to fill the array
  329. # Return the base address on the stack
  330.  
  331. caEnd:
  332. lw $ra, 0($sp) #loads ra from stack
  333. addiu $sp, $sp, 4 #deallocates
  334. jr $ra # Return
  335. ###########################################################
  336.  
  337. ###########################################################
  338. # Subprogram Description
  339. #
  340. # Print an array
  341. #
  342.  
  343. ###########################################################
  344. # Arguments In and Out of subprogram
  345. #
  346. # $a0
  347. # $a1
  348. # $a2
  349. # $a3
  350. # $v0
  351. # $v1
  352. # $sp+0 Base address (IN)
  353. # $sp+4 Length (IN)
  354. # $sp+8
  355. # $sp+12
  356. ###########################################################
  357. # Register Usage
  358. # $t0 Array address
  359. # $t1 Loop countdown
  360. # $t2
  361. # $t3
  362. # $t4
  363. # $t5
  364. # $t6
  365. # $t7
  366. # $t8
  367. # $t9
  368. ###########################################################
  369. .data
  370.  
  371. ###########################################################
  372. .text
  373. print_array:
  374. lw $t0, 0($sp) # t0 <-- base address
  375. lw $t1, 4($sp) # t1 <-- length
  376.  
  377. paWhile:
  378. blez $t1, paEndWhile # while(t1 > 0)
  379.  
  380. lw $a0, 0($t0) # $a0 <-- mem(t0 + 0)
  381. li $v0, 1
  382. syscall # printInt(a0)
  383.  
  384. li $a0, 0x20 # 0x20 == ASCII space
  385. li $v0, 11 # System call 11, print character
  386. syscall # printChar(' ')
  387.  
  388. addiu $t0, $t0, 4 # t0 <-- t0 + 4
  389.  
  390. addiu $t1, $t1, -1 # t0 <-- t0 - 1
  391. b paWhile # Loop
  392.  
  393. paEndWhile:
  394.  
  395. paEnd:
  396. jr $ra # Return
  397. ###########################################################
  398.  
  399. ###########################################################
  400. # Subprogram Description
  401. #
  402. # Read values into an array
  403. # Fills entire array
  404. #
  405.  
  406. ###########################################################
  407. # Arguments In and Out of subprogram
  408. #
  409. # $a0
  410. # $a1
  411. # $a2
  412. # $a3
  413. # $v0
  414. # $v1
  415. # $sp+0 Base address (IN)
  416. # $sp+4 Length (IN)
  417. # $sp+8
  418. # $sp+12
  419. ###########################################################
  420. # Register Usage
  421. # $t0 Array address
  422. # $t1 Loop countdown
  423. # $t2
  424. # $t3
  425. # $t4
  426. # $t5
  427. # $t6
  428. # $t7
  429. # $t8
  430. # $t9
  431. ###########################################################
  432. .data
  433. enterValue_p: .asciiz "Enter an integer: "
  434.  
  435. ###########################################################
  436. .text
  437. read_array:
  438. lw $t0, 8($sp) # t0 <-- base address
  439. lw $t1, 4($sp) # t1 <-- length
  440.  
  441. raWhile:
  442. blez $t1, raEndWhile # while(t1 > 0)
  443.  
  444. li $v0, 4
  445. la $a0, enterValue_p
  446. syscall # Print 'enter value'
  447. li $v0, 5
  448. syscall # Read integer
  449.  
  450. sw $v0, 0($t0) # Store entry
  451. addiu $t0, $t0, 4 # Increment address
  452.  
  453. addiu $t1, $t1, -1 # Decrement counter
  454. b raWhile # Loop
  455.  
  456. raEndWhile:
  457.  
  458. raEnd:
  459. jr $ra # Return
  460. ###########################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement