【问题标题】:Bad Addressing in MIPS - Matrix MultiplicationMIPS 中的错误寻址 - 矩阵乘法
【发布时间】:2018-03-26 00:09:51
【问题描述】:

我昨天帖子的后续,

我正在 MIPS 汇编中进行矩阵乘法分配。以下是我计算 A[i][k]*B[k][j] 的最里面的“k”循环中的代码。

    # 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 错误。我知道这个偏移计算一定有问题,因为当我删除它时,程序会正常继续,但我的输出有问题。这个问题源于缺少偏移量计算。如果有人可以在这里帮助我,将不胜感激。 Stackoverflow 在理解 MIPS 方面帮助了我很多,所以我很感谢大家的帮助!谢谢

【问题讨论】:

  • 最初的想法是你需要偏移 i * sizeof(array_type) 而不仅仅是 i,这可能会导致错误的对齐错误。
  • @MichaelDorgan 这就是我假设正在发生的事情 - 我应该用 sll 尝试这个吗?
  • 您是否尝试在调试器中跳过它?还是您真的相信我们更容易阅读代码并在我们的脑海中运行它?那sw $t9, 0($t2) ...如果一切正常,它应该将单词存储在相同的地址,因为lw $t1, 0($t1)从加载?如果是,您真的要计算该地址两次吗?而且你有一些过度使用寄存器和移动,加上注释看起来有点不准确,就像在lw 部分你使用 [i, j, k] 操作(我的意思是在注释中,而不是在代码中),在sw 部分中,只有 [i, j] 被注明。你加载t1却不使用?
  • 我的意思是,你至少在那里写那些 cmets 是好的,但试着重置你的头,然后自己读,而不是在普通的人类重读模式下(因为你的大脑会撒谎对你来说,阅读你确实想在那里写的东西,而不是真正写在那里的东西),但试着重新阅读它,就好像你第一次看到它并且不知道它应该做什么......然后修复它们要清晰准确。同样的事情也适用于代码,甚至更多。因此,请确保在寻找错误时,您专注并准确阅读那里所写的内容,而不是您的意愿。
  • @Ped7g 所以我刚刚用我最近的代码更新了我的帖子,但我仍然收到错误的内存地址。我使用调试器(我正在使用 QtSpim)逐步完成它,当我执行 sll $t1, $t5, 2 时它似乎中止了

标签: c arrays assembly matrix mips


【解决方案1】:

所以事实证明我没有正确计算位偏移 - 我没有将 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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多