0

I've written the following function to play around with inline assembly a bit and print out various registers:

void run(void)
{
    long rsp, rbp;
    asm("mov %%rsp, %0;" "mov %%rbp, %1;" : "=r" (rsp), "=r" (rbp));
    printf("Middle\n%%rsp = %#lx\n" "%%rbp = %#lx\n", rsp, rbp);
}
int main(void)
{
    long rsp, rbp;
    asm("mov %%rsp, %0;" "mov %%rbp, %1;" : "=r" (rsp), "=r" (rbp));
    printf("Start\n%%rsp = %#lx\n" "%%rbp = %#lx\n", rsp, rbp);

    run();
    long a,b,c;
    char* d = "Hello";
    char e[10];

    asm("mov %%rsp, %0;" "mov %%rbp, %1;" : "=r" (rsp), "=r" (rbp));
    printf("End\n%%rsp = %#lx\n" "%%rbp = %#lx\n", rsp, rbp);
}

And it prints out the following:

Start
%rsp = 0x7ffeec93bf60
%rbp = 0x7ffeec93bf90
Middle
%rsp = 0x7ffeec93bf40
%rbp = 0x7ffeec93bf50
End
%rsp = 0x7ffeec93bf60 * same as start
%rbp = 0x7ffeec93bf90 * same as start

So in the above case the stack/base pointer changes when going into another function (Middle) but why doesn't it change when defining those 5 variables in the middle of the main function? Other than calling a function, what types of operations result in changing the stack? Or is that totally up to the compiler, and you really don't have any control of it when not writing directly in asm?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • 3
    Because the compiler already allocated those variables at the start of the function. Consult the assembly code. Using `alloca` with a runtime value should change `rsp` for you. `rbp` doesn't change inside a function unless it's not used as a frame pointer. – Jester Jan 21 '21 at 21:44
  • 3
    The last sentence is correct. The compiler can rearrange your functions, inline them, and do all sorts of unexpected things. There is no easy way to predict what value these pointers will have. – fuz Jan 21 '21 at 21:45
  • @Jester Oh I see, it seems it puts all the `subq` at the very top. A variation is...`main: pushq %rbp` `movq %rsp, %rbp` `subq $80, %rsp`. – samuelbrody1249 Jan 21 '21 at 21:49
  • @fuz thanks. Additionally, is there something like a `size_pointer` to use instead of `long`, or what's the suggested type to use for that? Or just do something like `char *rsp, *rbp;` ? – samuelbrody1249 Jan 21 '21 at 21:50
  • 2
    @samuelbrody1249 Consider using `uintptr_t` or `intptr_t` from `stdint.h`. – fuz Jan 21 '21 at 21:56
  • Look at the compiler's asm output on https://godbolt.org/ to see where your inline asm blocks ended up mixed in with the compiler-generated asm. (And how that changes when you enable `-O2` or `-O3` optimization.) – Peter Cordes Jan 21 '21 at 22:08

0 Answers0