delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/10/01/13:00:19

From: "John S. Fine" <johnfine AT erols DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: STACK (Enter, Leave, etc...)
Date: Fri, 01 Oct 1999 10:56:00 -0400
Lines: 59
Message-ID: <37F4CB80.676A@erols.com>
References: <7t2fgv$1hu$1 AT tron DOT sci DOT fi>
Mime-Version: 1.0
X-Trace: 8dsot2l4rKbPzzK+UP7MtSnByqmjbvr9shgxC2gS5So=
X-Complaints-To: abuse AT rcn DOT com
NNTP-Posting-Date: 1 Oct 1999 14:58:27 GMT
X-Mailer: Mozilla 3.01 (Win95; U)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019