ENTER/LEAVE

According to the Intel manual for developers, these instructions provide machine-language support for procedure calls in block-structured languages; however, they are very useful for Assembly developers as well.

When we implement a procedure, we have to take care of the creation of the stack frame where we store the procedure's variables, storing the value of ESP and then restoring all that before we leave a procedure. These two instructions can do all that work for us:

; Do something here
call my_proc
; Do something else here

my_proc:
enter 0x10, 0 ; Save EBP register on stack,
; save ESP to EBP and
; allocate 16 bytes on stack for procedure variables
;
; procedure body
;
leave ; Restore ESP and EBP registers (this automatically
; releases the space allocated on stack with ENTER)
ret ; Return from procedure

The preceding code is equivalent to the following code:

; Do something here
call my_proc
; Do something else here

my_proc:
push ebp ; Save EBP register on stack,
mov ebp, esp ; save ESP to EBP and
sub esp, 0x10 ; allocate 16 bytes on stack for procedure variables
;
; procedure body
;
mov esp, ebp ; Restore ESP and EBP registers (this automatically
pop ebp ; releases the space allocated on stack with ENTER)
ret ; Return from procedure
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset