为什么这段代码没有将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);
}
asm("mov $1, %0;" : "=r"(temp) :/* no input*/);
- DrBeco您需要向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”。
myVar
。如果进行了优化编译,这可能会很容易地导致错误。** 这是如何不使用inline asm的示例。 ** 它需要成为一个单一的asm语句,最好带有一个“= m”(myvar)输出。 参见https://gcc.gnu.org/wiki/ConvertBasicAsmToExtended - Peter Cordes