繁体   English   中英

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

[英]Following is some assembly code by VS2012, how can I write it on gcc?

#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上编写它?

#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 的扩展 asm 语法以获取更多信息。

例如创建另一个文件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(){
我的乐趣();
}

像这样编译: g++ -o prog fun.s main.cpp

您可以在 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 ;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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