News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

HLA Activation Records

Started by matt, May 15, 2005, 04:54:54 PM

Previous topic - Next topic

matt

Hi,
In the discussion of activation record entry sequences in AofA (p314+) it is said that the way to code an entry AR for a procedure is:
following the push of the parameters and return address, push ebp onto the stack,  then save the stack pointer register into ebp, creating a base address for the new AR.
What I don't understand is why the procedure's return address can't be used as the base address for the AR??? Why is ebp pushed on to the stack? Why not MOV esp at the point of return address into ebi and access the AR at offsets of the return address instead of at offsets of the old address of ebi??? eg.
                                                       
                                                        push (ebp);// Why not remove this line?
                                                        mov (esp, ebp);
                                                        sub (NumVars, esp);

                                                         Then, to access variables in the AR:
                                                         mov ([ebp-4], eax); //ebp pointing at return address as the base address.

I'm new to assembly so i hope i've made (some) sense,    :wink. Thanx for reading/answering... Matt  :U
                                                       

chep

Hi,
In the Win32 environment, a caller assumes the callee will preserve all registers but eax, ecx and edx.

So generally you need to push ebp on the stack for one big reason : preserve it's value (so that the caller's stack frame is not messed up).
As a consequence at the end of the AR (aka "stack frame") you have to restore the original values of esp and ebp before returning from the procedure :

push (ebp);         // save the caller's frame pointer
mov (esp, ebp);   // setup your own frame pointer
sub (Size_Of_Local_Vars, esp); // reserve your local stack
...
// use it through [ebp+...]
...
mov (ebp, esp);  // free your local stack
pop (ebp)         // restore the caller's frame pointer
retn


Of course there are other ways to handle a local stack (eg. through direct esp addressing) but this one is the "standard" and easier way.