Last update Fri Dec 25 00:35:54 2015

Inline Assembler for Über Nerds

written by Walter Bright

July 21, 2011

Inline assembler (IA) is the practice of embedding assembly language instructions in a high level programming language's source code. They're an early example of an EDSL (Embedded Domain Specific Language).

IA isn't needed that often if the host programming language is powerful, but it is used to:

  1. generate special instructions, like locking code for multithreaded synchronization
  2. access special CPU features, such as debug registers
  3. super optimize a very critical section of code
  4. ease into learning assembler
  5. be an intermediate step when translating large assembly language projects into a high level language
  6. build thunks or access code that has special calling conventions
  7. interface to hardware requiring specific instruction sequences to access

Add a special point 8 for me, as I get an endorphin rush from writing a particularly crafty bit of assembler. Yes, I am a nerd.

Look over the source to an operating system or a device driver, and you're likely to find a sprinkling of inline assembler here and there.

Here's a simple example of the use of inline assembler in the D programming language to use the Intel RDTSC instruction to read the clock count into the EDX and EAX registers:

long clock()

long's are returned in EDX and EAX, so this works out famously. For a somewhat longer example to compute the tangent of x using the FPU instructions,

real tan(real x)
   fld     x[EBP]   ; // load x
   fxam             ; // test for oddball values
   fstsw   AX       ;
   sahf             ;
   jc      trigerr  ; // x is NAN, infinity, or empty
                      // 387's can handle denormals
   fptan            ;
   fstp    ST(0)    ; // dump X, which is always 1
   fstsw   AX       ;
   sahf             ;
   jnp     Lret     ; // C2 = 1 (x is out of range)
   // Do argument reduction to bring x into range
   fldpi            ;
   fxch             ;
   fprem1           ;
   fstsw   AX       ;
   sahf             ;
   jp      SC17     ;
   fstp    ST(1)    ; // remove pi from stack
   jmp     SC18     ;
 return real.nan;

Despite there being thousands of different programming languages, only a relative handful support inline assembler. Surprisingly, even C and C++ don't officially have inline assemblers - they are done as extensions by compiler vendors. A C and C++ compiler can be 100% standard compliant with no inline assembler, and these do exist, such as Microsoft VC++ for Win64. I'm a bit old skool in that a language that calls itself a systems programming language ought to support an inline assembler.

The D programming language specifies that inline assembler must be supported.

Some suggest that inline assembler is obsolete, if one really really needs assembler, just use a separate assembler. There are lots of great assemblers out there. Why not?

I abandoned using separate assemblers years ago for the following reasons:

  1. Poor (i.e. zero) integration with the compiler.
  2. You have to rewrite your data structure & manifest constant declarations in the assembler, and of course these always get out of sync with the ones in your D source.
  3. Having the compiler set up the call/return sequences and parameter addressing is so darned convenient.
  4. The compiler will keep track of register usage for you — which registers are read or written, and so smoothly integrating with the code generator's usage of registers.
  5. There are lots of 3rd party assemblers, all different. Even the same assembler will have multiple versions. The chances of the asm source assembling on all of them, and avoiding all the various bugs in them, is zero. It was a major tech support issue.
  6. It really hurts my brain to have gas (Gnu ASsembler) swap the order of the operands.
  7. gas doesn't follow the Intel syntax so you have to do a mental translation from the Intel datasheets to the gas source. gas doesn't even use the same instruction names.
  8. External assemblers don't do name mangling. You've got to do it all manually. This is a horror.
  9. Symbolic debug formats differ.
  10. . Having to manage a separate source file for just two instructions was highly annoying.
  11. . Writing an inline assembler isn't hard. There's nothing terribly clever about it.

Getting the assembler integrated into the compiler made me much more productive and my life much easier. It was a giant win for this nerd, no doubt about it. You can pry my inline assembler from my cold, dead fingers.


Thanks to Andrei Alexandrescu, Bartosz Milewski, and David Held for their helpful comments on a draft of this.

Home | Runtime Library | IDDE Reference | STL | Search | Download | Forums