[英]C and assembly __asm doesn't work
I found this piece of code to put the stack pointer into EAX register(It should be the register used by "return" in C) 我发现这段代码将堆栈指针放入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());
}
I tried it with Geany but it doesn't works!! 我和Geany一起试过但它不起作用!! Then I follow the compiler log and I changed the code in this way:
然后我按照编译器日志,我以这种方式更改了代码:
#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
}
}
this time I have no problems with the main but the other function is a tragedy!!! 这次我主要没有问题但其他功能是悲剧! It doesn't recognize __asm.
它无法识别__asm。 unknown type name 'mov'.... unused variable 'eax'... It seems like it wants __asm() instead of __asm{}, like a normal call of a function.
未知类型名称'mov'....未使用的变量'eax'...似乎它想要__asm()而不是__asm {},就像函数的正常调用一样。 Somebody can help me?
有人可以帮帮我吗? PS I have debian 64....it can have some problems with the 64 architecture??
PS我有debian 64 ....它可能有64架构的问题?
The correct GCC code would be 正确的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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.