# Linux Kernel Module Cheat

#### 29.8.4. x86 ENTER and LEAVE instructions

userland/arch/x86_64/enter.S

These instructions were designed to allocate and deallocate function stack frames in the prologue and epilogue: https://stackoverflow.com/questions/5959890/enter-vs-push-ebp-mov-ebp-esp-sub-esp-imm-and-leave-vs-mov-esp-ebp

ENTER appears obsolete and is kept mostly for backwards compatibility. LEAVE is still emitted by some compilers.

ENTER A, B is basically equivalent to:

    push %rbp
    mov %rsp, %rbp
    sub %rsp, A

which implies an allocation of:

- one dword to remember EBP

- A bytes for local function variables

I didn’t have the patience to study the B parameter, and it does not seem to be used often: https://stackoverflow.com/questions/26323215/do-any-languages-compilers-utilize-the-x86-enter-instruction-with-a-nonzero-ne

LEAVE is equivalent to:

    mov %rbp, %rsp
    pop %rbp

which restores RSP and RBP to the values they had before the prologue.