昨天发表的帖子,
我正在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 *行+列。但是,当我运行我的代码时,我收到错误寻址错误。我知道这个偏移计算有些问题,因为当我删除它时,程序会正常继续,但我的输出有问题。这个问题源于缺乏偏移计算。如果有人可以帮助我在这里,我将不胜感激。 Stackoverflow通过了解MIPS帮助了我,所以我很感激大家的帮助!感谢
答案 0 :(得分: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
我想我不妨与人们分享这个,所以如果其他人有这个问题,可以相对轻松地解决。感谢所有帮助我解决这个问题的人!