What happens to the stack when exiting a method?

后端 未结 6 431
长发绾君心
长发绾君心 2021-01-01 20:53

I was reading What and where are the stack and heap?. One thing I am a bit fuzzy on is what happens to the stack after a method exits. Take this image for example:

6条回答
  •  旧巷少年郎
    2021-01-01 21:44

    It might be useful for you to think about what your compiled code might look like at a machine (or, better for us humans, assembly) level. Consider this as a possible example in X86 Assembly:

    When the method is called, arguments will either be passed in the registers or passed on the stack itself. Either way, the code calling the method will eventually:

    call the_method
    

    When this happens, the current instruction pointer is pushed onto the stack. The stack pointer is pointing at it. Now we're in the function:

    the_method:
       push ebp
       mov  ebp, esp
    

    The current base pointer is preserved on the stack and the base pointer is then used to reference things in the stack (like passed in variables).

       sub  esp, 8
    

    Next, 8 bytes (assuming two four byte integers are allocated) are allocated on the stack.

       mov [ebp-4], 4
       mov [ebp-8], 2
    

    The local variables are assigned. This could actually be accomplished by simply pushing them but more likely there will be a sub involved. Fast forward to the end:

       mov esp, ebp
       pop ebp
       ret
    

    When this happens, the stack pointer is right back where it was when we started, pointing at the stored base pointer (saved frame pointer). This is popped back into EBP leaving ESP pointing at the return pointer which is then "popped" into EIP with the ret. Effectively, the stack has unwound. Even though the actual memory locations haven't changed for the two local variables, they are effectively above the stack (physically below in memory, but I think you get what I mean.)

提交回复
热议问题