簡體   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