gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:07:31 24 4
gpt4 key购买 nike

我昨天帖子的后续内容,

我正在 MIPS Assembly 中处理矩阵乘法作业。以下是我最里面的“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 * 行 + 列。但是,当我运行代码时,出现错误寻址错误。我知道这个偏移计算一定有问题,因为当我删除它时,程序会正常继续,但我的输出有问题。这个问题源于缺少偏移计算。如果有人可以在这里帮助我,我将不胜感激。 Stackoverflow 在理解 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

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

关于c - MIPS 中的错误寻址 - 矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46736977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com