简体   繁体   中英

Inline Assembly in C

#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");
}

I am fairly a newbie, and I copy the sample code in the book bought yesterday, but when I compiled my first asm code, I just got some warnings and errors reported from GCC-mingw32 Compiler listed below:

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

How can I compile it successfully? Thanks in advance :-)

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

There must be opening and closing quotes on the first two rows of the asm code like this:

#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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM