gpt4 book ai didi

gcc - 如何强制 gcc 对 asm 中的操作数使用两个不同的寄存器?

转载 作者:行者123 更新时间:2023-12-01 11:49:33 27 4
gpt4 key购买 nike

我正在使用为 sam7s 处理器编译的 gcc 4.6.3。

我需要使用一些内联汇编:

int res;
asm volatile (" \
MRS r0,CPSR \n \
MOV %0, r0 \n \
BIC r0,r0,%1 \n \
MSR CPSR,r0 \n \
" : "=r" (res) : "r" (0xc0) : "r0" );
return res;

由 gcc 翻译成(我添加的注释):

mov r3, #192    ; load 0xc0 to r3
str r0, [sl, #56] ; preserve value of r0?

mrs r0, CPSR ; load CPSR to r0
mov r3, r0 ; save r0 to "res"; r3 overwritten!
bic r0, r0, r3 ;
msr CPSR_fc, r0 ;

问题是在“%0”(res)和“%1”(常量:0xc0)的位置使用了相同的寄存器“r3”。因此 %1 在使用之前被覆盖,代码无法正常工作。

问题是如何禁止 gcc 对输入/输出操作数使用相同的寄存器?

最佳答案

好吧,我终于找到了here

& says that an output operand is written to before the inputs are
read, so this output must not be the same register as any input.
Without this, gcc may place an output and an input in the same register even if not required by a "0" constraint. This is very useful, but is mentioned here because it's specific to an alternative. Unlike = and %, but like ?, you have to include it with each alternative to which it applies.

"=r"(res) 更改为 "=&r"(res) 后一切正常。

关于gcc - 如何强制 gcc 对 asm 中的操作数使用两个不同的寄存器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12745217/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com