我现在正在学习计算机体系结构课程,我们正在学习基本的 R 型和 I 型指令(另外,这是一个RISC体系结构)等。我似乎无法弄清楚如何优化它代码。
说明:此代码将单词添加到数字数组(由 $s1 指向)中,直到达到零。结果存储在 $t1 中。$t0 保存当前单词。
add $t1, $zero, $zero # Initialize result to zero
again:
lw $t0, 0($s1) # Load the word from the array
beq $t0, $zero, done # Terminate if current word is a zero
add $t1, $t1, $t0 # Add current word to result
addi $s1, $s1, 4 # Point to the next word in the array
beq $t1, $t1, again # Loop again
done:
nop # Do nothing
我很难优化代码。我觉得beq $t1, $t1, again
(因为它总是正确的)是不必要的,但我不知道如何删除它。这是我的尝试,但我现在意识到我的代码不会终止。
add $t1, $zero, $zero # Initialize result to zero
again:
lw $t0, 0($s1) # Load the word from the array
add $t1, $t1, $t0 # Add current word to result
addi $s1, $s1, 4 # Point to the next word in the array
bne $t1, $zero, again # If result is not zero, loop
done:
nop # Do nothing
我从不检查终止零并跳到完成。但是如果我再添加一个检查,那么代码不会和以前一样吗?