Advertisement
hououinmakise

Untitled

Mar 25th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. .data
  2.  
  3. array: .word 2, 3, 5, 7, 11
  4. array_size: .word 5
  5.  
  6. pr1: .asciiz "Array["
  7. pr2: .asciiz "]="
  8. pr3: .asciiz "Sum of array is "
  9. nl: .asciiz "\n"
  10.  
  11. .text
  12.  
  13. .globl main
  14.  
  15. # the main routine
  16. # has a for loop, and then calls array_sum
  17. # s0: i
  18. # s1: always the array_size defined in .data
  19. # t0: addr of array
  20. # t1: i * 4; to calculate addr shift for array[i]
  21. main:
  22. li $s0, 0 # functions as i
  23. la $s1, array_size # prepare to load array_size to s1
  24. lw $s1, ($s1) # load array_size to s1
  25.  
  26. for_loop: la $a0, pr1 # "Array["
  27. li $v0, 4 # string
  28. syscall
  29.  
  30. move $a0, $s0 # load i into a0 for printing
  31. li $v0, 1 # int
  32. syscall
  33.  
  34. la $a0, pr2 # "]="
  35. li $v0, 4 # string
  36. syscall
  37.  
  38. la $t0, array # load addr of array into t0
  39. li $t1, 4 # load 4 into t1 for multiplication purposes
  40. mul $t1, $s0, $t1 # multiply i by 4
  41. addu $t0, $t0, $t1 # get addr of array[i]
  42. lw $a0, ($t0) # load array[i] into a0 for printing
  43. li $v0, 1 # int
  44. syscall
  45.  
  46. la $a0, nl # newline
  47. li $v0, 4 # string
  48. syscall
  49.  
  50. addiu $s0, $s0, 1 # increment i
  51. bne $s0, $s1, for_loop # loop again if i < array_size
  52.  
  53. la $t0, array # load addr of array into t0
  54. move $a0, $t0 # copy array addr into arg 0
  55. move $a1, $s1 # copy array size into arg 1
  56.  
  57. jal array_sum # go to array sum w/ args
  58.  
  59. move $t0, $v0 # copy result of array_sum into t0
  60.  
  61. la $a0, pr3 # "Sum of array is "
  62. li $v0, 4 # string
  63. syscall
  64.  
  65. move $a0, $t0 # result of array_sum
  66. li $v0, 1 # int
  67. syscall
  68.  
  69. la $a0, nl # newline
  70. li $v0, 4 # string
  71. syscall
  72.  
  73. # exit
  74. li $v0, 10
  75. syscall
  76.  
  77. # calculates the sum of all the elements in an array
  78. # a0: *array; the addr of array
  79. # a1: arraySize; the size of the array
  80. # t0: constant 0; used to compare to array_size
  81. # t1: array[n]
  82. # t2: addr of array[n + 1]
  83. # t3: array_size - 1
  84. array_sum:
  85. li $t0, 0 # load constant 0 into t0
  86. beq $a1, $t0, array_sum_end # go to return routine if array_size == 0
  87.  
  88. lw $t1, ($a0) # load array[n] into t1
  89. addiu $t2, $a0, 4 # shift array addr to point to array[n + 1]
  90. move $t3, $a1 # copy array_size into t3
  91. addiu $t3, $t3, -1 # decrement array_size
  92.  
  93. addiu $sp, $sp, 4 # prepare to save t1
  94. sw $t1, ($sp) # save t1
  95. addiu $sp, $sp, 4 # prepare to save t2
  96. sw $t2, ($sp) # save t2
  97. addiu $sp, $sp, 4 # prepare to save t3
  98. sw $t3, ($sp) # save t3
  99. addiu $sp, $sp, 4 # prepare to save ra
  100. sw $ra, ($sp) # save ra
  101.  
  102. move $a0, $t2 # copy shifted array addr to arg 0
  103. move $a1, $t3 # copy decr'd array_size to arg 1
  104.  
  105. jal array_sum # recursive call
  106.  
  107. lw $ra, ($sp) # restore ra
  108. addiu $sp, $sp, -4 # prepare to restore t3
  109. lw $t3, ($sp) # restore t3
  110. addiu $sp, $sp, -4 # prepare to restore t2
  111. lw $t2, ($sp) # restore t2
  112. addiu $sp, $sp, -4 # prepare to restore t1
  113. lw $t1, ($sp) # restore t1
  114. addiu $sp, $sp, -4 # restore stack pointer
  115.  
  116. addu $v0, $t1, $v0 # add array[n] and result of array_sum together into return
  117.  
  118. jr $ra # go back to caller
  119.  
  120. # sets return value of 0 and jumps back to caller
  121. # the caller will always be array_sum
  122. # used when array_size == 0
  123. array_sum_end:
  124. move $v0, $t0 # copy 0 to v0
  125. jr $ra # return to caller
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement