ASM in C++

Is there a way to incorporate ASM in C++?  
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

teratomaConnect With a Mentor Commented:
asm is a standard C and C++ keyword.  However, the format of the assembler code is undefined by the C/C++ standards, as it's dependent on the target CPU.  That's how to embed assembler code within C code.  To call a function defined in assembler from C, you just declare it "extern":

extern void func();

In C++ you have to declare it extern "C" to turn off name mangling:

extern "C" void func();

In the assembler it'll look something like

PROC func

You can do the same with data:

extern "C" mydata;

in the asm file:

PUBLIC mydata
LABEL mydata

or even

STRUC mydata

Where the data structure is defined in parallel in asm and C++.  The syntax depends on what assembler you're using.

You can also call a C function from an assembler function, at least with Intel processors, using the "call" instruction.  This is highly non-portable if parameters need to be passed.  In typical applications this kind of thing is usually done by passing a function pointer at runtime to the low-level code.  This is usually for interrupt service routines (ISRs) which don't take or return values.
AlexFMConnect With a Mentor Commented:
In Microsoft VC++ you can use inline Assembly blocks __asm:

For example:

void SomeFunction()
    int x = 2;

        mov al, x

example code if you are doing in g++
All Courses

From novice to tech pro — start learning today.