Question
In MIPS MARS, take your matrix multiply program from Lab#4 and unroll the inner loop a. Completely (no iterations of the inner loop) b. Two
In MIPS MARS, take your matrix multiply program from Lab#4 and unroll the inner loop
a. Completely (no iterations of the inner loop) b. Two (2) times (two iterations of the unrolled loop)
Make sure to use different registers for each multiply-add and its associated variables so that the computations (multiply-adds) could all be done in parallel (that is one of the primary reasons for unrolling, the other being to eliminate some of the loop overhead branches and counters).
Compare the program length, register usage and conceptual complexity.
Turn in two separate programs, one for a, and one for b
My lab 4:
.data
A: .double 0:16 # Intilaize all values with 0s
B: .double 0:16
C: .double 0:16
prompt1:.asciiz "Enter 16 values of A: "
prompt2:.asciiz "Enter 16 values of B: "
message:.asciiz "Matrix C = Matrix A x Matrix B= "
space: .asciiz " "
newline: .asciiz " "
.text
main:
li $v0,4
la $a0,prompt1
syscall
la $a0,A # pass address of the matrix A
li $a1,16 # pass no. of elements
jal inputmatrix # call inputmatrix
la $a0,A # pass base address of A
li $a1,4 # pass row size
li $a2,4 # pass column size
jal printmatrix
li $v0,4
la $a0,prompt2
syscall
la $a0,B # pass address of the matrix B
li $a1,16 # pass no. of elements
jal inputmatrix # call inputmatrix
la $a0,B # pass base address of the matrix B
li $a1,4 # pass row size
li $a2,4 # pass column size
jal printmatrix
li $a0,4 # pass row and col size to multiplymat
la $a1,A # pass base address of A, B, C
la $a2,B
la $a3,C
jal multiplymat
#Print resultant matrix C
li $v0,4
la $a0,message
syscall
la $a0,C # pass address of the matrix c
li $a1,4 # pass row size
li $a2,4 # pass column size
jal printmatrix
#exit
li $v0, 10
syscall
inputmatrix:
li $t0,0 # i=0
mul $t1,$a1,8 # size of the matrix in bytes
move $t2,$a0 # save address of matrix
loop: bge $t0,$t1,return
li $v0,7 # read double value
syscall
s.d $f0,($t2) # save the into matrix cell
li $v0,4 # print new line
la $a0,newline
syscall
addi $t0,$t0,8 # i++
addi $t2,$t2,8 # update the next cell
j loop # repat loop
return: jr $ra
#Procedure to multiply matrices
multiplymat:
move $t0,$a0 #size(row size/column size)
li $t1,0 #i=0
loop1: bge $t1,$t0,multover #Exit the loop when i>size
li $t2,0 #j=0
loop2: bge $t2,$t0,nextloop1 #Exit the loop when j>size
li $t3,0 #k=0
loop3: bge $t3,$t0,nextloop2 #Exit the loop when k>size
li $t4,8 #Data size
mul $s0,$t1,$t0 #Finding index for C[i,j]
add $s0,$s0,$t2
mul $s0,$s0,$t4
add $s0,$s0,$a3
mul $s1,$t1,$t0 #Finding index for A[i,k]
add $s1,$s1,$t3
mul $s1,$s1,$t4
add $s1,$s1,$a1
mul $s2,$t3,$t0 #Finding index for B[k,j]
add $s2,$s2,$t2
mul $s2,$s2,$t4
add $s2,$s2,$a2
l.d $f0,($s0) #C[i,j]
l.d $f2,($s1) #A[i,k]
l.d $f4,($s2) #B[k,j]
mul.d $f2,$f2,$f4 #A[i,k]*B[k,j]
add.d $f0,$f0,$f2 #C[i,j]+A[i,k]*B[k,j]
s.d $f0,($s0) #C[i,j]=C[i,j]+A[i,k]*B[k,j]
addi $t3,$t3,1 #k++
j loop3
nextloop2:
addi $t2,$t2,1 #j++
j loop2
nextloop1:
addi $t1,$t1,1 #i++
j loop1
multover: jr $ra
#Procedure to print the matrix of double precission values
printmatrix :
li $t0,0 #i=0
move $t5,$a0 #$t5=address of the matrix
outerloop: bge $t0,$a1,printingover #Exit the loop when i>Szie
li $t1,0 #j=0
innerloop: bge $t1,$a2,nextiter #Exit the loop when j>Szie
mul $t3,$t0,$a2 #Find the address of A[i,j]
add $t3,$t3,$t1
li $t4,8
mul $t3,$t3,$t4
add $t3,$t3,$t5
li $v0,3 #To print double value
l.d $f12,($t3) #load A[i,j] to $f12
syscall #Print A[i,j]
li $v0,4 #Print space between two values
la,$a0,space
syscall
addi $t1,$t1,1 #j++
j innerloop
nextiter: li $v0,4
la,$a0,newline #Print new line(to print values in the next line)
syscall
addi,$t0,$t0,1 #i++
j outerloop
printingover: jr $ra
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started