Please fix this code, so the answer is proper, not only 0's: .data
Please fix this code, so the answer is proper, not only 0's:
.data
x: .double 0:16 # initialize x to all zeros
y: .double 0:16 # initialize y to all zeros
z: .double 0:16 # initialize z to all zeros
msg1: .asciiz "Enter 16 values for y and z matrices: \n"
msg2: .asciiz "Resulting x matrix: \n"
.text
.globl main
main:
# Prompt user to enter values for y and z matrices
la $a0, msg1
li $v0, 4
syscall
# Input values for y
la $t0, y
li $t1, 16
li $v0, 7
yloop:
beq $t1, $zero, zloop
syscall
sdc1 $f0, ($t0)
addi $t0, $t0, 8
addi $t1, $t1, -1
j yloop
# Input values for z
zloop:
la $t0, z
li $t1, 16
li $v0, 7
zinner:
beq $t1, $zero, multiply
syscall
sdc1 $f0, ($t0)
addi $t0, $t0, 8
addi $t1, $t1, -1
j zinner
# Multiply y and z matrices and store the result in x
multiply:
la $t0, y
la $t1, z
la $t2, x
# Loop over rows of x
li $t3, 0
rows:
beq $t3, 4, print
# Loop over columns of x
li $t4, 0
cols:
beq $t4, 4, nextrow
# Initialize sum to 0
li $t5, 0
add.d $f4, $f0, $f0
# Loop over common dimension of y and z
common:
beq $t5, 4, addsum
# Load y[i][k] and z[k][j]
lwc1 $f0, ($t0)
lwc1 $f2, ($t1)
# Multiply y[i][k] and z[k][j] and add to sum
mul.d $f0, $f0, $f2
add.d $f4, $f4, $f0
# Move to next element in y and z
addi $t0, $t0, 8
addi $t1, $t1, 8
addi $t5, $t5, 1
j common
# Add sum to x[i][j]
addsum:
sdc1 $f4, ($t2)
addi $t2, $t2, 8
addi $t4, $t4, 1
j cols
# Move to next row of x
nextrow:
addi $t3, $t3, 1
j rows
# Print values of x
print:
la $a0, msg2
li $v0, 4
syscall
la $t0, x
li $t1, 16
li $v0, 3
printloop:
beq $t1, $zero, exit
lwc1 $f12, ($t0)
syscall
addi $t0, $t0, 8
addi $t1, $t1, -1
j printloop
# Exit program
exit:
li $v0, 10
syscall
Trending now
This is a popular solution!
Step by step
Solved in 3 steps