News:

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

The extended base pointer

Started by Druesukker, April 19, 2011, 01:40:34 AM

Previous topic - Next topic

Druesukker

Hello  :P

I am currently learning hla and i am wondering if anyone can tell me if I should always use the ebp when im calling procedures/ functions?

Consider the following program, is there any logical reason why i should use the ebp here?



// Program computes the sum of 2 numbers

program TestProgram;
#includeonce( "stdin.hhf" )
#includeonce( "stdout.hhf" )

procedure TestProcedure; @noframe; @nodisplay;
// stack parms
// (
// num1: int16;
// num2: int16;
// ); -->
const
num1: text := "( type int16 [ esp + 10 ] )"; // parm 1
num2: text := "( type int16 [ esp + 8 ] )"; // parm 2

begin TestProcedure;
//      why push ebp in this situation ?
push( eax );

// compute the sum in ax
mov( num1, ax );
add( num2, ax );
stdout.put( "The result is: ", ( type int16 ax ), nl );

pop( eax );

ret( 4 ); // 4 bytes of parms total
end TestProcedure;

begin TestProgram;

// stack parms start
pushw( 3 );
pushw( 2 );
// stack parms end - 4 bytes total
call( TestProcedure ); // call too print sum of parms

end TestProgram;


Thank you.

dedndave

no - one of the reasons we write in asm is to get away from all the overhead of compilers
i have seen old compiler code where ebp was pushed and loaded with esp just to inc a register - lol
the beauty of asm is, you get to be in control of such details
of course, that comes with the duty of seeing to them, as well

for functions that you may use over and over...
try to preserve EBX, EBP, ESI, EDI, and keep the DF cleared (up direction)
return status in EAX, count in ECX, address in EDX
pass values to the function on the stack
this makes your function look like an API call
it also makes it usable inside callback functions without modification

i tend to use a stack frame (ebp) if i have a lot of locals, or if speed isn't critical, which is really quite often
if i want fast code or super small code, i may do things manually, without using ebp
using a stack frame can make code easier to read and maintain

for internal routines, all bets are off
you can pass parms in register - do whatever suits you, or whatever suits the situation

but then, some in here don't like my code - lol
to each his own - if i didn't enjoy it, i'd probably be doing something else   :P
choose your own style - if you are comfortable with it - to hell with everyone else

Druesukker

Ok thakns.

So people don't expect i will preserve the eax, edx and ecx ?

Yes, I see the usefulness of the stack frame when having a lot of parameters  :bg

dedndave

well - it makes it easier to read, too - lol
sometimes, i may use EBP, but i initialize locals my own way

QuoteSo people don't expect i will preserve the eax, edx and ecx ?
not for most win 32 functions
the API functions trash these registers, usually returning a result or status in EAX
API functions rarely return values in ECX or EDX, rather, they want a pointer to a var or structure
but, you may use them that way

i do not care for IF THEN ELSE stuff at all
i have a hard time reading that crap
others have a hard time reading my code because i do not use it - lol
i am old-school - i write "straight line" code   :P

Druesukker

lol alright thank you for your help..