This sequence preserves the original base pointer EBP; points EBP to the current stack pointer (which points at the old EBP, followed by the return address and then the function parameters); and then creates space for automatic variables on the stack. Local variables are created on the stack with each call to the function, and are cleaned up at the end of each function. This behaviour allows for functions to be called recursively. In C and C++, variables declared “automatic” are created in this way.
Functions and Stack Frame
When a function is called, a new stack frame is created at the current esp location. A stack frame acts like a partition on the stack. All items from previous functions are higher up on the stack, and should not be modified. Each current function has access to the remainder of the stack, from the stack frame until the end of the stack page. The current function always has access to the “top” of the stack, and so functions do not need to take account of the memory usage of other functions or programs.