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
4

1 回答 1

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在循环开始之前清除。

于 2013-09-26T07:09:30.017 回答