Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

c - Why doesn't the stack/base pointer change here?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...