Search code examples
assembly64-bitriscv

Loading a 64 bit address into a register


I need to have my RISC-V assembly write instructions on the fly - (this is Forth so users can extend the language). When I was using a 32 bit ISA this was relatively simple, say I wanted to get an offset from the address I am currently writing at i’d do something like write out the op codes for:

 lui s8, [upper 20 bits of address]
 addi s8, s8, [offset]

This no longer works when I have 64 bit addresses and as I have to write out the actual op codes and not just use assembly tricks like

li s8, address

I am puzzled as to how to do it. What is the correct way?


Solution

  • Essentially I solved this by loading the higher 32 bits of the number into the bottom 32 bits, then shifting left by 32 bits and loading the bottom 32 bits.

    This is a typical piece of output assembly:

      0x0000003ff7dbd040:  li  s8,63                     #0x3f
      0x0000003ff7dbd044:  slli    s8,s8,0x20            #shift left 32 bits
      0x0000003ff7dbd048:  lui t0,0xf7dbd                #upper half load
      0x0000003ff7dbd04c:  ori t0,t0,0                   
      0x0000003ff7dbd050:  slli    t0,t0,0x20
      0x0000003ff7dbd054:  srli    t0,t0,0x20
      0x0000003ff7dbd058:  or  s8,s8,t0