C中的内联汇编

时间:2013-11-13 12:39:12

标签: c gcc assembly x86 inline-assembly

#include<stdio.h>
#include<stdlib.h>

int main (void)
{
 int a=10, b;
 asm ("movl %1, %%eax;
       movl %%eax, %0;"
      :"=r"(b)        /* output */
      :"r"(a)         /* input */
      :"%eax"         /* clobbered register */
     );
 printf("%d", b);
 system("pause");
}

我相当新手,我在昨天购买的书中复制了示例代码,但是当我编译我的第一个asm代码时,我刚刚收到了下面列出的GCC-mingw32编译器的一些警告和错误:

In function 'main':
line 7 --> warning: missing terminating " character
line 7 --> error: missing terminating " character
line 8 --> error: expected string literal before 'movl'
line 8 --> warning: missing terminating " character
line 8 --> error: missing terminating " character

如何成功编译?在此先感谢: - )

2 个答案:

答案 0 :(得分:7)

每条指令都应放在双引号"""movl %1, %%eax;"

答案 1 :(得分:1)

asm代码的前两行必须有开启和关闭引号,如下所示:

#include<stdio.h>
#include<stdlib.h>

int main (void)
{
int a=10, b;
asm ("movl %1, %%eax;"
"movl %%eax, %0;"
:"=r"(b) /* output */
:"r"(a) /* input */
:"%eax" /* clobbered register */
);
printf("%d", b);
system("pause");
}