2013-10-24 186 views

回答

2

让我们假设你已经从$ T0指向的内存位置在你的话数组的开始:

.data 
mywords: .word 0:100 # 100 words, initialized to 0x0 
.text 
la $t0, mywords 

假设你开始在$ T0的值,我们不希望转移,这里有一些选择:

SLL方法

# $t0 points to base address, $t1 word counter initialized to zero 
loop: 
sll $t2, $t1, 2 # Turn word pointer into a byte pointer 
add $t2, $t0, $t2 # Get pointer to the word we want 
lw $s0, 0($t2) # Load the word into $s0 
addi $t1, $t1, 1 # Point to next word 
# (do something useful here) 
j loop 

add方法,保留$ T0

# $t0 points to base address, $t1 byte counter initialized to zero 
loop: 
add $t2, $t0, $t1 # Get pointer to the word we want 
lw $s0, 0($t2) # Load the word into $s0 
addi $t1, $t1, 4 # Point to next word 
# (do something useful here) 
j loop 

add方法,捣毁$ T0

# $t0 points to base address 
loop: 
lw $s0, 0($t0) # Load the word into $s0 
addi $t0, $t0, 4 # Point to next word 
# (do something useful here) 
j loop 

我们下课只是讨论,其中建设循环的这些方法更有效。我一直在做sll方法,因为我喜欢玩比特......但它看起来效率最低(通过一条指令)。

我想这将取决于你需要保留哪些变量。

  • SLL方法:$ T0告诉你,你开始,$ T1告诉你哪个字 你在,$ t2为划伤

  • add方法1:$ T0告诉你,你开始,$ T1告诉你哪个 字节你在,$ t2为划伤

  • add方法2:不知道你在哪里,而且还能够节省$ T1和T2 $

与往常一样,哪一个“更好”将取决于您的程序需求。