MIPS 中的错误寻址 - 矩阵乘法

Bad Addressing in MIPS - Matrix Multiplication

我昨天 post 的跟进,

我正在 MIPS 汇编中处理矩阵乘法赋值。以下是我最里面的 'k' 循环中的代码,我在其中计算 A[i][k]*B[k][j]。

    # compute A[i][k] address and load into $t3
    # A[i][k] = A+4*((4*i) + k)
    sll $t3, $t5, 2         # Store 4*i in $t3
    addu $t3, $t3, $t7       # Adds $t3 to k
    sll $t8, $t3, 2         # Computes 4*($t3) = 4*(4*i+k) by temporarily storing the product in $t8
    move $t3, $t8           # Stores 4*($t3) into $t3
    addu $t3, $t3, $a0       # Adds A to $t3
    lw $t3, 0($t3)

    # compute B[k][j] address and load into $t4
    # B[k][j] = B+4*((4*k) + j)
    sll $t4, $t7, 2          # Stores 4*k in $t4
    addu $t4, $t4, $t6       # Adds $t4 to j
    sll $t8, $t4, 2          # Computes 4*($t4) = 4*(4*k+j) by temporarily storing the product in $t8
    move $t4, $t8            # Stores 4*($t4) into $t4
    addu $t4, $t4, $a1       # Adds B to $t4
    lw $t4, 0($t4)

    # multiply

    multu $t3, $t4
    mflo $t9


    # insert the multiplied value into $a2
    sll $t1, $t5, 2          # Multiplies $t5 by 4 and stores the value in $t1
    addu $t1 $t1, $t6        # Adds $t1 and $t6 (j) for the final bit offset
    move $t2, $a2            # Stores $a2's base register in $t2 temporarily
    addu $a2, $a2, $t1       # Adds bit offset to $a2

    sw $t9, 0($a2)           # Store $t9 into its respective location in with offset from $a2

    move $a2, $t2            # Restores base address back into $a2

    # increment k and jump back or exit
    addi $t7, $t7, 1
    j kLoop

据我所知,乘法工作正常。作为参考,我的 $t5 - $t7 用于我的 i、j 和 k。

我的下一步是将存储在 $t9 中的结果插入到位于寄存器 $a2 的结果数组中。为了将 $t9 存储在 $a2 中的正确位置,我需要计算偏移量。我知道偏移量是 $a2 * 行 + 列。但是,当我 运行 我的代码时,我收到 Bad Addressing 错误。我知道这个偏移量计算一定有问题,因为当我删除它时,程序会正常继续,但我的输出有问题。这个问题源于缺少偏移量计算。如果有人可以在这里帮助我,我将不胜感激。 Whosebug 在理解 MIPS 方面帮助了我很多,所以我感谢大家的帮助!谢谢

结果证明我没有正确计算位偏移 - 我没有将 4*i+j 乘以 4 以获得可以寻址的 4 的倍数。进行更改后,我的 k 循环的乘法部分现在看起来像这样:

# multiply
multu $t3, $t4
mflo $t9


# insert the multiplied value into $a2
sll $t1, $t5, 2          # Multiplies $t5 (i) by 4 and stores the value in $t1
addu $t1 $t1, $t6        # Adds $t1 and $t6 (j) for the col offset
sll $t1, $t1, 2          # Multiplies $t1 by 4 and stores value back into $t1 for final bit offset
move $t2, $a2            # Stores $a2's base register in $t2 temporarily
addu $t2, $t2, $t1       # Adds bit offset to $t2 / $a2's temp alias


lw $t1, 0($t2)           # Overwrites $t1 with the value currently at $t2
addu $t9, $t9, $t1       # Adds the current $t2 val ($t1) to $t9

sw $t9, 0($t2)           # Store $t9 into its respective location in with offset from $a2

我想我不妨与其他人分享这个,这样如果其他人有这个问题,它可以相对容易地解决。感谢所有帮助我解决这个问题的人!