在內嵌組譯碼中呼叫 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
。
END Microsoft 特定的
另請參閱
意見反映
https://aka.ms/ContentUserFeedback。
即將推出:我們會在 2024 年淘汰 GitHub 問題,並以全新的意見反應系統取代並作為內容意見反應的渠道。 如需更多資訊,請參閱:提交及檢視以下的意見反映: