Question: The following RISC - V assembly code calculates the trace ( sum of the diagonal ) of a square matrix. Fill in the gaps in

The following RISC-V assembly code calculates the trace (sum of the diagonal) of a square matrix. Fill in the gaps in the code. For registers. use the ABI name from the card given below (second column). For immediate operands, use decimal numbers, without leading zeros.
# sum of the diagonal of a square matrix
# matrix allocation in memory
.data
m: .word 1,2,3,4
.word 5,6,7,8
.word -1,20,3,-4
.word 9,2,8,5
n: .word 4
.text #
li a0,0 # a0 will store the sum
la s0, m
lw s1, n #this is a pseudoinstruction equivalent to la s1, n; lw s1,0(s1)
# calculate 4*n in s2
slli s2,__,__# set s2 to point to the next row, same column of the matrix
addi s2,__,__ # now set s2 to point to the next element in the row; this will be on the diagonal
loop: lw t0,__(s0) # load array element on the diagonal in t0, start with m(0,0)
add __,__, t0 # add the element to the sum
add __, s0,__ # point to the next element in the diagonal
addi s1, s1,-1 # decrement iterator
bne s1,__, loop #loop until you have gone through the entire array

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!