对不一样的编译器,在c中嵌入汇编的格式也不一样。下面的代码是Red Hat Enterprise中的gcc中编译的。
源程序:
assembly.c
#include<stdio.h>
int x = 2;
int y = 3;
int c = 0;
int main(void)
{
__asm__
(
"push %eax \n"
"mov x, %eax \n"
"add y, %eax \n"
"mov %eax, c \n"
"pop %eax \n"
);
printf("c = %d\n", c);
return 0;
}
说明:gcc采用的汇编格式是AT&T格式,如mov %eax,c的意思是把寄存器eax中的值放入到变量c中。