0

I am confused how adding 8 to esp would remove the values from the stack. Wouldn't the space allocated in the stack still be there?

Here is some sample code for how the AddTwo process could be called.

   Example1 PROC
      push 6
      push 5
      call AddTwo
      add esp,8 ; remove arguments from the stack
      ret
   Example1 ENDP

Here is AddTwo

AddTwo PROC
   push ebp
   mov ebp,esp ; base of stack frame
   mov eax,[ebp + 12] ; second parameter
   add eax,[ebp + 8] ; first parameter
   pop ebp
   ret
AddTwo ENDP
  • 2
    You allocate and free by moving the stack pointer (`push` and `pop` also adjust the stack pointer). Anything under `esp` is considered free. The bytes are not immediately overwritten and the memory is not typically reclaimed but you can't rely on the values being preserved. – Jester Oct 19 '22 at 21:32
  • Gotcha that makes sense. I wasn't sure if there was some automatic "cleanup" that happens. – Bryan Turns Oct 19 '22 at 21:36
  • Basically a duplicate of [Is it valid to write below ESP?](https://stackoverflow.com/q/52258402) - everything there applies equally to writing and then moving ESP, explaining the real-world mechanisms that can lead to stepping on bytes below the stack pointer (or below the red-zone in calling conventions like x86-64 SysV) – Peter Cordes Oct 19 '22 at 23:16

0 Answers0