在內嵌組譯碼中呼叫 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 );

此範例將推入指標來world, hello,以及format、 該順序],然後呼叫printf

結束 Microsoft 特定

請參閱

參考

內嵌組譯工具