0

I have some codes to sum array 50 times. Now I'm asked to reduce this code by at least 1 instructions. All I could think of was to increase this loop to 100 times and then add one element per loop. But this would increase the instruction counts a lot.

      addi $t1, $0, 50 
LOOP: lw $s1, 0($s0) 
      add $s2, $s2, $s1 
      lw $s1, 4($s0) 
      add $s2, $s2, $s1 
      addi $s0, $s0, 8 
      subi $t1, $t1, 1 
      bne $t1, $0, LOOP

Note that you can only rewrite this code, not write another sumArray algorithm

4

1 回答 1

1

通过“组合”地址和循环计数器,减少了一条指令,从而将数组向后求和:

      addi $t1, $s0, 400   # $t1 = $s0 + 50*2*sizeof(word)
LOOP: lw $s1, -4($t1) 
      add $s2, $s2, $s1 
      lw $s1, -8($t1) 
      add $s2, $s2, $s1 
      sub $t1, $t1, 8 
      bne $t1, $s0, LOOP
于 2013-10-24T07:43:50.363 回答