What is the use of “push

前端 未结 6 1619
我寻月下人不归
我寻月下人不归 2020-12-23 09:38

What effect these two instructions cause in the assembly code generated by gcc for x86 machines:

push %ebp
movl %esp, %ebp
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 10:05

    The piece of code sets up the stack for your program.

    In x86 stack information is held by two registers.

    
        Base pointer (bp): Holds starting address of the stack
        Stack pointer (sp): Holds the address in which next value will be stored
    

    These registers have different names in different modes:

    
                                Base pointer           Stack pointer
        16 bit real mode:       bp                     sp
        32 bit protected mode:  ebp(%ebp)              esp(%esp)
        64 bit mode:            rbp                    rsp
    

    When you set up a stack, stack pointer and base pointer gets the same address at the beginning.

    Now to explain your code,

    
        push %ebp
    

    This code pushes the current stack address into the stack so that the function can "exit" or "return" properly.

    
        movl %esp, %ebp
    

    This code sets up the stack for your function.

    For more info, refer to this question.

    Hope this help!

提交回复
热议问题