I’m currently learning about the stack (x86). I know it’s a pile of data which operates according the LIFO principle. I know the basic operations in regards to the stack are push (to add a value on the top of the stack) and pop (to remove a value). ESP is the reference to where we are now on the stack. Now, for what I don’t understand:
Example:
Push 4
Push ebx
Push eax
The instructions above will generate a stack as followed:
8 eax <-- ESP
4 ebx
0 4
With the ESP pointing towards the last added value eax.
Now when we expand these instructions with the pop operation we would get something as:
Push 4
Push ebx
Pop ebx
Push eax
The instructions above should result (if I’m correct) in following stack (for first three instructions):
4 (ebx)
0 4 <-- ESP
Ebx is removed from the stack and the ESP has moved downwards for 4 bits. Now the stack after executing all the instructions:
4 eax <-- ESP
0 4
I hope everything up to here is correct, if not comments are more than welcome ;-) Now for the instruction mov edx, [ebx,+04], starting from the first stack in this post. Is the result of this following:
16 eax
8 edx <-- ESP
4 ebx
0 4
It will start at ebx + 4 bits en write edx there moving the previous value (eax) to the top, or will it replace eax with edx?
A second questions is (more in general) how to initiate, address, and remove arrays on the stack.
My apologies for this long question, but I want to understand the (basics of the) stack. Thanks.
mov edx, [esp + 4]? - ruslikebxis not an address, it's a register.push ebxwill place its contents on stack, and not the register.[ebx+4]will be just a memory address that have nothing to do with stack. Also, stack is in memory (and it's continuous!) so you can't insert something in the middle. It's an array, not list. - ruslik