2013-10-24 184 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 $

與往常一樣,哪一個「更好」將取決於您的程序需求。