Is there a way to insert assembly code in C?

I remember that day with the old DOS Borland compiler that you could do something like this:

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

Is there an independent, semi-paid way to do this now? I need to make a BIOS call, so if there was a way to do this without the asm code, it would also be useful to me.

+46
c assembly inline-assembly
Sep 14 '08 at 13:55
source share
4 answers

Using GCC

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

Using VC ++

 __asm { mov eax, edx add eax, 2 } 
+53
Sep 14 '08 at 14:05
source share

GCC has more than that. In the instructions, you must tell the compiler what has changed so that its optimizer does not hang. I am not an expert, but sometimes it looks something like this:

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

It is a good idea to write sample code in C, then ask GCC to create a list of assemblies, and then modify that code.

+9
Sep 15 '08 at 0:10
source share

A good start is to read this article, which talks about the built-in 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 ; } 
+4
Sep 14 '08 at 13:57
source share

Microsoft non-x86 compilers do not support stream assembly. You must define the entire function in a separate assembly source file and pass it to the assembler.

You are unlikely to be able to call the BIOS on an operating system with protected mode and use any means available on this system. Even if you are in kernel mode, this is probably unsafe - the BIOS may not be properly synchronized with respect to the OS state if you do this.

+2
Sep 15 '08 at 14:55
source share



All Articles