16

为什么此代码不设置temp为 1?我该怎么做?

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

3 回答 3

17

你想temp成为一个输出,而不是一个输入,我想。尝试:

  __asm__(
      ".intel_syntax;"
      "mov eax, %1;"
      "mov %0, eax;"
      ".att_syntax;"
      : "=r"(temp)
      : "r"(1) 
      : "eax");
于 2011-03-22T21:00:09.583 回答
8

这段代码完成了您想要实现的目标。我希望这可以帮助你:

#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);
}
于 2011-03-22T21:30:41.717 回答
5

您必须将参数传递给 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”。

于 2013-08-31T03:45:56.633 回答