以下是VS2012的一些汇编代码,如何在gcc上编写?

时间:2013-11-14 10:36:33

标签: c assembly

#include <stdio.h>

int main ()
{
    int a = 0 ;
    /*How can I write it on gcc*/
    __asm {
         mov a, 2 ;
         add a, 4 ;
    }
    printf ("%d\n",a );
    return 0 ;
}

这是VS2012的一些汇编代码,如何在gcc上编写?

3 个答案:

答案 0 :(得分:0)

#include <stdio.h>

int main ()
{
    int a = 0 ;
    /*How can I write it on gcc*/
    asm volatile 
    (
         "mov $2, %0\n"
         "add $4, %0\n"
         : "=r"(a) /* output operand */
         : /* input operand */
         : /* clobbered operands */
    );
    printf ("%d\n",a );
    return 0 ;
}

请阅读GCC's extended asm syntax以获取更多信息。

答案 1 :(得分:0)

例如,创建另一个文件fun.s并执行以下操作

.global my_fun #to show where program should start
     my_fun:
     push %ebp  #save stack ptr
     #function body
     pop %ebp   #recover stack ptr
 ret

然后只需在主函数中调用它

  

int main(){   
my_fun();
  }

像这样编译:{{1​​}}

答案 2 :(得分:0)

你可以用gcc编写它:

#include <stdio.h>

int main ()
{
  int a = 0 ;
  /*How can I write it on gcc*/
  __asm__ __volatile__ (
    "movl $2, %0\n\t"
    "addl $4, %0\n\t"
    :"=r"(a) /* =r(egister), =m(emory) both fine here */
  );
  printf ("%d\n",a );
  return 0 ;
}
相关问题