共用方式為


在內嵌組譯碼中呼叫 C 函式

Microsoft 特定的

__asm 區塊可以呼叫 C 函式,包括 C 程式庫常式。 下列範例會呼叫 printf 程式庫常式:

// InlineAssembler_Calling_C_Functions_in_Inline_Assembly.cpp
// processor: x86
#include <stdio.h>

char format[] = "%s %s\n";
char hello[] = "Hello";
char world[] = "world";
int main( void )
{
   __asm
   {
      mov  eax, offset world
      push eax
      mov  eax, offset hello
      push eax
      mov  eax, offset format
      push eax
      call printf
      //clean up the stack so that main can exit cleanly
      //use the unused register ebx to do the cleanup
      pop  ebx
      pop  ebx
      pop  ebx
   }
}

由於函式引數會在堆疊傳遞,您只要在呼叫函式之前推入需要的引述 (如前述範例中的字串指標) 即可。 引數會以反向順序推入,所以會依預期順序退出堆疊。 模擬 C 陳述式

printf( format, hello, world );

這個範例會將指標依序推入 worldhelloformat,然後呼叫 printf

END Microsoft 特定的

另請參閱

內嵌組合語言