3
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ using store statement in RISC-V ISA of course. \$\endgroup\$
    – Mitu Raj
    Nov 29 '21 at 10:39
  • 1
    \$\begingroup\$ Can you be more specific? The largest value you can make a register via the addi instruction is 2047. Addresses in RISC-V are 32 bits. \$\endgroup\$
    – Anthony
    Nov 29 '21 at 10:43
4
\$\begingroup\$

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – Anthony
    Nov 29 '21 at 11:04
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – devnull
    Nov 29 '21 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.