代码之家  ›  专栏  ›  技术社区  ›  Mask

理解标准输入顺序有问题

  •  4
  • Mask  · 技术社区  · 16 年前

    Standard Entry Sequence :

    _function:
        push ebp       ;store the old base pointer
        mov ebp, esp   ;make the base pointer point to the current
                       ;stack location – at the top of the stack is the 
                       ;old ebp, followed by the return address and then
                       ;the parameters.
        sub esp, x     ;x is the size, in bytes, of all
                       ;"automatic variables" in the function
    

    最上面的是旧的 EBP,后跟返回地址 然后是参数。

    老EBP在那里是因为 push ebp ,

    但是为什么返回地址和参数也在那里呢?

    更新

    标准退出顺序

    mov esp, ebp   ;reset the stack to "clean" away the local variables
    pop ebp        ;restore the original base pointer
    ret            ;return from the function
    

    什么? ret 事实上呢?我想ESP应该已经到了回信地址 pop ebp

    2 回复  |  直到 16 年前
        1
  •  4
  •   kennytm    16 年前

    在x86标准调用约定中,在调用函数之前,首先将参数推送到堆栈中。

    以及 call op的意思是“将下一个地址推送到堆栈,然后跳到函数”,所以返回地址也在堆栈上。

    也就是说,在 push ebp ,堆栈看起来像:

     ...
     param2
     param1
     param0
     return_address  <- esp
    

    打电话之后 推EBP 它变成

     ...
     param2
     param1
     param0
     return_address
     ebp            <- esp
    

    最后, mov ebp, esp 存储此 esp 进入之内 ebp ,因此您可以引用返回地址和与 EBP ,并释放堆栈以供本地使用。

        2
  •  0
  •   Paul R    16 年前

    这都是ABI的一部分。按照惯例,调用方创建 stack frame 它包含参数等,然后调用函数(在这个过程中,返回地址也被推送到堆栈上)。被调用的函数将在堆栈上为局部变量分配额外的空间,并且可以通过一个公共指针和偏移量引用参数和局部变量。

    推荐文章