Can you identify my problem with using the stack in this MIPS Assembly snippet? The code…
- User
Can you identify my problem with using the stack in this MIPS Assembly snippet? The code accesses a stack argument, and then it pushes 6 registers, and pops them at the end. Identify the problem:
-------------------------------------------------------
function to multiply two matrix's.
-----
Matrix multiplication formula:
for (i=0; i<DIM; i++)
for j=0; j<DIM; j++)
for (k=0; k<DIM<; k++)
MC(i,j) = MC(i,j) + MA(i,k) * MB(k,j)
end_for
end_for
end_for
-----
Formula for multiple dimension array indexing:
addr of ARRY(x,y) = [ (x * y_dimension) + y ] * data_size
-----
Arguments
$a0 - address matrix a
$a1 - address matrix b
$a2 - address matrix c
$a3 - value, i dimension
stack, ($fp) - value, j dimension
stack, 4($fp) - value, k dimension
YOUR CODE GOES HERE
.globl multMatrix .ent multMatrix multMatrix:
# Stack args -> $fp subu $sp, $sp, 4 sw $fp, ($sp) addu $fp, $sp, 4 # Push saved registers subu $sp, $sp, 24 sw $s0, ($sp) sw $s1, 4($sp) sw $s2, 8($sp) sw $s3, 12($sp) sw $s4, 16($sp) sw $s5, 20($sp) # x Dimension Loop Counters -> saved regs li $s0, 0 # i li $s1, 0 # j li $s2, 0 # k # Matrix addresses -> saved regs move $s3, $a0 # MA address -> s3 move $s4, $a1 # MB address -> s4 move $s5, $a2 # MC address -> s5 iDimLoop: bge $s0, $a3, iEnd # if i >= iDim, end loop jDimLoop: lw $t0, ($fp) bge $s1, $t0, jEnd # if j >= jDim, end loop kDimLoop: lw $t0, 4($fp) bge $s2, $t0, kEnd # if j >= kDim, end loop ########## Inner loop ########## # MA address = $s3 + (($s0 * 4($fp) + $s2) * 4 lw $t0, 4($fp) mul $t4, $s0, $t0 # (rowIndex * colSize) add $t4, $t4, $s2 # (rowIndex * colSize) + colIndex mul $t4, $t4, 4 # ((rowIndex * colSize) + colIndex) * dataSize add $t4, $s3, $t4 # baseAddress + calculatedOffset # $t4 = MA(i,k) # MB address = $s4 + (($s2 * ($fp) + $s1) * 4 lw $t0, ($fp) mul $t5, $s2, $t0 # (rowIndex * colSize) add $t5, $t5, $s1 # (rowIndex * colSize) + colIndex mul $t5, $t5, 4 # ((rowIndex * colSize) + colIndex) * dataSize add $t5, $s4, $t5 # baseAddress + calculatedOffset # $t5 = MB(k,j) # MC address = $s5 + (($s0 * ($fp) + $s1) * 4 lw $t0, ($fp) mul $t6, $s0, $t0 # (rowIndex * colSize) add $t6, $t6, $s1 # (rowIndex * colSize) + colIndex mul $t6, $t6, 4 # ((rowIndex * colSize) + colIndex) * dataSize add $t6, $s5, $t6 # baseAddress + calculatedOffset # $t6 = MC(i,j) # MC(i,j) = MC(i,j) + ( MA(i,k) * MB(k,j) ) lw $t4, ($t4) lw $t5, ($t5) lw $t7, ($t6) mul $t3, $t4, $t5 add $t3, $t3, $t7 sw $t3, ($t6) ########## Inner loop end ########## add $s2, $s2, 1 # k++ j kDimLoop kEnd: add $s1, $s1, 1 # j++ j jDimLoop jEnd: add $s0, $s0, 1 # i++ j iDimLoop iEnd: # Print Matrices A, B move $s0, $a3 # save iDim value # Print A header la $a0, msg_a li $v0, 4 syscall # Print matrix A lw $t0, 4($fp) move $a0, $s3 # matrix A move $a1, $s0 # i dimension move $a2, $t0 # j dimension jal matrixPrint # Print B header la $a0, msg_b li $v0, 4 syscall # Print matrix B move $a0, $s3 # matrix B lw $t0, 4($fp) lw $t1, ($fp) move $a1, $t0 # i dimension move $a2, $t1 # j dimension jal matrixPrint # Push saved registers lw $s0, ($sp) lw $s1, 4($sp) lw $s2, 8($sp) lw $s3, 12($sp) lw $s4, 16($sp) lw $s5, 20($sp) addu $sp, $sp, 24 # Put stack back lw $fp, ($sp) addu $sp, $sp, 4 jr $ra - Assistant
The problem in this MIPS assembly code snippet is related to the way the stack management is handled, specifically when dealing with the saved registers (s5) and the frame pointer ($fp). When we push registers onto the stack, we do it in a sequential manner, and when popping them, we need to do it in the reverse order to ensure that we restore the correct values for each register. However, in this code snippet, there are inconsistencies in the push/pop operations.