2013-09-26 36 views
0

我有問題在一個循環中總結一個數組的內容。總結一個數組的內容

loop3:  
    beq $t5, $t1, loop4  #if $t5 is equal to $t1, then goto exit 

    lw $t6, 0($s0)  #load contents of $s0 to $t6 
    add $t6, $t6, $t6  #sums the contents 

    addi $s0, $s0, 4  #increments pointer of pArry 
    add $t5, $t5, 1  #increments counter of loop3 
    j loop3 

回答

1

你不是因爲你在每次迭代的開始與當前數組元素覆蓋$t6所有元素求和:lw $t6, 0($s0) #load contents of $s0 to $t6

負載電流元到一些其他的(免費),而不是註冊:

lw $t7, 0($s0)  #load contents of $s0 to $t7 
add $t6, $t6, $t7 #sums the contents 

確保循環開始前清除$t6

+0

謝謝你的提示。我確實感覺錯了。 – Kevin