Inline assembler explained

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:[1]

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.[2] This means the performance might actually decrease. Inline assembler also complicates future porting and maintenance of a program.[1]

Alternative facilities are often provided as a way to simplify the work for both the compiler and the programmer. Intrinsic functions for special instructions are provided by most compilers and C-function wrappers for arbitrary system calls are available on every Unix platform.

Syntax

In language standards

The ISO C++ standard and ISO C standards (annex J) specify a conditionally supported syntax for inline assembler:

An asm declaration has the form asm-declaration: (string-literal) ;The asm declaration is conditionally-supported; its meaning is implementation-defined.[3]

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:

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.[7] It is still possible to check GCC-style assembly for clobber mistakes with knowledge of the instruction set.[8]

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

The Rust language has since migrated to a syntax abstracting away inline assembly options further than the LLVM (GCC-style) version. It provides enough information to allow transforming the block into an externally-assembled function if the backend could not handle embedded assembly.[7]

Examples

A system call in GCC

Calling an operating system directly is generally not possible under a system using protected memory. The OS runs at a more privileged level (kernel mode) than the user (user mode); a (software) interrupt is used to make requests to the operating system. This is rarely a feature in a higher-level language, and so wrapper functions for system calls are written using inline assembler.

The following C code example shows an x86 system call wrapper in AT&T assembler syntax, using the GNU Assembler. Such calls are normally written with the aid of macros; the full code is included for clarity. In this particular case, the wrapper performs a system call of a number given by the caller with three operands, returning the result.

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.[4]

extern int errno;

int syscall3(int num, int arg1, int arg2, int arg3)

Processor-specific instruction in D

This example of inline assembly from the D programming language shows code that computes the tangent of x using the x86's FPU (x87) instructions.

// Compute the tangent of xreal tan(real x)

For readers unfamiliar with x87 programming, the followed by conditional jump idiom is used to access the x87 FPU status word bits C0 and C2. stores the status in a general-purpose register; sahf sets the FLAGS register to the higher 8 bits of the register; and the jump is used to judge on whatever flag bit that happens to correspond to the FPU status bit.[13]

External links

Notes and References

  1. Web site: DontUseInlineAsm . GCC Wiki . 21 January 2020.
  2. Web site: Striegel . Ben . "To a compiler, a blob of inline assembly is like a slap in the face.". Reddit . 13 January 2020 . 15 January 2020.
  3. C++, [dcl.asm]
  4. Web site: Extended Asm - Assembler Instructions with C Expression Operands . Using the GNU C Compiler . 15 January 2020.
  5. Web site: Inline Assembler . docs.microsoft.com . en-us.
  6. Web site: Migration and Compatibility Guide: Inline assembly with Arm Compiler 6.
  7. Web site: d'Antras . Amanieu . Rust RFC-2873: stable inline asm . 15 January 2020 . 13 December 2019. However it is possible to implement support for inline assembly without support from the compiler backend by using an external assembler instead. . Pull Request for status tracking
  8. Web site: ⚙ D54891 [RFC] Checking inline assembly for validity ]. reviews.llvm.org.
  9. Web site: LLVM Language Reference: Inline assembly expressions . LLVM Documentation . 15 January 2020.
  10. Web site: Inline Assembly . Rust Documentation (1.0.0) . 15 January 2020.
  11. Web site: Inline Assembler . D programming language . 15 January 2020.
  12. Web site: LDC inline assembly expressions . D Wiki . 15 January 2020.
  13. Web site: FSTSW/FNSTSW — Store x87 FPU Status Word . The FNSTSW AX form of the instruction is used primarily in conditional branching....