Search code examples
assemblyinline-assemblyicc

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


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.


Solution

  • 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;
    }