在内联汇编程序内调用 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 专用