views:

232

answers:

4

I have read in a lot of places that assembly language is not usually used to create complete programs, but is used by other programs to make certain procedures more efficient, particularly the ones that are called a couple thousand times a second. I am wondering how small bits of assembly code are incorporated into larger programs.

  • I thought that a small executable could be made and then run from another program, but that seems inefficient.

  • Then I thought about the inline assembly for Visual Studio, but that's specific to Microsoft, and it seems like there would be a better way.

So how can small bits of assembly code be used within a larger program without creating separate programs or using the Visual Studio inline assembly?

+5  A: 

Inline assembly is not specific to Visual Studio - GCC supports it as well. Inline assembly is typically the easiest way to incorporate assembly into a program. There are a couple issues however - it can greatly affect the portability of your code, and some compilers do not support it - for instance, Microsoft's x64 compiler does not support it.

For compilers that do not support inline assembly or when you want to contain all machine specific code, you typically separate the assembly language specific portions into their own files, exposing the assembly code as functions that your C++ code can call. You then assemble and link them as part of the build. Your linker doesn't care what language was used to generate the object files - it can assemble object files from multiple languages into a single program.

Michael
I lost my taste for inline assembly some time ago.
Joshua
Same here - having to code and test the same bit of assembly for each CPU we support as well as a C fallback gets really old.
Michael
Still beats having to code html/css/js to IEx !!! I'm ready to pull my hair out
Jason Watts
+1  A: 

Here's an example (GCC):

__asm__ ("movl %eax, %ebx\n\t"
"movl $56, %esi\n\t"
"movl %ecx, $label(%edx,%ebx,$4)\n\t"
"movb %ah, (%ebx)");

For Microsoft Inline Assembly, the keyword is __asm, the assembly code is wrapped in curly braces, it's not a string, and the destination register is now on the left.

Microsoft example:

int foo(void) {
  __asm{
   mov eax,100 ; Moves 100 into eax!
   leave
   ret
  };
}

Links for more information

Inline Assembly Instructions and Reference: http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

IBM tutorial, specific to x86: http://www.ibm.com/developerworks/linux/library/l-ia.html

mandaleeka
A: 

For small bits of code like inner loops, it would usually be done with inline assembly. And that is definitely not Microsoft specific! (GCC and pretty much every modern compiler supports it too, although with different variations in syntax).

For larger chunks of code (for example, a bootloader for an operating system), it would usually be put in a separate source file (just like you use multiple source files with every language), and then linked in with the rest of the program using the linker.

Keep in mind that assembly is mostly just used for OSs nowadays, because compilers are now good enough that it doesn't help much to use it for inner loops in things like games (and it is not at all portable to different architectures). It is mainly used for really low level stuff like hardware interrupts and setting up paging (virtual memory).

Zifre
Don't forget small (8 or 16-bit) embedded programming. Assembly is quite common. And the bane of my existence! ☺In general, assembly should be avoided and if used, heavily commented.
Harold Bamford
A: 

Then I thought about the inline assembly for Visual Studio, but that's specific to Microsoft, and it seems like there would be a better way.

Every compiler supports some form of inline assembly, though usually in slightly different ways.

So if you need to use inline assembly, encapsulate each snippet in a small function. Then you can reimplement these functions without much effort when porting to another compiler.

starblue