'RISC-V: Multiplication of two matrices
---update---
I now have kind of a MMA function but the code still doesn't work. And i know that for example:
Mul X28, X6(x11), X7(x12)
Ld X29, X5(x10)
is the wrong syntax and the code would not work but i dont know how i should write it.
When x11 = 1600 then x6(x11) with x6=8 should be 1608 and next with x6=16 it should be 1616 etc.
I think i have to use the stack pointer x2 but i don't know how this works.
---old---
I have to multiply two matrices. The dimensions of the matrices should be variable. I am an absolute beginner with RISC-V and just confused how I can multiply two matrices. I understood how to do the multiplication (Binary Arithmetic) of two numbers on paper but I find it confusing to convert it into a code using two matrices. The MMA function is the function which should multiply the two matrices.
I would really appreciate if someone could help me! Thank you!
MAIN:
# MAIN stack
addi x2, x2, -64
sd x8, 0(x2)
addi x8, x2, 64
sd x1, -8(x8)
sd x10, -16(x8)
sd x11, -24(x8)
sd x12, -32(x8)
sd x13, -40(x8)
sd x14, -48(x8)
sd x15, -56(x8)
# Address of matrix A = 1600
# Address of matrix B = 2400
# Address of matrix C = 3200
addi x10, x0, 1600
slli x10, x10, 1 # x10 = 3200
addi x11, x0, 1600 # x11 = 1600
addi x12, x0, 1200
slli x12, x12, 1 # x12 = 2400
# Matrix A = M (rows) * N (cols)
# Matrix B = N (rows) * K (cols)
# Matrix C = M (rows) * K (cols)
# M = 3, N = 4, K = 5
addi x13, x0, 3
addi x14, x0, 4
addi x15, x0, 5
# Call subroutine MMA(C, A, B, M, N, K)
# C += A * B
jal x1, MMA
# Clear MAIN stack
ld x1, -8(x8)
ld x10, -16(x8)
ld x11, -24(x8)
ld x12, -32(x8)
ld x13, -40(x8)
ld x14, -48(x8)
ld x15, -56(x8)
ld x8, 0(x2)
addi x2, x2, 64
5# End of the program (x1 == 0)
jalr x0, 0(x1)
MMA:
# MMA function that calcualtes C += A * B.
#clear registers
Sd X0 0(X5)
Sd X0 0(X6)
Sd X0 0(X7)
Sd X0 0(X16)
Sd X0 0(X17)
Sd X0 0(X18)
Sd X0 0(X19)
Sd X0 0(X28)
Sd X0 0(X29)
Sd X0 0(X30)
Sd X0 0(X31)
#LOOP1 for multiplying and summing
LOOP1:
Bed X16, X14, LOOP2 #LOOP2 if x16 = # of columns in Matrix A
Addi X16, X16, 1 #x16 + 1
Mul X28, X6(x11), X7(x12) #X28 = X6(x11) * x7(x12)
Ld X29, X5(x10) #load value of adress x5(x10) in x29
Add X29, X29, X28 #Sum = Sum + Mul
sd X29, X5(x10) #store value of X29 in address x5(x10)
addi X6, X6, 8 #x6 + 8
Mul X30, X15, 8 #x30 = # of columns in matrix B * 8
Addi X7, X7, X30 #x7 + x 30
Beq X0, X0, LOOP1
#LOOP2 to go to next column in matrix B
LOOP2:
Bed X17, X15, LOOP3 #LOOP3 if x17 = # of columns in matrix B
addi X5, X5, 8 #x5 + 8
sd X0, X16 #reset x16
addi X17, X17, 1 #x17 + 1
mul X30, X31, 8 #x30 = current row * 8
mul X6, X30, X14 #x6 =x30* #of column in matrix A
mul X7, X17, 8 #x7 = current column * 8
Bne X17, X15, LOOP1 #LOOP1 if x17 != #of columns in matrix B
Beq X0, X0, LOOP2
#LOOP3 to go to next row in matrix A
LOOP3:
beq X31, X13, SKIP #SKIP if x31 = # of rows in matrix A
Sd X0, X17 #reset x17
Sd X0, X7 #reset x7
addi X31, X31, 1 #x31 + 1
mul X30, X31, 8 #x30 = current row * 8
Mul X6, X30, X14 #x6 =x30* #of columns in matrix A
Bne X31, X13, LOOP1 #LOOP1 if x31 != # of rows in matrix A
Beq X0, X0, LOOP3
SKIP:
jalr x0, 0(x1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|