3

I'm new to RISC-V and I'm having trouble understanding how one would store specific values in large addresses. For example, if I wanted to store the value 5 in 0x12312312, how would you go about that?

Anthony
  • 55
  • 5

1 Answers1

4

It requires two steps. "Load upper immediate":

enter image description here

and add the 12 LSBits:

enter image description here

Images source: "The RISC-V Instruction Set Manual Volume I: Unprivileged ISA"

Example to build any constant/address: a5 = 0x12345678

lui a5,0x12345000(305418240)
addi a5,a5,1656

For values with all 12 LSbits = 0, only lui is required. For small values, addi to register 0 is enough.

Another example: add 65535 to a pointer stored at address (a3):

   lw   a5,0(a3)  -> load the base address, stored in (a3) to a5
   lui  a4,0x10   -> "build" the initial MSBit of the constant
   addi a4,a4,-1 # ffff -> "build" the final value of the constant
   add  a5,a5,a4  -> calculate the new adress
   sw   a5,0(a3)  -> store the new address
devnull
  • 8,447
  • 2
  • 15
  • 38
  • 1
    This clears stuff up. I guess if you're in a situation where you need to add more than 2047 to an address, you just use the addi instruction twice? – Anthony Nov 29 '21 at 11:04
  • 1
    I've added another example. It gets simpler for small values, as you mentioned, and also with shifts with all 12 the LSBits equal 0. – devnull Nov 29 '21 at 11:21