News:

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

Reducing the prolog/epilog footprint.

Started by Damos, December 19, 2008, 08:56:10 AM

Previous topic - Next topic

Damos

Prolog code for a function that is invoked looks like this:
push ebp
mov ebp,esp
sub ebp,(4*number of local vars)

and epilog:

pop ebp
add esp,(4*number of arguments)

but could the use of local variables not be done without using ebp at all and just use esp.
I know this would mean the assembler keeping track of the amount of pushes and pops so that it could generate the right offset, but it's doable right?
I sometimes disable prolog/epilog when I have no need for local vars, or if I need stack space ie to switch between fpu and cpu data i would simply:

fstp dword ptr[esp+4]
mov eax,[esp+4]

even better,imagine a macro that keeps track of stack so that you can generate local vars as you go maybe somthing like this:

mov eax,12345
push LOCALVAR(eax,var1)

this would assign var1 to the position on the stack where you will push eax, this would however require that you keep track of the stack,and because it is done at compile time any strange runtime stack manipulation that doesn't balance the stack properly as in:

.repeat
     push eax
     dec runtimeval
.until runtimeval==0

could not be done, but in most cases this is not the case anyway and stackgrowth can be followed at compile time.

so effectively a global var will need to be maintained at compile time:
so every:
push eax
(increase globalvar by 4)

every
pop eax
(decrease globalvar by 4)

and don't forget invoke statements:

invoke Sendmessage,0,0,0,0
(increase globalvar by 20)

so that when refering to the local var above:

mov eax,var1

compiles to

mov eax,[esp+globalvar+var1offset]

no need to set up a stack frame at all!

it's just a shame that macros can't override mnemonics otherwise I would of tried to implement this myself.

what do you think?
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction. - Albert Einstien

MichaelW

zooba posted some information on the prologue and epilogue macros here. The MSDN Wiki page appears to be gone, so I included an attachment that contains a copy of it. zooba's post starts about half way down the page.

[attachment deleted by admin]
eschew obfuscation

zooba

The MSDN Wiki at the time I posted the first link was still in beta. It has since been combined into the MSDN library (as "Community Content").

The OPTION (MASM) page has my original documentation for PROLOGUE and EPILOGUE on it (though it seems when a new version of MSDN comes out the community content doesn't copy over, so you may need to go to one of the older versions).

Also, sounds like an interesting exercise, though you would need to override so many instructions that I think you'd end up with your own language (which is fine :bg ).

Cheers,

Zooba :U

Rockoon

Its too much work to automate it with macros...

...and its not that difficult to do it by hand (I never use a stack frame, nor the simplified local variables stuff, nor the simplified calling/invoking)

Once you've completely turned your back on these high level features, you may wonder why you ever used them to begin with. I mean come on.. AN EXTRA REGISTER.. who doesnt want an extra register.. or more to the point, why would you reserve 25% of your x86 registers for stack management when it could be 12.5%.
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.