Search code examples
cassemblygnu-assemblerriscv

Loading an address, as a symbol, into a general register


I have some RISC V assembly in a crt.S file.

If I use the name of the C function _init like this:

j _init

(i.e. execute an unconditional jump to _init), the code compiles fine and runs.

But if I use it like this:

li a3, _init

(ie, load general register A3 with the integer value _init) the code will not compile. If I substitute a literal integer value (e.g., 0x80001958, which all other things being equal, is value of _init currently) the code compiles.

What am I doing wrong here? (This is using GNU GCC/AS)


Solution

  • The answer is that - as RISCV supports a wide variety of address formats - I cannot use li and so work on the assumption that the address format is the same size as the register format. Instead, I have to use la to load a number appropriately extended to the address format as opposed to the integer register format.