Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to insert assembly code into C?

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.

like image 212
Nathan Avatar asked Sep 14 '08 13:09

Nathan


People also ask

Can we write assembly code in C?

We can write assembly program code inside c language program. In such case, all the assembly code must be placed inside asm{} block.

Which macro is used to insert assembly code in C program?

C macros offer a convenient way to insert assembly code into your source code, but they demand extra care because a macro expands into a single logical line. To create trouble-free macros, follow these rules: Enclose the __asm block in braces.

What is assembly code in C language?

An assembly language is a low-level programming language designed for a specific type of processor. It may be produced by compiling source code from a high-level programming language (such as C/C++) but can also be written from scratch. Assembly code can be converted to machine code using an assembler.

Does C go to assembly?

Related: Does a compiler always produce an assembly code? - no, big mainstream C compilers that provide a complete toolchain often go straight to machine code, especially ones (unlike GCC) that only target a few ISAs / object file formats.


1 Answers

Using GCC

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

Using VC++

__asm {   mov eax, edx   add eax, 2 } 
like image 148
Niall Avatar answered Sep 18 '22 21:09

Niall