Skip to content

Latest commit

 

History

History
54 lines (44 loc) · 1.67 KB

calling-c-functions-in-inline-assembly.md

File metadata and controls

54 lines (44 loc) · 1.67 KB
description title ms.date helpviewer_keywords ms.assetid
Learn more about: Calling C Functions in Inline Assembly
Calling C Functions in Inline Assembly
08/30/2018
function calls, C functions
function calls, in inline assembly
functions [C], calling in inline assembly
Visual C, functions
inline assembly, calling functions
__asm keyword [C++], calling functions
f8a8d568-d175-4e23-9b24-36ef60a4cab3

Calling C Functions in Inline Assembly

Microsoft Specific

An __asm block can call C functions, including C library routines. The following example calls the printf library routine:

// 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
   }
}

Because function arguments are passed on the stack, you simply push the needed arguments — string pointers, in the previous example — before calling the function. The arguments are pushed in reverse order, so they come off the stack in the desired order. To emulate the C statement

printf( format, hello, world );

the example pushes pointers to world, hello, and format, in that order, and then calls printf.

END Microsoft Specific

See also

Inline Assembler