Is there a way to insert assembly code into C?

A good start would be reading this article which talk about inline assembly in C/C++:

http://www.codeproject.com/KB/cpp/edujini_inline_asm.aspx

Example from the article:

#include <stdio.h>


int main() {
    /* Add 10 and 20 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                "movl $20, %ebx;"
                "addl %ebx, %eax;"
    );

    /* Subtract 20 from 10 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                    "movl $20, %ebx;"
                    "subl %ebx, %eax;"
    );

    /* Multiply 10 and 20 and store result into register %eax */
    __asm__ ( "movl $10, %eax;"
                    "movl $20, %ebx;"
                    "imull %ebx, %eax;"
    );

    return 0 ;
}

In GCC, there's more to it than that. In the instruction, you have to tell the compiler what changed, so that its optimizer doesn't screw up. I'm no expert, but sometimes it looks something like this:

    asm ("lock; xaddl %0,%2" : "=r" (result) : "0" (1), "m" (*atom) : "memory");

It's a good idea to write some sample code in C, then ask GCC to produce an assembly listing, then modify that code.


Using GCC

__asm__("movl %edx, %eax\n\t"
        "addl $2, %eax\n\t");

Using VC++

__asm {
  mov eax, edx
  add eax, 2
}