Delen via


C-functies aanroepen in inline assembly

Microsoft-specifieke

Een __asm blok kan C-functies aanroepen, waaronder C-bibliotheekroutines. In het volgende voorbeeld wordt de printf bibliotheekroutine aanroepen:

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

Omdat functieargumenten op de stack worden doorgegeven, pusht u de benodigde argumenten (string-pointers, in het vorige voorbeeld) voordat u de functie aanroept. De argumenten worden in omgekeerde volgorde gepusht, zodat ze uit de stapel komen in de gewenste volgorde. Om de C-instructie te emuleren

printf( format, hello, world );

in het voorbeeld worden aanwijzers naar world, hello en format gepusht in die volgorde en vervolgens wordt printf aangeroepen.

Microsoft-specifieke beƫindigen

Zie ook

Inline-assembler