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
%%rsp = %#lx
" "%%rbp = %#lx
", rsp, rbp);
}
int main(void)
{
long rsp, rbp;
asm("mov %%rsp, %0;" "mov %%rbp, %1;" : "=r" (rsp), "=r" (rbp));
printf("Start
%%rsp = %#lx
" "%%rbp = %#lx
", 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
%%rsp = %#lx
" "%%rbp = %#lx
", 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?
question from:
https://stackoverflow.com/questions/65836151/why-doesnt-the-stack-base-pointer-change-here 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…