我发现这段代码将堆栈指针放入EAX寄存器(它应该是C中“return”使用的寄存器)
#include <stdio.h>
unsigned long get_sp(){
unsigned long stp;
__asm{
mov
eax, esp
}
}
void main(void){
printf("\n0x%x", get_sp());
}
Run Code Online (Sandbox Code Playgroud)
我用 Geany 试过了,但它不起作用!!然后我按照编译器日志,以这种方式更改了代码:
#include <stdio.h>
unsigned long get_sp(void);
int main(void){
printf("\n0x%ld", get_sp());
return 0;
}
unsigned long get_sp(void){
unsigned long stp;
__asm{
mov eax, esp
}
}
Run Code Online (Sandbox Code Playgroud)
这次我的主要功能没有问题,但其他功能是悲剧!!!它无法识别 __asm。未知类型名称 'mov'.... 未使用的变量 'eax'... 似乎它想要 __asm() 而不是 __asm{},就像正常调用函数一样。有人可以帮助我吗?PS 我有 debian 64 ......它可能在 64 架构上有一些问题??
正确的 GCC 代码是
__attribute__((noinline,noclone))
unsigned long get_sp(void) {
unsigned long stp;
asm(
// For x86_64: "movq %%rsp, %0"
"movl %%esp, %0"
: "=r"(stp)
);
return stp;
}
Run Code Online (Sandbox Code Playgroud)