In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a higher-level language such as C or Ada.

Motivation and alternatives

The embedding of assembly language code is usually done for one of these reasons:

  • Optimization: Programmers can use assembly language code to implement the most performance-sensitive parts of their program's algorithms, code that is apt to be more efficient than what might otherwise be generated by the compiler.
  • Access to processor-specific instructions: Most processors offer special instructions, such as Compare and Swap and Test and Set instructions which may be used to construct semaphores or other synchronization and locking primitives. Nearly every modern processor has these or similar instructions, as they are necessary to implement multitasking. Examples of specialized instructions are found in the SPARC VIS, Intel MMX and SSE, and Motorola Altivec instruction sets.
  • Access to special calling conventions not yet supported by the compiler.
  • System calls and interrupts: High-level languages rarely have a direct facility to make arbitrary system calls, so assembly code is used. Direct interrupts are even more rarely supplied.
  • To emit special directives for the linker or assembler, for example to change sectioning, macros, or to make symbol aliases.

On the other hand, inline assembler poses a direct problem for the compiler itself as it complicates the analysis of what is done to each variable, a key part of register allocation. This means the performance might actually decrease. Inline assembler also complicates future porting and maintenance of a program.

</poem></blockquote>

This definition, however, is rarely used in actual C, as it is simultaneously too liberal (in the interpretation) and too restricted (in the use of one string literal only).

In actual compilers

In practical use, inline assembly operating on values is rarely standalone as free-floating code. Since the programmer cannot predict what register a variable is assigned to, compilers typically provide a way to substitute them in as an extension.

There are, in general, two types of inline assembly supported by C/C++ compilers:

  • (or ) in GCC. GCC uses a direct extension of the ISO rules: assembly code template is written in strings, with inputs, outputs, and clobbered registers specified after the strings in colons. C variables are used directly while register names are quoted as string literals.
  • in Microsoft Visual C++ (MSVC), Borland/Embarcadero C compiler, and descendants. This syntax is not based on ISO rules at all; programmers simply write ASM inside a block without needing to conform to C syntax. Variables are available as if they are registers and some C expressions are allowed. ARM Compiler used to have a similar facility.

The two families of extensions represent different understandings of division of labor in processing inline assembly. The GCC form preserves the overall syntax of the language and compartmentizes what the compiler needs to know: what is needed and what is changed. It does not explicitly require the compiler to understand instruction names, as the compiler is only needed to substitute its register assignments, plus a few operations, to handle the input requirements. However, the user is prone to specifying clobbered registers incorrectly. The MSVC form of an embedded domain-specific language provides ease of writing, but it requires the compiler itself to know about opcode names and their clobbering properties, demanding extra attention in maintenance and porting. It is still possible to check GCC-style assembly for clobber mistakes with knowledge of the instruction set.

GNAT (Ada language frontend of the GCC suite), and LLVM uses the GCC syntax. The D programming language uses a DSL similar to the MSVC extension officially for x86_64, but the LLVM-based LDC also provides the GCC-style syntax on every architecture. MSVC only supports inline assembler on 32-bit x86.

To recap, GCC supports both basic and extended assembly. The former simply passes text verbatim to the assembler, while the latter performs some substitutions for register locations.

References

  • GCC-Inline-Assembly-HOWTO
  • Clang Inline assembly
  • GNAT Inline Assembler
  • GCC Inline Assembler Reference
  • Compiler Explorer