Q
Q
Qubc2021-08-05 19:09:16
C++ / C#
Qubc, 2021-08-05 19:09:16

Why is the memory on the stack for the auto variable not reserved by rsp?

Why isn't memory reserved with rsp? What if an interrupt occurs? Or is there some trick here?

int main(void) {
  int a = 5;
  return 0;
}


main:
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $5, -4(%rbp)
        movl    $0, %eax
        popq    %rbp
        ret

Here everything is ok and clear.
void f(void){
  ;
}
int main(void) {
  int a = 5;
  f();  
  return 0;
}

f:
        pushq   %rbp
        movq    %rsp, %rbp
        nop
        popq    %rbp
        ret
main:
        pushq   %rbp
        movq    %rsp, %rbp
        subq    $16, %rsp
        movl    $5, -4(%rbp)
        call    f
        movl    $0, %eax
        leave
        ret

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2021-08-05
@Qubc

I'm not sure I understood the question correctly. Apparently, you do not understand why in the first example there is no analogue subq $16, %rsp?
Because you don't have to. rsp is still not used by the main function, it would just be an extra couple of instructions.
In the second example, rsp needs to be configured because there is a call to f(), and it cannot be pushed onto the main() stack.

J
jcmvbkbc, 2021-08-06
@jcmvbkbc

Why isn't memory reserved with rsp? What if an interrupt occurs? Or is there some trick here?

There is some trick called red zone .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question