[英]Strange behavior of ADD in asm
The output of this code:这段代码的输出:
int i=0;
while(i<5)
{
asm volatile
(
"addl $1,%0"
:"=r"(i)
:
:"memory"
);
printf("%d\n",i);
}
is like this:是这样的:
2
3
3
3
.
.
.
But it should be like this:但它应该是这样的:
1
2
3
4
5
Why is that so?为什么呢? I can't seem to understand where the problem is.我似乎无法理解问题出在哪里。
You are specifying i
as an output-only operand.您将i
指定为仅输出操作数。 That is, the compiler uses the output of the register %0
for the variable, but the current value if i
is not copied to the register at the beginning.也就是说,编译器将寄存器%0
的输出用于变量,但如果i
没有在开始时复制到寄存器中,则使用当前值。
Specify i
as input as well, for the same register:对于同一个寄存器,也指定i
作为输入:
asm volatile
(
"addl $1,%0"
:"=r"(i)
:"0"(i)
:"memory"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.