编写 Prolog/Epilog 代码时的注意事项Considerations When Writing Prolog/Epilog Code

Microsoft 专用Microsoft Specific

在编写你自己的 prolog 和 epilog 代码序列之前,请务必了解堆栈帧的布局方式。了解如何使用 __LOCAL_SIZE 预定义的常量也很有用。Before writing your own prolog and epilog code sequences, it is important to understand how the stack frame is laid out. It is also useful to know how to use the __LOCAL_SIZE predefined constant.

C 堆栈帧布局C Stack Frame Layout

此示例显示了可能出现在 32 位函数中的标准 prolog 代码:This example shows the standard prolog code that might appear in a 32-bit function:

push     ebp                 ; Save ebp  
mov      ebp, esp            ; Set stack frame pointer  
sub      esp, localbytes     ; Allocate space for locals  
push     <registers>         ; Save registers  

localbytes 变量表示局部变量堆栈上所需的字节数,registers 变量是表示要保存在堆栈上的寄存器列表的占位符。The localbytes variable represents the number of bytes needed on the stack for local variables, and the registers variable is a placeholder that represents the list of registers to be saved on the stack. 推入寄存器后,您可以将任何其他适当的数据放置在堆栈上。After pushing the registers, you can place any other appropriate data on the stack. 下面是相应的 epilog 代码:The following is the corresponding epilog code:

pop      <registers>         ; Restore registers  
mov      esp, ebp            ; Restore stack pointer  
pop      ebp                 ; Restore ebp  
ret                          ; Return from function  

堆栈始终向下增长(从高内存地址到低内存地址)。The stack always grows down (from high to low memory addresses). 基指针 (ebp) 指向 ebp 的推入值。The base pointer (ebp) points to the pushed value of ebp. 局部变量区域从 ebp-2 开始。The local variables area begins at ebp-2. 若要访问局部变量,可通过从 ebp 中减去适当的值来计算 ebp 的偏移量。To access local variables, calculate an offset from ebp by subtracting the appropriate value from ebp.

__LOCAL_SIZE 常量The __LOCAL_SIZE Constant

编译器提供常量 __LOCAL_SIZE 以用于函数 prolog 代码的内联汇编程序块。The compiler provides a constant, __LOCAL_SIZE, for use in the inline assembler block of function prolog code. 此常数用于在自定义 prolog 代码中的堆栈帧上为局部变量分配空间。This constant is used to allocate space for local variables on the stack frame in custom prolog code.

编译器确定 __LOCAL_SIZE 的值。The compiler determines the value of __LOCAL_SIZE. 该值是所有用户定义的局部变量和编译器生成的临时变量的总字节数。The value is the total number of bytes of all user-defined local variables and compiler-generated temporary variables. __LOCAL_SIZE 只能用作即时操作数;它不能在表达式中使用。__LOCAL_SIZE can be used only as an immediate operand; it cannot be used in an expression. 您不得更改或重新定义此常量的值。You must not change or redefine the value of this constant. 例如: For example:

mov      eax, __LOCAL_SIZE           ;Immediate operand--Okay  
mov      eax, [ebp - __LOCAL_SIZE]   ;Error  

包含自定义 prolog 和 epilog 序列的 naked 函数的以下示例在 prolog 序列中使用 __LOCAL_SIZE:The following example of a naked function containing custom prolog and epilog sequences uses __LOCAL_SIZE in the prolog sequence:

__declspec ( naked ) func()  
{  
   int i;  
   int j;  

   __asm      /* prolog */  
      {  
      push   ebp  
      mov      ebp, esp  
      sub      esp, __LOCAL_SIZE  
      }  

   /* Function body */  

   __asm      /* epilog */  
      {  
      mov      esp, ebp  
      pop      ebp  
      ret  
      }  
}     

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

naked 函数Naked Functions