为什么此代码不设置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
为 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”。