関数は次のasm()
順序に従います。
asm ( "assembly code"
: output operands /* optional */
: input operands /* optional */
: list of clobbered registers /* optional */
);
そして、Cコードを介してアセンブリでxに11を配置するには:
int main()
{
int x = 1;
asm ("movl %1, %%eax;"
"movl %%eax, %0;"
:"=r"(x) /* x is output operand and it's related to %0 */
:"r"(11) /* 11 is input operand and it's related to %1 */
:"%eax"); /* %eax is clobbered register */
printf("Hello x = %d\n", x);
}
破壊されたレジスターを回避することで、上記の asm コードを単純化できます。
asm ("movl %1, %0;"
:"=r"(x) /* related to %0*/
:"r"(11) /* related to %1*/
:);
入力オペランドを避け、c の代わりに asm のローカル定数値を使用することで、さらに単純化できます。
asm ("movl $11, %0;" /* $11 is the value 11 to assign to %0 (related to x)*/
:"=r"(x) /* %0 is related x */
:
:);
別の例: 2 つの数値をアセンブリと比較する