int a=10, b;
asm ("movl %1, %%eax;
movl %%eax, %0;"
:"=r"(b) /* output */
:"r"(a) /* input */
:"%eax" /* clobbered register */
);
Here what we did is we made the value of ’b’ equal to that of ’a’ using assembly instructions. Some points of interest are:
“b” is the output operand, referred to by %0 and “a” is the input operand, referred to by %1.
“r” is a constraint on the operands. We’ll see constraints in detail later. For the time being, “r” says to GCC to use any register for storing the operands. output operand constraint should have a constraint modifier “=”. And this modifier says that it is the output operand and is write-only.
There are two %’s prefixed to the register name. This helps GCC to distinguish between the operands and registers. operands have a single % as prefix.
The clobbered register %eax after the third colon tells GCC that the value of %eax is to be modified inside “asm”, so GCC won’t use this register to store any other value.
When the execution of “asm” is complete, “b” will reflect the updated value, as it is specified as an output operand. In other words, the change made to “b” inside “asm” is supposed to be reflected outside the “asm”.
这段来自于:
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
b是输出操作数,关联的是上面的‘1’;a是输入操作数,关联的是上面的‘0’;
输出操作数是将寄存器的value写入b的地址,输入操作数是将a的value写入寄存器;
‘r’是对操作数施加的限制,r代表不限制,可以用任何寄存器来存储这个操作数;若是‘a’代表用eax来存储,’b‘代表用ebx来存储,如下所示:
±–±-------------------+
| r | Register(s) |
±–±-------------------+
| a | %eax, %ax, %al |
| b | %ebx, %bx, %bl |
| c | %ecx, %cx, %cl |
| d | %edx, %dx, %dl |
| S | %esi, %si |
| D | %edi, %di |
±–±-------------------+
输出操作数必须带有‘=’号;
clobbered register其实就是告知gcc,这个clobber list里面的寄存器都是不能使用的,不能用它去存储任何值,因为它已经被inline的asm汇编给修改了