Okay, you had only one bug and it was a one-liner so you were doing pretty well.
However, there weren't too many comments in the code, so as I was reading/analyzing it, I added some.
I should mention that as I did this, I was able to spot the bug with just visual inspection and review. That is, I was able to find the bug and annotate it along with the fix with this alone.
So, I'd didn't actually need to step the code with the debugger. After applying the fix, it ran correctly the first time.
I recently answered a very similar question here: MIPS questions about writing assembly to call functions on an array that does sum/product on an array
Also, please see my answer here: MIPS linked list Amongst other things, it has a howto on writing clean mips/asm code, based on my own personal experience.
I created two versions of your code. One with minimal changes and just the bug fix. And, another, where I tightened it up and simplified it a bit. Please pardon the gratuitous style cleanups.
Here's the code with the bug/fix annotation:
.data
# NOTE/BUG: due to alignment issues put the .word directives _before_
# the [variable length] .asciiz directives
size: .word 0
to_store: .word 0
sum: .word 0
avg: .word 0
size_prompt: .asciiz "Enter number of elements: "
element_prompt: .asciiz "Enter an element: "
msg_space: .asciiz " "
.text
.globl main
main:
# Prints prompt
la $a0,size_prompt
addi $v0,$0,4
syscall
# Gets input from user
addi $v0,$0,5
syscall
sw $v0,size
addi $t0,$0,0 # Initialise i = 0
# Creates the list
lw $s0,size # get array count
addi $t1,$0,4 # get 4
mult $s0,$t1 # get count * 4
mflo $t2 # get offset
add $a0,$t1,$t2
addi $v0,$0,9
syscall
stack:
beq $t0,$s0,list_sum # done with number input? if yes, fly
# prompt user for list element
la $a0,element_prompt
addi $v0,$0,4
syscall
# read in element value
addi $v0,$0,5
syscall
sw $v0,to_store
lw $t5,to_store
# push element to stack
addi $sp,$sp,-4
sw $t5,0($sp)
addi $t0,$t0,1 # advance array index
j stack
# get sum of array elements
list_sum:
beq $s0,0,average # at end? if yes, fly
lw $t6,sum # get previous sum
lw $t7,0($sp) # get next array element value
addi $sp,$sp,4 # advance array pointer
add $t6,$t7,$t6 # sum += array[i]
sw $t6,sum # store it
addi $s0,$s0,-1 # bump down the _count_
j list_sum
# NOTE/BUG: in the list_sum loop above, s0 is being decremented, so when we
# get here it is _always_ zero
average:
lw $t0,sum # get the sum
# print the sum
li $v0,1
move $a0,$t0
syscall
# NOTE/FIX: restore the count value
lw $s0,size
div $t0,$s0 # divide by count
mflo $t0
sw $t0,avg # store average
li $v0,4
la $a0,msg_space
syscall
# print average
lw $a0,avg
addi $v0,$0,1
syscall
exit:
li $v0,10 # exit program
syscall
Here's my take on cleanup.
mips has many registers, so when they can be used, intermediate results can be preserved.
For example, note that in the summation loop, a different register is used for the decrement, so $s0
remains. Also, I was able to use registers to hold all values that previously used memory locations [except for the stack usage]
.data
size_prompt: .asciiz "Enter number of elements: "
element_prompt: .asciiz "Enter an element: "
msg_space: .asciiz " "
.text
.globl main
main:
# prompt user for number of elements
la $a0,size_prompt
li $v0,4
syscall
# get array count from user
li $v0,5
syscall
move $s0,$v0 # save array count
li $t0,0 # i = 0
stack:
beq $t0,$s0,sumlist # input done (i.e. i >= count)? if yes, fly
# prompt user for list element
la $a0,element_prompt
li $v0,4
syscall
# read in element value
li $v0,5
syscall
# push element to stack
addi $sp,$sp,-4
sw $v0,0($sp)
addi $t0,$t0,1 # i += 1
j stack
# get sum of array elements
# t6 -- sum value
sumlist:
li $t6,0 # sum = 0
move $t4,$s0 # get the count
sumlist_loop:
beqz $t4,average # at end? if yes, fly
# pop element off stack
lw $t7,0($sp) # get next array element value
addi $sp,$sp,4 # advance array pointer
add $t6,$t6,$t7 # sum += array[i]
addi $t4,$t4,-1 # bump down the count
j sumlist_loop
# get average
# t6 -- sum value
# t5 -- average value
average:
# print the sum
li $v0,1
move $a0,$t6 # get the sum
syscall
div $t6,$s0 # compute sum / count
mflo $t5 # retrieve it
# output a space
li $v0,4
la $a0,msg_space
syscall
# print average
move $a0,$t5
li $v0,1
syscall
exit:
li $v0,10 # exit program
syscall