WhiteMask
WhiteMask

Reputation: 646

Does Changing the Valuye of %esp Remove Values from Stack?

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.

Upvotes: 2

Views: 1098

Answers (2)

MSalters
MSalters

Reputation: 179819

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.

Upvotes: 1

Gabriel Ciubotaru
Gabriel Ciubotaru

Reputation: 1092

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

Upvotes: 1

Related Questions