2

So a command like subl $4, %esp would open up a word of local variable space on the stack. Then %esp would point to this value. However, after a function call, when you restore %esp by movl %ebp, %esp, this would make %esp point above the local variable space you had opened up before. Then does this space no longer exist on the stack, because %esp must always point at the bottom of the stack, and moving the value of %esp then removes lower stuff from stack? Also, after function calls I have seen code addl $8, %esp. Does this just delete the local variable data and the rest of the stack data until the return address, where $8 is the length from the bottom of stack to the return address?

Forgive me for the strange wording of my question. I think I might understand these concepts but I am not sure, therefore I simply tried to write what I think and then wait for corrections.

3
  • 2
    Yes, logically it removes things from the stack, but physically they are still there in memory until overwritten.
    – Jester
    Apr 11, 2015 at 20:11
  • That is precisely the purpose the stack serves. When you push a value onto the stack and then later pop the value off the stack, you are doing the same thing. The stack grows lower, so take for example your current stack pointer at 100, you push an int, your stack pointer is 96, you pop the int and your stack pointer is again 100, but until you overwrite your int, it still exists at 96. Apr 11, 2015 at 21:20
  • So basically, incrementing the stack pointer also removes things from the stack in a less formal way than pop?
    – WhiteMask
    Apr 11, 2015 at 22:32

2 Answers 2

1

Inside your function, the ESP restore just remove the function's variables from the stack. From caller you have to remove the parameters given to that function. I don't know the AT notation but you will understand

...
push %eax ; ESP will be decreased by 4
push %ebx ; ESP will be decreased by 4
call fucntion
addl $8, %esp ; restore ESP removing the parameters from stack
1

It's important to understand that in assembly we leave all the convenient abstractions of high-level languages behind us. Memory is just bytes. "The stack" is just a piece of memory, with %esp somewhere in the middle. And the CPU doesn't really know where the edges of the stack are.

So, when we change %esp, we don't change values, because they don't exist to us. We don't change bytes, either, because a change to %esp affects just a CPU register and not memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.