Is there a way to insert assembly code into C?

CInline AssemblyAssembly

C Problem Overview


I remember back in the day with the old borland DOS compiler you could do something like this:

asm {
 mov ax,ex
 etc etc...
}

Is there a semi-platform independent way to do this now? I have a need to make a BIOS call, so if there was a way to do this without asm code, that would be equally useful to me.

C Solutions


Solution 1 - C

Using GCC

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

Using VC++

__asm {
  mov eax, edx
  add eax, 2
}

Solution 2 - C

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.

Solution 3 - 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 ;
}

Solution 4 - C

For Microsoft compilers, inline assembly is supported only for x86. For other targets you have to define the whole function in a separate assembly source file, pass it to an assembler and link the resulting object module.

You're highly unlikely to be able to call into the BIOS under a protected-mode operating system and should use whatever facilities are available on that system. Even if you're in kernel mode it's probably unsafe - the BIOS may not be correctly synchronized with respect to OS state if you do so.

Solution 5 - C

use of asm or __asm__ function ( in compilers have difference )

also you can write fortran codes with fortran function

asm("syscall");
fortran("Print *,"J");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathanView Question on Stackoverflow
Solution 1 - CNiallView Answer on Stackoverflow
Solution 2 - CMartin Del VecchioView Answer on Stackoverflow
Solution 3 - CEspoView Answer on Stackoverflow
Solution 4 - CMike DimmickView Answer on Stackoverflow
Solution 5 - Cuser15049586View Answer on Stackoverflow