TL;DR:要加载的 32 位常量x2
is 0xffffffff
这对应于-1. Since -1是在范围内[-2048, 2047],这个常量可以用一条指令加载:addi x2, zero, -1
。您还可以使用li
伪指令:li, x2, -1
汇编器又将其翻译为addi x2, zero, -1
.
使用 a 加载 32 位常量lui
+addi
顺序
一般来说,我们需要一个lui
+addi
序列——两条指令——用于将 32 位常量加载到寄存器中。这lui
指令编码一个 20 位立即数,而addi
指令编码一个 12 位立即数。lui
and addi
可用于分别加载 32 位常量的高 20 位和低 12 位。
Let N be a 32-bit constant we want to load into a register: N ≡ n31 ... n0. Then, we can split this constant into its upper 20 bits and lower 12 bits, NU and NL, respectively: NU ≡ n31 ... n12 ; NL ≡ n11 ... n0
In principle, we encode NU in the immediate in lui
and NL in the immediate in addi
. Nevertheless, there is a difficulty to handle if the most significant bit of the 12-bit immediate in addi
is 1 because the immediate value encoded in the addi
instruction is sign extended