我找到这段代码把栈指针放入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());
}
我用 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
}
}
这次我的 main 没问题,但是 other 函数就悲剧了!!! 它不识别 __asm。 未知类型名称“mov”.... 未使用的变量'eax'... 它似乎需要 __asm() 而不是 __asm{},就像函数的正常调用一样。 有人可以帮助我吗? 聚苯乙烯 我有 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;
}
关于C 和汇编 __asm 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30904654/