Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- array: .word 2, 3, 5, 7, 11
- array_size: .word 5
- pr1: .asciiz "Array["
- pr2: .asciiz "]="
- pr3: .asciiz "Sum of array is "
- nl: .asciiz "\n"
- .text
- .globl main
- # the main routine
- # has a for loop, and then calls array_sum
- # s0: i
- # s1: always the array_size defined in .data
- # t0: addr of array
- # t1: i * 4; to calculate addr shift for array[i]
- main:
- li $s0, 0 # functions as i
- la $s1, array_size # prepare to load array_size to s1
- lw $s1, ($s1) # load array_size to s1
- for_loop: la $a0, pr1 # "Array["
- li $v0, 4 # string
- syscall
- move $a0, $s0 # load i into a0 for printing
- li $v0, 1 # int
- syscall
- la $a0, pr2 # "]="
- li $v0, 4 # string
- syscall
- la $t0, array # load addr of array into t0
- li $t1, 4 # load 4 into t1 for multiplication purposes
- mul $t1, $s0, $t1 # multiply i by 4
- addu $t0, $t0, $t1 # get addr of array[i]
- lw $a0, ($t0) # load array[i] into a0 for printing
- li $v0, 1 # int
- syscall
- la $a0, nl # newline
- li $v0, 4 # string
- syscall
- addiu $s0, $s0, 1 # increment i
- bne $s0, $s1, for_loop # loop again if i < array_size
- la $t0, array # load addr of array into t0
- move $a0, $t0 # copy array addr into arg 0
- move $a1, $s1 # copy array size into arg 1
- jal array_sum # go to array sum w/ args
- move $t0, $v0 # copy result of array_sum into t0
- la $a0, pr3 # "Sum of array is "
- li $v0, 4 # string
- syscall
- move $a0, $t0 # result of array_sum
- li $v0, 1 # int
- syscall
- la $a0, nl # newline
- li $v0, 4 # string
- syscall
- # exit
- li $v0, 10
- syscall
- # calculates the sum of all the elements in an array
- # a0: *array; the addr of array
- # a1: arraySize; the size of the array
- # t0: constant 0; used to compare to array_size
- # t1: array[n]
- # t2: addr of array[n + 1]
- # t3: array_size - 1
- array_sum:
- li $t0, 0 # load constant 0 into t0
- beq $a1, $t0, array_sum_end # go to return routine if array_size == 0
- lw $t1, ($a0) # load array[n] into t1
- addiu $t2, $a0, 4 # shift array addr to point to array[n + 1]
- move $t3, $a1 # copy array_size into t3
- addiu $t3, $t3, -1 # decrement array_size
- addiu $sp, $sp, 4 # prepare to save t1
- sw $t1, ($sp) # save t1
- addiu $sp, $sp, 4 # prepare to save t2
- sw $t2, ($sp) # save t2
- addiu $sp, $sp, 4 # prepare to save t3
- sw $t3, ($sp) # save t3
- addiu $sp, $sp, 4 # prepare to save ra
- sw $ra, ($sp) # save ra
- move $a0, $t2 # copy shifted array addr to arg 0
- move $a1, $t3 # copy decr'd array_size to arg 1
- jal array_sum # recursive call
- lw $ra, ($sp) # restore ra
- addiu $sp, $sp, -4 # prepare to restore t3
- lw $t3, ($sp) # restore t3
- addiu $sp, $sp, -4 # prepare to restore t2
- lw $t2, ($sp) # restore t2
- addiu $sp, $sp, -4 # prepare to restore t1
- lw $t1, ($sp) # restore t1
- addiu $sp, $sp, -4 # restore stack pointer
- addu $v0, $t1, $v0 # add array[n] and result of array_sum together into return
- jr $ra # go back to caller
- # sets return value of 0 and jumps back to caller
- # the caller will always be array_sum
- # used when array_size == 0
- array_sum_end:
- move $v0, $t0 # copy 0 to v0
- jr $ra # return to caller
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement