Mail Archives: djgpp/1999/10/01/13:00:19
stefan fröberg wrote:
> - Is the instruction "enter n,0" the same thing as doing:
> push ebp
> mov ebp,esp
> sub esp, n bytes
Close enough. (Condition codes are different, of course).
> Also, the last part of the "enter" instruction has something to do with the
> (function nesting ?) in Pascal
It is for languages in which variable scope can be based on lexical
nesting of functions. Such as Pascal, but not just Pascal.
> and I should not worry about it, right ?
Right. C does not permit any lexical nesting of functions.
C++ does permit lexical nesting, but I think variable scope does
not follow the lexical nesting.
> - The instruction "leave" performs the same as:
>
> mov esp,ebp
> pop ebp
>
> right ?
Right.
> - If I use the "enter n,0" instruction to set the stack frame pointer and
> allocate space for local variables do I have to set instruction "add esp,n
> bytes" before the "leave" or will the "leave" take care of unallocating
> space of the local variables ?
Leave does it. The "mov esp,ebp" part clearly restores esp
to what it was before the allocation.
> - Is there use for the "ret n bytes" instruction in C ?
Not in C calling convention, but most x86 C compilers support
other calling conventions than just the standard C calling convention.
> (That should unallocate passed parameters and local variables but the
> above thing
> should also do that, right ?)
No. The "leave" deallocates the local variables and not the
passed parameters. The "ret n" deallocates the passed parameters
and not the local variables. Standard x86 C calling conventions say
that a function should not remove the parameters passed to it
(the caller does that after the function returns).
BTW, ENTER and LEAVE may be slower than doing the same job with
multiple instructions. Except for very short functions called in
inner loops, you probably shouldn't care about that.
--
http://www.erols.com/johnfine/
http://www.geocities.com/SiliconValley/Peaks/8600/
- Raw text -