现在的位置: 首页 > 综合 > 正文

MSDN帮助文档中关于内联函数学习笔记

2013年09月08日 ⁄ 综合 ⁄ 共 6126字 ⁄ 字号 评论关闭

--------------------------Naked
Function Calls关键字在MSDN中--------------------------------------------

Microsoft Specific
Functions declared with the naked attribute are emitted without prolog or epilog code, enabling you to write your own custom prolog/epilog sequences using the inline assembler. Naked functions are provided as an advanced feature. They enable you to declare
a function that is being called from a context other than C/C++, and thus make different assumptions about where parameters are, or which registers are preserved. Examples include routines such as interrupt handlers. This feature is particularly useful for
writers of virtual device drivers (VxDs). 

实例函数原型:
__declspec( naked ) int func( int a, int b )
{
__asm ret
}

--------------------------Considerations for Writing Prolog/Epilog Code--------------------------
Microsoft Specific
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 symbol.

Stack Frame Layout
push        ebp                ; Save ebp
mov         ebp, esp           ; Set stack frame pointer
sub         esp, localbytes    ; Allocate space for locals
push        <registers>        ; Save 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. 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). The base pointer (ebp) points to the pushed value of ebp. The locals area begins at ebp-2. To access local variables, calculate an offset from ebp by subtracting the appropriate value from ebp.

__LOCAL_SIZE
The compiler provides a symbol, __LOCAL_SIZE, for use in the inline assembler block of function prolog code. This symbol is used to allocate space for local variables on the stack frame in custom prolog code.

The compiler determines the value of __LOCAL_SIZE. Its value is the total number of bytes of all user-defined local variables and compiler-generated temporary variables. __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 symbol. For example:

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

The following example of a naked function containing custom prolog and epilog sequences uses the __LOCAL_SIZE symbol in the prolog sequence:

// the__local_size_symbol.cpp
// processor: x86
__declspec ( naked ) int main() {
   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
      }
}

------------------------__declspec keyword关键字在MSDN中----------------------------------------------
The extended attribute syntax for specifying storage-class information uses the __declspec keyword, which specifies that an instance of a given type is to be stored with a Microsoft-specific storage-class attribute listed below. Examples of other storage-class
modifiers include the static and extern keywords. However, these keywords are part of the ANSI specification of the C and C++ languages, and as such are not covered by extended attribute syntax. The extended attribute syntax simplifies and standardizes Microsoft-specific
extensions to the C and C++ languages.

decl-specifier: 
__declspec ( extended-decl-modifier-seq )

extended-decl-modifier-seq: 
extended-decl-modifieropt

extended-decl-modifier extended-decl-modifier-seq

extended-decl-modifier: 
align(#) 

allocate("segname") 

appdomain 

deprecated 

dllimport 

dllexport 

jitintrinsic

naked 

noalias 

noinline 

noreturn 

nothrow 

novtable 

process 

property({get=get_func_name|,put=put_func_name}) 

restrict 

selectany 

thread 

uuid("ComObjectGUID")

White space separates the declaration modifier sequence. Examples appear in later sections.

Extended attribute grammar supports these Microsoft-specific storage-class attributes: align, allocate, appdomain, deprecated, dllexport, dllimport, jitintrinsic, naked, noalias, noinline, noreturn, nothrow, novtable, process, restrict, selectany, and thread.
It also supports these COM-object attributes: property and uuid. 

The dllexport, dllimport, naked, noalias, nothrow, property, restrict, selectany, thread, and uuid storage-class attributes are properties only of the declaration of the object or function to which they are applied. The thread attribute affects data and objects
only. The naked attribute affects functions only. The dllimport and dllexport attributes affect functions, data, and objects. The property, selectany, and uuid attributes affect COM objects.

The __declspec keywords should be placed at the beginning of a simple declaration. The compiler ignores, without warning, any __declspec keywords placed after * or & and in front of the variable identifier in a declaration.

A __declspec attribute specified in the beginning of a user-defined type declaration applies to the variable of that type. For example:

  Copy Code 
__declspec(dllimport) class X {} varX;
 

In this case, the attribute applies to varX. A __declspec attribute placed after the class or struct keyword applies to the user-defined type. For example:

  Copy Code 
class __declspec(dllimport) X {};
 

In this case, the attribute applies to X.

The general guideline for using the __declspec attribute for simple declarations is as follows:

  Copy Code 
decl-specifier-seqdeclarator-list;
 

The decl-specifier-seq should contain, among other things, a base type (e.g. int, float, a typedef, or a class name), a storage class (e.g. static, extern), or the __declspec extension. The init-declarator-list should contain, among other things, the pointer
part of declarations. For example:

  Copy Code 
__declspec(selectany) int * pi1 = 0;   //OK, selectany & int both part of decl-specifier
int __declspec(selectany) * pi2 = 0;   //OK, selectany & int both part of decl-specifier
int * __declspec(selectany) pi3 = 0;   //ERROR, selectany is not part of a declarator
 

The following code declares an integer thread local variable and initializes it with a value:

  Copy Code 
// Example of the __declspec keyword
__declspec( thread ) int tls_i = 1;

----------------------Calling C Functions in Inline Assembly内联汇编中调用C库函数---------Inline Assembler关键字---------

#include <stdio.h>
#include <Windows.h>

char format[] = "%s %s\n";
char hello[] = "Hello";
char world[] = "world";

//在内嵌汇编中调用C库函数
int __declspec(naked) main( void )
{
__asm
{
push offset world
push offset hello
push offset format
call dword ptr printf
add esp,0ch

call dword ptr getchar

xor eax,eax
push eax
push offset world
push offset hello
push eax
call dword ptr MessageBox

ret
}

}

说明:内联汇编的使用会影响编译器对代码的优化

抱歉!评论已关闭.