簡體   English   中英

如何使用Intel語法內聯匯編在GCC中設置變量?

[英]How to set a variable in GCC with Intel syntax inline assembly?

為什么這段代碼沒有將temp設置為1? 我該怎么做呢?

int temp;
__asm__(
    ".intel_syntax;"
    "mov %0, eax;"
    "mov eax, %1;"
    ".att_syntax;"
    : : "r"(1), "r"(temp) : "eax");
printf("%d\n", temp);

我想你想要temp是輸出,而不是輸入。 嘗試:

  __asm__(
      ".intel_syntax;"
      "mov eax, %1;"
      "mov %0, eax;"
      ".att_syntax;"
      : "=r"(temp)
      : "r"(1) 
      : "eax");

此代碼執行您要實現的目標。 我希望這可以幫助你:

#include <stdio.h>

int main(void)
{
    /* Compile with C99 */
    int temp=0;

    asm
    (   ".intel_syntax;"
        "mov %0, 1;"
        ".att_syntax;"
        : "=r"(temp)
        :                   /* no input*/
    );
    printf("temp=%d\n", temp);
}

您必須將參數傳遞給GCC匯編程序。

gcc.exe -masm=intel -c Main.c
gcc.exe Main.o -oMain.exe

你有這樣的C代碼:

#include <conio.h>
#include <stdio.h>

int myVar = 0;

int main(int argc, char *argv[])
{
    asm("mov eax, dword ptr fs:[0x18]");
    asm("mov eax, dword ptr ds:[eax+0x30]");
    asm("movzx eax, byte ptr ds:[eax+0x2]");
    asm("mov _myVar, eax");

    if(myVar == 1) printf("This program has been debugged.\r\n");
    printf("Welcome.\r\n");
    getch();

    return 0;
}

不要忘記為asm()關鍵字中的每個變量添加前綴下划線(_),否則它將無法識別它。

關鍵字asm()對每個十六進制整數使用前綴'0x',而不是后綴'h'。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM