search

Home  >  Q&A  >  body text

c++ - gcc编译后每个函数开始的 “push %ebp; movl %esp, %ebp” 是什么意思

push %ebp
movl %esp, %ebp

是什么意思?有什么作用?为什么要这样做???

天蓬老师天蓬老师2803 days ago1215

reply all(3)I'll reply

  • 迷茫

    迷茫2017-04-17 13:09:07

    ebp is the frame pointer and esp is the stack pointer. These two lines of code save the old frame and create a new stack frame. Procedure calls in assembly require this action

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:09:07

    Create a stack frame, which can be optimized. Keeping this is mainly convenient for debugging, and you can trace the function call chain.
    unsigned long *p=ebp;
    *(p+1) is the return address of the calling function.
    p=*p is the frame of the upper-level function
    *(p+1) is the return address of the calling function of the upper-level function
    You can always trace it back to the top function through this.

    reply
    0
  • 阿神

    阿神2017-04-17 13:09:07

    This is related to the function stack frame.
    When a process starts, a stack frame will be created for the current process. The machine uses the stack to pass process parameters and store return information. %ebp is the frame pointer and %esp is the stack pointer. The two sentences you mentioned are stack building statements.

    reply
    0
  • Cancelreply