How to use an array in RISC-V Assembly
Asked Answered
S

2

7

I'm learning RISC-V assembly and i need to use array for an exercise that i'm solving; the problem is that the simulator that i'm using(RARS) gave me an error:
Error in /home/username/file_name line 8: Runtime exception at 0x00400010: address out of range 0x000003e8.

This is the code i wrote so far:

.data
arr: .word 1000
e0: .word 5

.text
lw t1, arr # load arr into t1
lw t2, e0 # Load e0 value into t2
sw t2, 0(t1) # save t2 value into arr[0]

What i'm doing wrong?

Sarad answered 19/1, 2020 at 19:42 Comment(0)
P
9

The instruction sw t2, 0(t1) stores the content of the register t2 into the memory address provided by the register t1. However, t1 does not contain the address that corresponds to the label arr – the address where the value 1000 is stored – because t1 was initialized by the instruction lw t1, arr, and this loads the content of the address corresponding to arr into t1, i.e., it loads the value 1000 into t1.

Instead, replace lw t1, arr by la t1, arr, which does load into t1 the address that arr is representing.

Pylorectomy answered 19/1, 2020 at 20:38 Comment(0)
S
0

Here is how you can do it

.data
arr: .word 1000
e0: .word 5

.text
la t1, arr # You can only load the address of your arr into t1
lw t2, e0 # Load e0 value into t2
sw t2, 0(t1) # save t2 value into the address of arr[0] pointed by t1
Spec answered 18/5, 2023 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.