Continue the risc-v code from where I left: .data
A: .word 1, 1, -2, 1, 3, 1 #, 2, 4 # Matrix A [n x 2]
B: .word 0, 0, 0, 0, 0, 0 #, 0, 0 # Transpose of Matrix A (A^T) [2 x n]
MatrixC:.word 1,0,0,0,1,0,0,0,1 #The new matrix C, a modification for the Viva
MatrixCinverse:.word 0,0,0,0,0,0,0,0,0 #the inverse of matrix C
C: .word 0, 0, 0, 0,0,0 # Result of A^T x C^-1 | [2 x 3]
T: .word 0,0,0,0 # Result of (A^T x C^-1 x A)| [2 x 2]
D: .word 0, 0, 0, 0 # Result of (A^T x C^-1 x A)^-1 | Inverse of Matrix T | [2 x 2]
E: .word 0, 0, 0, 0, 0, 0 # [(A^T x C^-1 x A)^-1] x (A^T) | [2 x 2] x [2 x n] --> [2 x n] | Matrix D x Matrix B
U: .word 0,0,0,0,0,0 # [(A^T x C^-1 x A)^-1] x (A^T) x C^-1
b: .word 6, 0, 0 # b Matrix | [n x 1]
F: .word 0, 0 # [(A^T x A)^-1] x (A^T) x C^-1 x b | [2 x n] x [n x 1] --> [2 x 1] | Matrix U x Matrix b | m | <-- The resultant matrix
| c |
G: .word 4, 2, 2, 2, 2, 2 # Row information of every matrix
.text
Some Initialization
#x-----------------------------x--------------------------------x
la s0, A # Base Address of Matrix A
la s1, B # Base Address of Matrix B
la s3, MatrixC # Base Address of Matrix C( viva part)
la s4,MatrixCinverse # Base Address of Matrix C inverse ( viva part)
la s2, C # Base Address of C
addi a1, zero, 2
addi t1, zero, 0
Calculating Number of Rows in Matrix A and storing it in t2
add a0, zero, s0 # COPY(Base Address Mat1)
for_count_n: bge a0, s1, for_count_ne
addi a0, a0, 4
addi t1, t1, 1 # Counting the Number of Elements in MatA
j for_count_n
for_count_ne:
div a2, t1, a1 # Total_Rows/Cols = total_element / 2 = t1 / 2 | Total_Rows = total_elements / Total_Columns | Total_Cols = total_elements / Total_Rows
beq ra, zero, skipC
jr ra
skipC:
#x-----------------------------x--------------------------------x
add t2, zero, a2
Finding the tranpose of Matrix A and storing it in Matrix B
#x-----------------------------x--------------------------------x
addi t3, zero, 2 # CONSTANT(t3=2)
add s10, zero, s0 # COPY(Base Address of Matrix A)
add s11, zero, s1 # COPY(Base Address of Matrix B)
transpose:
addi t5,zero,0 # Outter Loop Counter [CounterOutter = t5]
for_outer: bge t5, t3, done_outer # if CounterOutter == 2
addi t6,zero,0 # Reset Inner Counter [InnerCounter = t6]
for_inner: bge t6, t2, done_inner # if InnerCounter == Total_Rows
lw s3, 0(s10) # Load an element from MatA
sw s3, 0(s11) # Store the element from MatA into MatB
addi s10, s10, 8 # Increment through MatA by a factor of two
addi s11, s11, 4 # Increment through MatB by a factor of one
addi t6, t6, 1 # Increment the InnerCounter by 1
j for_inner
done_inner:
add s10, zero, s0 # COPY(Base Address of Matrix A)
addi s10, s10, 4 # Base Address Matrix A + Offset(4) to pick next st of alternate values
addi t5, t5, 1 # Increment OutterCounter
addi t6,zero,0 # Reset InnerCounter
j for_outer
done_outer:
#x-----------------------------x--------------------------------x
Assistant
ordan_inner_loop:
bge t4, zero, gauss_jordan_inner_done
addi t4, t4, -1
beq t1, t4, pivot_row # If it is a pivot row, go to pivot_row
lw t5, 0(t0) # Pick one element from the current row
lw t6, t4t24(t0) # Replace the current element with the result
division_by_zero: # Set the flag to detect division by zero
beq t6, x0, set_flag
j gauss_jordan_inner_loop
set_flag:
li t3, 1
j gauss_jordan_inner_loop
pivot_row: # If it is a pivot row
lw t5, t44(t0) # Pick the pivot element
j gauss_jordan_inner_loop
gauss_jordan_inner_done:
addi t0, t0, t2*4 # Increment the base address to move to the next row
j gauss_jordan_loop
gauss_jordan_exit:
Calculating the inverse of MatrixC and storing it in MatrixCinverse:
la s1, s4 # Base Address of Matrix C inverse
la s2, MatrixCinverse # Base Address of Matrix C inverse in .data
addi t0, zero, 0
addi t3, zero, 9 # 3x3 Matrix
Copying the Matrix C inverse from the stack to the .data section
for2: bge t6,t2, for2_done # if OutterCounter == Total_Rows
lw s3,0(t0) # Pick One Element from B
lw s4,0(t4) # Pick Element from First Column of A
lw s5,4(t4) # Pick Element from Second Column of A
mul s4,s3,s4 # Multiply Element of B by Element from First Column of A
mul s5,s3,s5 # Multiply Element of B by Element from Second Column of A
add s6,s6,s4 # Keep adding the result of Multiplication(s4) in s6
add s7,s7,s5 # Keep adding the result of Multiplication(s5) in s7
addi t0,t0,4 # Increment Base Address of B by 4 to pick next Element
addi t4,t4,8 # Increment Base Address of A by 8 to get next Row
addi t6,t6,1 # Increment InnerCounter by 1
j for2
for2_done:
addi t6,zero,0 # Reset InnerCounter
add t4, zero, s0 # COPY(Base Address of Matrix A)
addi t5,t5,1 # Increment OutterCounter by 1
sw s6, 0(t1) # Store Result Accumulated in s6 in First Column of C
sw s7, 4(t1) # Store Result Accumulated in s6 in Second Column of C
addi t1,t1,8 # Increment Base Address of C by 8 to Store new elements in Next Row
addi s6,zero,0 # Reset s6
addi s7,zero,0 # Reset s7
j for1
for22: bge t6, t2, for22_done # if OutterCounter == Total_Rows
lw s3, 0(t1) # Pick One Element from C
lw s4, 0(t0) # Pick Element from First Column of A
lw s5, 4(t0) # Pick Element from Second Column of A
mul s4, s3, s4 # Multiply Element of C by Element from First Column of A
mul s5, s3, s5 # Multiply Element of C by Element from Second Column of A
add s6, s6, s4 # Keep adding the result of Multiplication(s4) in s6
add s7, s7, s5 # Keep adding the result of Multiplication(s5) in s7
addi t1, t1, 8 # Increment Base Address of C by 8 to get the next row
addi t0, t0, 8 # Increment Base Address of A by 8 to get next Row
addi t6, t6, 1 # Increment InnerCounter by 1
j for22
for22_done:
addi t6, zero, 0 # Reset InnerCounter
add t0, zero, s0 # COPY(Base Address of Matrix A)
addi t5, t5, 1 # Increment OutterCounter by 1
sw s6, 0(t3) # Store Result Accumulated in s6 in First Column of T
sw s7, 4(t3) # Store Result Accumulated in s7 in Second Column of T
addi t3, t3, 8 # Increment Base Address of T by 8 to store new elements in the next row
addi s6, zero, 0 # Reset s6
addi s7, zero, 0 # Reset s7
j for11
la t0, D # Base Address of D
addi t1, zero, 4 # Total number of rows
addi t2, zero, 4 # Total number of columns
la t3, 0x80000000 # Set a flag to detect division by zero
4(t0) # Pick the pivot element from the pivot row
mul t5, t5, t6 # Multiply the element with the pivot element
lw t6, t4
4(t0) # Pick the pivot element from the pivot row
beq t6, x0, division_by_zero # Check if the pivot element is zero
div t5, t5, t6 # Divide the result by the pivot element
sw t5, t4
t2
Check if the flag is set
beq t3, x0, inverse_T_done
If the flag is set, print an error message and exit
la a0, error_message
li a7, 4 # Calling the print_string system call
ecall
for222: bge t6, t1, for222_done # if OutterCounter == Total_Rows
lw s3, 0(t1) # Pick One Element from C
lw s4, 0(t0) # Pick Element from B
mul.s f4, f3, f1
mul.s f5, f4, f2
div.s f6, f