Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

In this RISC-V code, I want you to multiply the matrix D with matrix B and store the…

  1. User

    In this RISC-V code, I want you to multiply the matrix D with matrix B and store the result in matrix E. .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

    #x-----------------------------x--------------------------------x Row_Col_Calc:

    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

    Calculating the inverse of MatrixC and storing it in MatrixCinverse:

    #x-----------------------------x--------------------------------x

    la s0, D # Base Address of Matrix D la s1, s3 # Base Address of Matrix C la s2, s4 # Base Address of Matrix C inverse

    flw f0, 0(s1) flw f1, 4(s1) flw f2, 8(s1) flw f3, 12(s1)

    fmul.s f4, f0, f3 fmul.s f5, f1, f2 fsub.s f6, f4, f5

    fdiv.s f7, f3, f6 fsw f7, 0(s2)

    fneg.s f8, f1 fdiv.s f9, f8, f6 fsw f9, 4(s2)

    fneg.s f10, f2 fdiv.s f11, f10, f6 fsw f11, 8(s2)

    fneg.s f12, f0 fdiv.s f13, f12, f6 fsw f13, 12(s2)

    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

    copy_loop: bge t0, t3, copy_exit flw f14, 0(s1) fsw f14, 0(s2) addi s1, s1, 4 addi s2, s2, 4 addi t0, t0, 1 j copy_loop copy_exit: #x-----------------------------x--------------------------------x

    Calculating A^T x MatrixCinverse and storing it in C

    #x-----------------------------x--------------------------------x

    add t0, zero, s1 # COPY(Base Address of Matrix B) add t4, zero, s0 # COPY(Base Address of Matrix A) add t1, zero, s2 # COPY(Base Address of Matrix C)

    multiply:

    addi t5, zero, 0 # OutterCounter = t5 addi t6, zero, 0 # InnerCounter = t6

    for1: bge t5,t3, for1_done

    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
    

    for1_done: nop

    #x-----------------------------x--------------------------------x

    Calculating A^T x MatrixCinverse x A and storing it in T:

    Multiply C with A to get the final result

    add t0, zero, s0 # COPY(Base Address of Matrix A) add t1, zero, s2 # COPY(Base Address of Matrix C) add t2, zero, s0 # COPY(Base Address of Matrix A) add t3, zero, s3 # COPY(Base Address of Matrix T)

    Nested loops for multiplying each element of C with each row of A

    addi t5, zero, 0 # OutterCounter = t5 addi t6, zero, 0 # InnerCounter = t6

    for11: bge t5, t2, for11_done

    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
    

    for11_done: nop #x-----------------------------x--------------------------------x

    Calculate the result of T^-1 that is (A^T x C^-1 x A)^-1 and store the result in D

    Calculate the inverse of T

    Copy T to D

    la t0, T # Base Address of T la t1, D # Base Address of D addi t2, zero, 4 # Total number of rows in T addi t3, zero, 4 # Total number of columns in T

    copy_T_to_D_loop: bge t2, zero, copy_T_to_D_exit addi t2, t2, -1 addi t4, zero, t3 copy_T_to_D_inner_loop: bge t4, zero, copy_T_to_D_inner_done addi t4, t4, -1 lw t5, 0(t0) addi t0, t0, 4 sw t5, 0(t1) addi t1, t1, 4 j copy_T_to_D_inner_loop copy_T_to_D_inner_done: j copy_T_to_D_loop copy_T_to_D_exit:

    Create the identity matrix I

    la t0, D # Base Address of D addi t1, zero, 4 # Total number of rows in I addi t2, zero, 4 # Total number of columns in I

    create_identity_matrix_loop: bge t1, zero, create_identity_matrix_exit addi t1, t1, -1 addi t3, zero, t2 create_identity_matrix_inner_loop: bge t3, zero, create_identity_matrix_inner_done addi t3, t3, -1 beq t1, t3, set_one addi t0, t0, 4 j create_identity_matrix_inner_loop set_one: sw x1, 0(t0) addi t0, t0, 4 j create_identity_matrix_inner_loop create_identity_matrix_inner_done: j create_identity_matrix_loop create_identity_matrix_exit:

    Apply Gauss-Jordan elimination

    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

    gauss_jordan_loop: bge t1, zero, gauss_jordan_exit addi t1, t1, -1 addi t4, zero, t2 gauss_jordan_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, 0(t0) # Pick the pivot element from the pivot row mul t5, t5, t6 # Multiply the element with the pivot element lw t6, 0(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, t44(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, 0(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:

    Check if the flag is set

    beq t3, x0, inverse_T_done

    Inverse of T is now stored in D

    inverse_T_done: #x-----------------------------x--------------------------------x

    #multiplying matrix D with matrix b that is (A^T x C^-1 x A)^-1 x A^t and storing the result in E: #----------------------------------------------------------------------------------------------------------- #write your code here

  2. Assistant

    Multiply D with B to get the final result

    add t0, zero, s1 # COPY(Base Address of Matrix B) add t1, zero, s4 # COPY(Base Address of Matrix D) add t2, zero, s1 # COPY(Base Address of Matrix B) add t3, zero, E # COPY(Base Address of Matrix E)

    Nested loops for multiplying each element of D with each row of B

    addi t5, zero, 0 # OutterCounter = t5 addi t6, zero, 0 # InnerCounter = t6

    for111: bge t5, t3, for111_done

    for222: bge t6, t2, for222_done  # if OutterCounter == Total_Rows
    lw s3, 0(t1)                 # Pick One Element from D
    lw s4, 0(t0)                 # Pick Element from First Column of B
    lw s5, 4(t0)                 # Pick Element from Second Column of B
    
    mul s4, s3, s4              # Multiply Element of D by Element from First Column of B
    mul s5, s3, s5              # Multiply Element of D by Element from Second Column of B
    
    add s6, s6, s4              # Keep adding the result of Multiplication(s4) in s6
    add s7, s7, s5              # Keep adding the result of