// 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 - does not work
//The call instruction wants to use the "__imp__printf" directly
//as an address where the printf function is.
//
//solution 1 below works - it puts the address of the function into esi
//and performs an indirect call to that function using the address in esi.
//mov esi,printf
//call esi
//
//or solution 2 below works
call DWORD PTR printf
//or
//call dword ptr [esi+printf]
//The call instruction uses __imp__printf as a pointer to this //address.
//reason to use this: the address of the printf function is unknown before
runtime
//because the library is dynamically linked.
//Another way to fix it is to change the C runtime linking to /MT or /MTd
//
//Stack clean-up
//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
}
}
//See for more:
http://www.codeproject.com/Messages/3168938/Calling-C-Functions-in-Inline-Assembly-MSDN-exampl.aspx