回到文章列表
risc-v-regitster-address

【RISC-V】指令、Regitster、Address長度小筆記

發布於 2026-04-21·11·
RISC-V

每次都會忘記RISC-V各種東西的長度,因此要記錄下來。

  • 指令: 32 bits
  • Regitster: 64 bits
  • Address: 64 bits (但SV39只用了39 bits 剩下的保留)

因此如果要Load uint32到暫存器Lower的地方,就會需要lui (Load Upper Immediate) + addi指令

riscv
lui  a0, 0x12345       # 載入高 20 位元,a0 = 0x12345000
addi a0, a0, 0x678     # 加上低 12 位元,a0 = 0x12345678

而如果是uint64則可以用Pseudo Instruction li

riscv
li t0, 0x1234567812345678

如果不用li就會比較麻煩,需要再加上slli (Shift Left Logical Immediate)

riscv
# 步驟 1:處理最高 32 位元的前半段 (0x12345)
lui     t0, 0x12345          # t0 = 0x0000000012345000
addi    t0, t0, 0x678        # t0 = 0x0000000012345678

# 步驟 2:將這段數據推到最左邊 (左移 32 位)
slli    t0, t0, 32           # t0 = 0x1234567800000000

# 步驟 3:開始蓋下半層大樓 (0x12345)
# 注意:這裡通常會用另一個暫存器來湊,或者直接在 t0 上疊加
lui     t1, 0x12345          # t1 = 0x0000000012345000
addi    t1, t1, 0x678        # t1 = 0x0000000012345678

# 步驟 4:將兩者合體
or      t0, t0, t1           # t0 = 0x1234567812345678