您的位置:首页 > 编程语言

在c程序中嵌入汇编代码的例子

2012-05-21 15:01 183 查看
对不一样的编译器,在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中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  汇编 c gcc 编译器