MIPS assembly (60pts 3. Implement a Vector-Matrix multiply code in MIPS assembly
ID: 3599860 • Letter: M
Question
MIPS assembly (60pts 3. Implement a Vector-Matrix multiply code in MIPS assembly. The skeleton code is provided. The input data are also embedded to the skeleton code (do not modify). Fill your code between the following two comment lines in the skeleton code. Do not change any other code in the skeleton code. You can test and debug your code in MARS simulator. The MARS simulator and the tutorial are posted in the Homework 4 entry of Canvas. Your CODE HERE You can add new code here. End cODE Vector-Matrix Multiplication: Vector-Matrix multiplication is a binary operation that takes a pair of a vector and a matrix, and produces another vector. The pseudo code and the graphical projection of the vector-matrix multiplication of a vector with N entries and a N×N square matrix is like below: for (i-0; iExplanation / Answer
.data
matrixA: .word 1,3,2,1,3,2,1,3,2
matrixB: .word 0,1,2,0,1,2,0,1,2
output: .word 0:9 #9 elements all initialized to 0
msg: .asciiz "The output matrix elements are : "
comma: .asciiz ", "
.text
#multiply the 2 matrices using the logic
#for(int i = 0 ; i < 3; i ++)
# for( int j = 0; j < 3; j++)
# {
# sum = 0
# for(int k = 0; k < 3; k++)
# {
# sum += A[i][k] * B[k][j];
# }
# output[i][j] = sum
# }
li $t0, 0 #row number, i
OuterLoop:
bge $t0, 3, EndOuterLoop #if row >= 3
li $t1, 0 #col number , j
InnerLoop1:
bge $t1, 3, EndInnerLoop1 #if col >= 3
li $t2, 0 # k
li $t6, 0 #sum
InnerLoop2:
bge $t2, 3, EndInnerLoop2
#get the element A[i][k] by calculating offset i * 3 + k, store t4 = A[i][k]
mul $t3, $t0, 3
add $t3, $t3, $t2
mul $t3, $t3, 4 #multiply by 4 since each int takes 4 bytes
lw $t4, matrixA($t3)
#get the element B[k][j] by calculating offset k * 3 + j, store t5 = B[k][j]
mul $t3, $t2, 3
add $t3, $t3, $t1
mul $t3, $t3, 4 #multiply by 4 since each int takes 4 bytes
lw $t5, matrixB($t3)
#t4 = A[i][k] * B[k][j]
#sum = sum + t4
mul $t4, $t4, $t5
add $t6, $t6, $t4
addi $t2, $t2, 1
b InnerLoop2
EndInnerLoop2:
#calculate offset for output element and store ans output[i][j] = sum
mul $t3, $t0, 3
add $t3, $t3, $t1
mul $t3, $t3, 4 #multiply by 4 since each int takes 4 bytes
sw $t6, output($t3) #store sum into output
#incremetn col
addi $t1, $t1, 1
b InnerLoop1
EndInnerLoop1:
#increment row
addi $t0, $t0,1
b OuterLoop
EndOuterLoop:
#display the output matrix
la $t0, output #base address of output
li $t1, 1 #counter
DisplayLoop:
bgt $t1, 9, Exit #displayed 9 elements?
#display the element from output matrix
li $v0, 1
lw $a0, 0($t0)
syscall
#show a comma
li $v0, 4
la $a0, comma
syscall
#increment counter
addi $t1, $t1, 1
add $t0, $t0, 4
b DisplayLoop
Exit:
#exit
li $v0, 10
syscall
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.