Error in Matrix multiplication in MIPS Assembly

I've been staring at this for a while and though it's clear from the errors I'm getting (invalid results, Memory Address Out of Bounds) that something is wrong with my algorithm, it's not immediately obvious what that may be. Any assistance would be greatly appreciated.

The matrix elements are stored as half words in an array. Please forgive the indentions and any other style errors. I just want it to work correctly at the moment.

# matrixMult
#
# Description: This function multiplies
# the two input matrices and places the
# result in matrixC.
#
# register use:
#    a0: address of matrixA
#    a1: address of matrixB
#    a2: address of matrixC
#    t0: counter for row number of matrix C
#    t1: counter for column number of matrix C
#    t2: counter for addend number
#    t3: running total for each element of matrix C
#    t4: number of rows of matrixC
#    t5: number of columns of matrixC
#    t6: number of columns of matrixA/rows of matrixB
#    t7: incrementer for matrix addresses
#    t8: temporary storage
#########################################
matrixMult:
      lw    $t4, 0($s1)         #rows of C
      lw    $t5, 4($s2)         #columns of C
      lw    $t6, 4($s1)         #columns of A/rows of B
      move  $s4, $ra
      la    $a0, matrixA        #loads address of matrixA into a0
      la    $a1, matrixB        #loads address of matrixB into a1
      la    $a2, matrixC            #loads address of matrixC into a2
      addi  $t0, $zero, 0       #initializes t0 to be 0
      forrowsA:
        addi    $t1, $zero, 0       #initializes t1 to be 0
        forcolsB:
        addi    $t2, $zero, 0       #initializes t2 to be 0
          addi    $t3, $zero, 0           #sets the total for this element to 0   
        forcolsA:
          #for matrix A pointer
          mul     $t7, $t5, $t0           # t7 = number of columns * row number
          add     $t7, $t7, $t1           # t7 += column number
          add     $t7, $t7, $t2           # t7 += addend number
          sll     $t7, $t7, 1             # t7 *= 2
          add     $a0, $a0, $t7           # a0 += 2(number of columns * row number + column number)
          lh      $a3, 0($a0)             # integer at memory address a0 saved as a3
          sub     $a0, $a0, $t7           # returns pointer address to the base address

          #for matrix B pointer
          add     $t7, $t2, $t0           # t7 = addend number + row number
          mul     $t7, $t7, $t5           # t7 *= number of columns
          add     $t7, $t7, $t0           # t7 += column number
          sll     $t7, $t7, 1             # t7 *= 2
          add     $a1, $a1, $t7           # a0 += 2(number of columns * row number + column number)
          lh      $t8, 0($a1)             # integer at memory address a0 saved as a3
          sub     $a1, $a1, $t7           # returns pointer address to the base address

          add     $t3, $a3, $t8           #adds A(m,n+x) * B(n+x,m) to the total for C(m,n)

          addi  $t2, $t2, 1         #increments t2
        beq $t2, $t6, nextel    #moves on to next element once one is finished
        jal forcolsA            #repeats totaling loop for an individual element
          nextel:   

          sh      $t3, 0($a2)             #saves the matrix element at the pointer's address
          addi    $a2, $a2, 2             #moves pointer to next element address in matrixC
          addi      $t1, $t1, 1             #increments t1
        beq $t1, $t5, finrow    #moves on to next row once one is finished
          jal   forcolsB            #repeats loop for new element
      finrow:
        addi    $t0, $t0, 1         #increments t0
        beq $t0, $t4, finmult   #ends loop if finished multiplying all elements
        jal forrowsA    

      finmult:
      jr        $s4         # return to the caller  
链接地址: http://www.djcxy.com/p/19214.html

上一篇: 在MIPS中加载双字伪指令

下一篇: MIPS装配中的矩阵乘法错误