The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: cman on June 28, 2005, 08:39:49 PM

Title: Enter , Leave
Post by: cman on June 28, 2005, 08:39:49 PM
Does anyone know of examples of these instructions? Thanks :bg
Title: Re: Enter , Leave
Post by: Jeff on June 28, 2005, 10:36:46 PM
in its most simplest form:
thisproc PROC
    ENTER 0,0
    ;...
    ENTER 1024,0
    ;...
    LEAVE
    ;...
    LEAVE
    RET
thisproc ENDS
is equivalent tothisproc PROC
    PUSH ebp
    MOV ebp,esp
    ;...
    PUSH ebp
    MOV ebp,esp
    SUB esp,1024
    ;...
    ADD esp,1024
    MOV esp,ebp
    POP ebp
    ;...
    MOV esp,ebp
    POP ebp
    RET
thisproc ENDS


the first operand for enter is the amount of bytes in the stack to reserve for local variables and whatnot.  the second sets the nesting level.  in laymans terms, how many stack frames to have pointers to so you can access local variables from those frames.  for the most part, you would have the second operand 0.  it is difficult to show the nesting levels in action so you should try experimenting with it and check out the stack in a debugger.
Title: Re: Enter , Leave
Post by: hutch-- on June 29, 2005, 12:32:32 AM
The manual expansion that Jeff has shown is usually more efficient at the entry end than the exit end. ENTER is slow where LEAVE on exit saves a potential read after write stall.

MASM has used this combination for years since it was viable to use leave simply because it was smaller but it survives in modern code because it actualy works well.
Title: Re: Enter , Leave
Post by: MazeGen on June 29, 2005, 08:53:50 AM
Take a look at Instructions for Block-Structured Languages (http://www7.informatik.uni-erlangen.de/~msdoerfe/embedded/386html/s03_07.htm).
Title: Re: Enter , Leave
Post by: AeroASM on June 29, 2005, 10:08:49 AM
Quote from: hutch-- on June 29, 2005, 12:32:32 AM
LEAVE on exit saves a potential read after write stall.

Surely a write after read?
Title: Re: Enter , Leave
Post by: thomasantony on June 29, 2005, 01:52:09 PM
Quote from: AeroASM on June 29, 2005, 10:08:49 AM
Quote from: hutch-- on June 29, 2005, 12:32:32 AM
LEAVE on exit saves a potential read after write stall.

Surely a write after read?
Hi,
   I don't know about read or write but a LEAVE takes only 4-5 cycles and 1 byte while the mov esp,ebp pop ebp combo takes 6-8 cycles and 3-4 bytes. But at the same time ENTER is much slower than push ebp. mov ebp,esp

Thomas :U
Title: Re: Enter , Leave
Post by: hutch-- on June 29, 2005, 02:56:04 PM
Aero,

Write after read does not procude a stall, its usually dependency on the previous operation that causes the problem.


    MOV esp,ebp    ; read EBP
    POP ebp          ; next OP that depends on completion of previous OP.