尝试在 arm64 架构中运行以下内联汇编指令时出现编译错误。在 32 位架构下运行良好。
Store指令将堆栈指针(sp)存储到变量stack_ptr。
unsigned long stack_ptr = 0;
__asm__ __volatile__("str sp, %[stack_ptr]"
:[stack_ptr]"=m" (stack_ptr) //output operand list
);
最佳答案
在 64 位代码中,您不能使用 SP
作为 STR
指令中的操作数。引用 documentation :
You can only use SP as an operand in the following instructions:
- As the base register for loads and stores. In this case it must be quadword-aligned before adding any offset, or a stack alignment exception occurs.
- As a source or destination for arithmetic instructions, but it cannot be used as the destination in instructions that set the condition flags.
- In logical instructions, for example in order to align it.
你应该先将它复制到一个通用寄存器中,然后将它存储到内存中。
除非你需要一个真正精确的值,否则你可以只使用普通 C 并获取局部变量本身的地址来估计堆栈指针:
unsigned long stack_ptr = (unsigned long)&stack_ptr;
关于ios - "Invalid operand for instruction"arm64 架构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31915667/