内联汇编程序 (C)Inline Assembler (C)

Microsoft 专用Microsoft Specific

利用内联汇编程序,您可以直接在 C 源程序中嵌入汇编语言指令,而无需额外的程序集和链接步骤。The inline assembler lets you embed assembly-language instructions directly in your C source programs without extra assembly and link steps. 内联汇编程序生成到该编译器中,因此您不需要一个单独的汇编程序,例如 Microsoft Macro Assembler (MASM)。The inline assembler is built into the compiler — you don't need a separate assembler such as the Microsoft Macro Assembler (MASM).

由于内联汇编程序不需要单独的程序集和链接步骤,因此它比单独的汇编程序更方便。Because the inline assembler doesn't require separate assembly and link steps, it is more convenient than a separate assembler. 内联程序集代码可以使用任何 C 变量或范围中的函数名,因此,将其与程序的 C 代码集成非常容易。Inline assembly code can use any C variable or function name that is in scope, so it is easy to integrate it with your program's C code. 由于程序集代码可以与 C 语句混合,因此它可以完成在单独的 C 中难以完成或无法完成的任务。And because the assembly code can be mixed with C statements, it can do tasks that are cumbersome or impossible in C alone.

__asm 关键字调用内联汇编程序,并且可在 C 语句合法的任何位置显示。The __asm keyword invokes the inline assembler and can appear wherever a C statement is legal. 它不能单独出现。It cannot appear by itself. 它必须后跟一个程序集指令、一组括在大括号中的指令或者至少一对空大括号。It must be followed by an assembly instruction, a group of instructions enclosed in braces, or, at the very least, an empty pair of braces. 此处的术语“__asm 块”指任何指令或指令组(无论是否在大括号中)。The term "__asm block" here refers to any instruction or group of instructions, whether or not in braces.

以下代码是括在大括号内的简单 __asm 块。The code below is a simple __asm block enclosed in braces. (此代码是一个自定义函数 prolog 序列。)(The code is a custom function prolog sequence.)

__asm  
{  
   push ebp  
   mov  ebp, esp  
   sub  esp, __LOCAL_SIZE  
}  

或者,您还可以将 __asm 放在每个程序集指令前面:Alternatively, you can put __asm in front of each assembly instruction:

__asm push ebp  
__asm mov  ebp, esp  
__asm sub  esp, __LOCAL_SIZE  

虽然 __asm 关键字是语句分隔符,但您也可将程序集说明置于相同的行上:Since the __asm keyword is a statement separator, you can also put assembly instructions on the same line:

__asm push ebp   __asm mov  ebp, esp   __asm sub  esp, __LOCAL_SIZE   

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

函数特性Function Attributes