How to access stack base pointer (rbp) using inline assembly?

两盒软妹~` 提交于 2019-12-11 01:05:48

问题


My requirement is to access a function call parameters by offsetting rbp using inline assembly. But I couldn't find a suitable operand constraint to specify the base pointer in x86. I am using Intel compiler but it's documentation states that it supports GCC style inline assembly. So GCC based example would be sufficient.


回答1:


You can try:

#include <stdio.h>
#include <inttypes.h>

int
main(int argc, char **argv)
{
 uint64_t n;

 __asm__ __volatile__(
   "movq %%rbp, %0\n\t"
   : "=r"(n)
 );

 printf("rbp = 0x%" PRIx64 "\n", n);
 return 0;
}



回答2:


I know you asked for inlined assembly, but note that on gcc you could also use __builtin_frame_address, which both frees you from the need for inlined assembly (yikes), and could provide frame levels further than the immediate one.

See here - http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html



来源:https://stackoverflow.com/questions/22540825/how-to-access-stack-base-pointer-rbp-using-inline-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!