# data segment .data Z: .word 2, 4, 6, 8, 10, 12, 14, 16, 18, 0 #First array\'s
ID: 3768529 • Letter: #
Question
# data segment
.data
Z: .word 2, 4, 6, 8, 10, 12, 14, 16, 18, 0 #First array's elements
sumZ: .word 0
.text
main:
la $t3, Z # put address of Z into $t3
lw $t1, sumZ
Loop: lw $s6, ($t3) # load the first value of array
bne $s6, 0, Exit # branch to exit if $s6 <> 0
add $t1, $t1, $s6
sll $t3, $t3, 2 # move to next address
j Loop
Exit:
sw $t1, sumZ
li $v0, 1
syscall
jr $ra
This is MIPS.
I seem to get the output: 0.
I do not know why it is not adding up the values in the array. Can someone help me with this?
Explanation / Answer
I think instead of bne you should use beq i.e. it should jump to exit when $s6 is equal to 0
# data segment
.data
Z: .word 2, 4, 6, 8, 10, 12, 14, 16, 18, 0 #First array's elements
sumZ: .word 0
.text
main:
la $t3, Z # put address of Z into $t3
lw $t1, sumZ
Loop: lw $s6, ($t3) # load the first value of array
beq $s6, 0, Exit # branch to exit if $s6 <> 0
add $t1, $t1, $s6
sll $t3, $t3, 2 # move to next address
j Loop
Exit:
sw $t1, sumZ
li $v0, 1
syscall
jr $ra
DOES THIS WORKS?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.