2011-04-19 59 views
0

我想了解MIPS中的數組,並且在這樣做的時候真的很難。是我與這個樣子的在C++中工作的陣列:幫助打印MIPS數組5 - 15

int array [10]; 
void main(){ 
    int i; 
    for(i = 0; i < 10; i++){ 
    array[i] = i + 5; 
    } 
    for(i = 0; i < 10; i++){ 
    cout << array[i] << endl; 
    } 
    return 0; 
} 

我有這樣的MIPS到目前爲止的代碼,但它有錯誤,打印所有0的:

.data 
array: .space 40 
    .globl main 
    .text 
<code>main: 
li $t0, 0    # i=0 
li $t4, 0    # i=0 for print loop 
li $s1, 10    # $s1 = 10 
la $a1, array   # loads array to $a1 

LOOP: 
bge $t0, $s1, print # branch to print if i<10 
addi $t1, $t0, 5  # i+5 

add $t2, $t1, $t1  # 2 * i 
add $t2, $t2, $t2  # 4 * i 
add $t2, $t2, $a1  # $t2=address of array[i] 
sw $t3, 0($t2) 
addi $t0, $t0, 1  # i++ 
j LOOP     # jumps to top of loop 

print: 
bge $t4, $s1, exit  # branch to exit if i < 10 
add $t5, $t4, $t4  # 2 * i 
add $t5, $t5, $t5  # 4 * i 
add $t5, $t5, $a1  # $t2=address of array[i] 
sw $t6, 0($t5) 

li $v0, 1 
move $a0, $t6   #moves value to $a0 to be printed 
syscall 

addi $t4, $t4, 1  # i++ 
j print     # jumps to top of print 


exit: 
li $v0, 10      #load value for exit 
syscall       #exit program 

回答

1

我看到3個錯誤:

add $t2, $t1, $t1  # 2 * i 

應該

add $t2, $t0, $t0  # 2 * i 

因爲$t1 = $t0 + 5

其次,

sw $t3, 0($t2) 

應該

sw $t1, 0($t2) 

最後,

sw $t6, 0($t5) 

應該

lw $t6, 0($t5)