ARM AArch64 程序集:立即超出范围

ARM AArch64 Assembly: immediate out of range

我在 AArch64 汇编指令中使用立即值时遇到问题。我用谷歌搜索但找不到任何解决方案。我想简单地 AND 一个 w 注册一个立即值如下:

"and w9, w8, #0x5fffffff \n\t"

这给我 immediate out of range at operand 3 错误。当我想 xor 立即值与 x 寄存器时,会发生同样的情况:

"eor x0, x0, #ffffffffffffffff"

有人知道为什么吗?

A64 指令集对可在指令中使用的立即数类型有非常奇怪的限制。基本限制相对简单,所有指令都是 32 位长,指令只能使用这 32 位中的一小部分作为立即值。奇怪的是哪些立即值对于哪些指令是合法的。根据 ARM 编译器 armasm 参考指南,AND and EOR 指令将立即数限制为:

Such an immediate is a 32-bit or 64-bit pattern viewed as a vector of identical elements of size e = 2, 4, 8, 16, 32, or 64 bits. Each element contains the same sub-pattern: a single run of 1 to e-1 non-zero bits, rotated by 0 to e-1 bits. This mechanism can generate 5,334 unique 64-bit patterns (as 2,667 pairs of pattern and their bitwise inverse). Because the all-zeros and all-ones values cannot be described in this way, the assembler generates an error message.

对于第一条指令,您需要先将立即值加载到另一个寄存器中。类似于:

ldr w10, =0x5fffffff
and w9, w8, w10

对于第二条指令,您可以将其替换为MVN (bitwise NOT)指令:

mvn x0, x0

注意最后一条指令实际上是ORN (bitwise OR NOT)指令的别名:

orn x0, xzr, x0