2

嘿伙计们,我需要对所有 i 和 j 执行 C[i][j] = A[i][j] + B[j][i],二维数组中的大小为 16 x 16。

这是我的代码的主要部分(如下所示)。

当我在 SPIM 中运行此代码时,我在“lw $t4, 0($t4) # value of 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:     
4

1 回答 1

2

j您每次输入时都忘记重置为零loopi,否则在第一次之后loopj不会从零开始loopj...

要修复它,您可以在 label 之后移动设置$t6(包含j)的 addi loopi

loopi:
  addi   $t6, $zero, 0     # set j = 0
  jal    loopj             # starts inner loopj
   ...
于 2012-11-06T14:21:32.727 回答