2012-11-06 82 views
2

嘿,我需要爲所有的i和j做C [i] [j] = A [i] [j] + B [j] [i],大小爲16 x 16在二維數組中。在MIPS中穿越2D陣列

這是我的代碼的主要部分(如下所示)。

當我在SPIM中運行此代碼時,我收到一條異常,在行「lw $ t4,0($ t4)#值B [j] [i]」處,表示數據/堆棧的地址不正確,讀取爲

當我檢查存儲在每個寄存器中的值時,我意識到i == 0x1,但是j達到了0xbf0! (這是3056)

我不知道爲什麼會發生這種情況,因爲我的j應該只是從0增加到15.幫助我!

la $t0, A     # $t0 represents start address of A[i][j] 
la $t1, B     # $t1 represents start address of B[i][j] 
la $t2, C     # $t2 represents start address of C[i][j] displacement of A will be the same as C       

addi $t3, $zero, 16   # set maximum iteration to be 16 
addi $t5, $zero, 0  # set i = 0 
addi $t6, $zero, 0  # set j = 0 

loopi: 
jal loopj    # starts inner loopj 
addi $t5, $t5, 1  # i++ 
bne $t3, $t5, loopi # continue loopi if i < 16 
j  finish    

loopj: 
sll $t7, $t5, 4  
add $t7, $t7, $t6 
sll $t7, $t7, 2  # 4 * ((i * 16) + j) 
add $t9, $t7, $t0  # address of A[i][j] 
lw  $t9, 0($t9)  # value of A[i][j] 

sll $t4, $t6, 4  
add $t4, $t4, $t5 
sll $t4, $t4, 2  # 4 * ((j * 16) + i) 
add $t4, $t4, $t1  # address of B[j][i] 
lw  $t4, 0($t4)  # value of B[j][i] 

add $t4, $t4, $t9  # A[i][j] + B[j][i] 

add $t7, $t7, $t2  # address of C[i][j] 
sw  $t4, 0($t7)  # store answer into C[i][j] 

addi $t6, $t6, 1  # j++ 
bne $t3, $t6, loopj # continue loopj if j < 16 
jr $ra 

finish:  

回答

2

你忘了復位j每次進入loopi時間爲零時,第一loopj後否則在loopj零無法啓動......

要解決它,你可以移動阿迪這設置$t6(持有j)標籤loopi後:

loopi: 
    addi $t6, $zero, 0  # set j = 0 
    jal loopj    # starts inner loopj 
    ... 
+0

哦,謝謝,我忽略了!我的問題現在解決了。你是一個很好的幫助。 – user1802890