Syntax of Naked Function Calls

HomeOverviewHow Do I

Because the naked attribute is not a type modifier, naked functions use the extended attribute syntax. For example, the following code defines a function with the naked attribute:

__declspec( naked ) int func( formal_parameters )
{
    // Function body
}

Or, alternatively:

#define Naked   __declspec( naked )
Naked int func( formal_parameters )
{
    // Function body
}

The naked modifier affects only the nature of the compiler’s code generation for the function’s prolog and epilog sequences. It does not affect the code that is generated for calling such functions. Thus, the naked attribute is not considered part of the function’s type. As such, function pointers cannot have the naked attribute. Furthermore, the naked attribute has no meaning when applied to a data definition. Any attempt to apply it to a data definition will generate a compiler error. For example, the following code samples will generate errors:

__declspec( naked ) int i;  // Error--naked attribute not permitted on
                            // data declarations.

extern __declspec( naked ) int i;   // Error--naked attribute not
                                    // permitted on data declarations.

The naked attribute is relevant only to the definition of the function. Thus it cannot be specified on the function’s prototype. The following declaration will generate a compiler error:

__declspec( naked ) int func();  // Error--naked attribute not permitted
                                 // on function declarations.

The compiler provides a new symbolic argument for use in the inline assembler block of function prolog code. This symbol, __LOCAL_SIZE, is used to allocate space for local variables on the stack frame in your custom prolog code. This constant contains a value determined by the compiler, and it represents the number of bytes of local variables.

__LOCAL_SIZE includes all user-defined local variables as well as compiler-generated temporary variables. __LOCAL_SIZE may be used as an immediate operand or in an expression. For example:

mov     eax, __LOCAL_SIZE           /* Immediate operand */
mov     eax, __LOCAL_SIZE + 4       /* Expression */
mov     eax, [ebp - __LOCAL_SIZE]   /* Expression */