I understand the basic semantics of the EBP and ESP registers but I am slightly confused by the following:
mov ebp, eax
Next line is:
mov edi, dword ptr [ebp]
I thought the ebp register is our reference point to walk the stack, adding to it gives us parameteres, subtracting gets local variables. So by moving eax to ebp, does this not cause issues?
I could understand say:
mov [ebp+12], eax
I would guess that is along the lines of moving eax into an out parameter?
It’s impossible to tell what entities are being accessed with these instructions without seeing more code.
Nothing in the x86 CPU precludes you from using
ebpfor things other than accessing subroutine parameters and local variables.In 32-bit mode you can access on-stack variables through
espas well because there are memory operand encodings for[esp+something]. In 16-bit mode you can’t do that withspand are required to use one of the registers from the following list:bp,bx,si,di.Some compilers have an option to use
[esp+something]instead of[ebp+something]for accessing on-stack data. This buys you an extra general-purpose register that you could use for other things.As long as your code isn’t required to preserve the value of
ebpor as long as it restoresebpto the expected value, nothing bad can happen from doingmov ebp, eax.