64.5k views
2 votes
Write the RISC-V Assembly code for a function that accepts as arguments:

1) the base address of a bi-dimensional array A stored in row-major order,
2) the number of the array rows,
3) the number of the array columns,
4) the number of bytes per array element,
5) a specific row i of the array,
6) a specific column j of the array. Then, the function returns element A[i][j].

Note that the array elements are stored in consecutive memory locations
(there are no memory holes) and also note that row-major order means that the array
elements are stored in memory as follows: successive array elements belong to
the same row but successive columns
(until of course the maximum column of the same row is exhausted and we begin with
a new row).

User Mentalist
by
8.0k points

1 Answer

4 votes

Final answer:

In RISC-V Assembly, you can write a function that accepts the base address of a bi-dimensional array, the number of rows and columns, the number of bytes per element, a specific row, and a specific column. The function can then calculate the memory location of the element A[i][j] and return its value.

Step-by-step explanation:

In RISC-V Assembly, you can write a function that accepts the base address of a bi-dimensional array, the number of rows and columns, the number of bytes per element, a specific row, and a specific column. The function can then calculate the memory location of the element A[i][j] and return its value.

Here is an example of how the function can be implemented:

.data
base_address: .word 0x1000 # Example base address
rows: .word 3
columns: .word 4
bytes_per_element: .word 4

.text
.globl array_element
array_element:
lw a0, base_address # Load the base address
lw a1, rows # Load the number of rows
lw a2, columns # Load the number of columns
lw a3, bytes_per_element # Load the number of bytes per element
lw a4, 0($a1) # Load specific row i
lw a5, 0($a2) # Load specific column j
mul a4, a4, a2 # Calculate the offset of row i
mul a4, a4, a3 # Calculate the offset of row i in bytes
add a4, a4, a5 # Calculate the offset of element A[i][j] from the base address
add a0, a0, a4 # Calculate the memory location of element A[i][j]
lw a0, 0(a0) # Load the value of element A[i][j]
jr ra # Return the value

User Eduardo Herzer
by
8.0k points