Uploaded by 16arms

matrixMult

advertisement
#12/8/23
#ECE331 Project 2
#this program multiplies two matricies
#================Func Args=======================
# a0 pointer to matrix A
# a1 pointer to matrix B
# a2 pointer to resultant matrix C
# a3, Rowsize of A, columnsize of B
# a4, ColumnSize of A
#===============================================
#================Return registers=======================
# a0 &C
#=======================================================
#================Variables=======================
# t0 row itterator i
# t1 column itterator j
# t2 row vector &A[i]
# t3 column vecotr &B[][j]
# t4 &C[i,j] # t5 temp storage for *A
# t6 temp storage for *B
# a6 used for calculations
#================================================
# t0, row itterator j
# t1, column itterator i
# t2, B matrix column itterator
# t3, temp storage for A
# t4, temp storage for B
# t5, temp storage for C
# a3 = &A[i][j]
# a4 = &B[i][j]
# a5 = &C
# variables needed, &A, &B, &C, RowSize, ColumnSize
# t0 i, t1 j, t2 &A[i] t3 &B[][j], t4 &C[i,j] t5 *A t6 *B a6 *C
# a6, mul ITER k
TESTSTART:
addi a3, x0, 5 # init rowsize
addi a4, x0, 6 # init column size
addi s0, x0, 30 # totalsize
addi t0, x0, 4 mv s3, s0
mul s0, s0, t0 # stack offset
addi sp, sp -4 sw x0, 0(sp) # insert null byte on stack for end of array
sub sp, sp, s0 # change stack
add a2, sp, x0 # init C on stack
mv t2, sp
mv t0, x0
mv t1, x0
L0: # load C
sw t1, 0(t2) # initialize C
addi t2, t2, 4
addi t0, t0, 1
bne t0, s3, L0
li t1, 1
addi sp, sp -4 sw x0, 0(sp) # insert null byte on stack for end of array
sub sp, sp, s0 # change stack
mv a1, sp # init B on stack
mv t2, sp
mv t0, x0
L1: # load B
sw t1, 0(t2) # store 1 in A
addi t2, t2, 4 # increment frame
addi t0, t0, 1 # increment check
bne t0, s3, L1 # if we did enough continue
addi sp, sp -4 sw x0, 0(sp) # insert null byte on stack for end of array
sub sp, sp, s0 # change stack
mv a0, sp # init A on stack
mv t2, sp
mv t0, x0
L2: # load A
sw t1, 0(t2) # store 1 in A
addi t2, t2, 4 # increment frame
addi t0, t0, 1 # increment check
bne t0, s3, L2 # if we did enough continue
mv s0, a0
mv s1, a1
mv s2, a2
jal ra, FUNCSTRT
li a7, 11 # print cahr
li a0, 10 # newline character
ecall
li a7, 11
li a0, 65
ecall
li a0, 58
ecall
li a0, 32
ecall
mv a0, s0
jal ra, PRINTARR
li a7, 11 # print cahr
li a0, 10 # newline character
ecall
li a7, 11
li a0, 66
ecall
li a0, 58
ecall
li a0, 32
ecall
mv a0, s1
jal ra, PRINTARR
li a7, 11 # print cahr
li a0, 10 # newline character
ecall
li a7, 11
li a0, 67
ecall
li a0, 58
ecall
li a0, 32
ecall
mv a0, s2
jal ra, PRINTARR
li a7, 11 # print cahr
li a0, 10 # newline character
ecall
j END
FUNCSTRT:
addi sp, sp, -8
sw s0, 0(sp) # store s0
sw s1, 4(sp)
addi t0, x0, 4
mul a3, a3, t0
mul a4, a3, a4 add t0, x0, x0 # init i
add t1, x0, x0 # init j
SETUPMULT:
add t2, a0, t0 # t2 = &A[i][]
add t3, a1, t1 # t3 = &B[][j]
add t4, a2, t0 # t4 = &C[i][]add row offset to C add t4, t4, t1 # t4 = &C[i][j]
li s0, 0 # reset result li a6, 0 # reset k
li t5, 0 # reset t5
MULT:
lw t5, 0(t2) # A[i][k]
lw t6, 0(t3) # B[k][j]
mul t5, t5, t6
addi t2, t2, 4 # t2 = &A[i][k+1]
add t3, t3, a3 # t3 = &B[k+1][j]
addi a6, a6, 4 # increment k by 1
add s0, s0, t5 # add A*B to C
mv s1, a0
mv a0, s0
li a7 1
ecall
li a0, 32
li a7, 11
ecall
mv a0, s1
bne a6, a3, MULT # while k!=rowsize MULT
sw s0, 0(t4)
addi t1, t1, 4 # increment j
bne t1, a3, SETUPMULT
add t0, t0, a3 # increment t0 by RowSize offset
add t1, x0, x0 # reset j to 0
bne t0, a4, SETUPMULT
lw s0, 0(sp) # load s0
lw s1, 4(sp)
addi sp, sp, 8
jalr x0, ra, 0
PRINTARR:
mv t0, x0
addi t1, x0, 30
mv t2, a0
mv t5, a0
NEXT:
li a7, 1
lw a0, 0(t2)
addi t2, t2, 4
addi t0, t0, 1
ecall
li a7, 11 #print char
li a0, 44 # ascii comman
ecall
li a0, 32 # ascii space char
ecall
bne t0, t1, NEXT mv a0, t5
jalr x0, ra, 0
END: nop
Download