Integrated assembler

from Wikipedia, the free encyclopedia

Inline assembler (also known as integrated assembler code ) is a program part written in assembly language that is integrated in the source text of a higher-level programming language . The embedding of the assembler code part is declared by means of special instructions (e.g. starting with asm , __asm__ or _asm_ and possibly with further information) and thus made known to the compiler .

use

Many compilers use assembler source code to compile their high-level language anyway. The inline assembler source code can be taken directly from the compiler. The advantages are that you can write program parts in assembler instead of whole functions (as in the case of integration via the linker), that direct access to the symbols and variables of the high-level language is possible and that, conversely, the compiler also sees the assembler code and its Can consider optimization. In the GNU assembler z. B. the communication between the high-level language code part and the assembler code part is made possible via an assembler template . It defines the mapping of input and output variables from the high-level language to assembly language accessible register as elsewhere used in the assembler part register ( Clobber List ) corresponding to the high-level language compiler will be announced.

In the alternative customary variant of using assembler code parts in high-level language programs, program parts are linked as machine code from possibly different languages ​​on the binary linker level and called up via the function interface . The advantage is that no compiler support is required; Disadvantages are the calling conventions that vary between operating systems , which make portability more difficult, and the additional command overhead caused by the function call. These problems do not occur when using inline assembler .

The newest variant is the use of so-called assembler intrinsics , effectively macros that hide the assembler syntax, which should allow the easier use of SIMD instructions.

Example (x86)

Example of a C program with an inline assembler code part in AT&T syntax ( GNU assembler ), which transfers the values ​​of two variables to the inline assembler code part, adds them, then increases the result by the value 1 and then to the C code part passes on. At the end the result is output (here 10):

#include <stdio.h>

int main(void)
{
  int foo = 5;
  int bar = 4;

  /* Hier beginnt der Inline-Assembler Abschnitt in AT&T-Syntax */
  __asm__ (
      "add %1, %0\n\t" /* Addiert den Wert von Operand %1 zum Wert von Operand %0. */
      "inc %0"         /* Erhöht den Wert von Operand %0 um 1. */

      /* Definition der Nebenbedingungen:
       * Diese weisen den C-Variablen der Reihe nach aufzählend einen für den Inline-Assemblercode nutzbaren Operanden
       * zu und teilen dem Compiler mit, auf welche Weise (lesend und/oder schreibend) dieser im Inline-Assemblercode
       * verwendet werden kann und auf welche Register dieser beschränkt ist. Dadurch wird eine korrekte und effiziente
       * Übergabe der Variablenwerte in und aus dem Assemblercodeabschnitt gewährleistet. */
      : "+r" (bar) /* Gibt an, dass die Variable bar sowohl gelesen als auch beschrieben ('+') wird und deren Wert in
                    * ein allgemeines Register ('r') zu platzieren ist.
                    * Als erster Operand wird er im Assemblercodeabschnitt unter der Bezeichnung %0 genutzt. */
      : "g" (foo)  /* Beschränkt die Verwendung der Variable foo nur zum Lesen. Sie kann auf beliebige Weise ('g', im
                    * Speicher, in einem Register oder als Direktwert) an den Assemblerteil übergeben werden.
                    * Als zweiter Operand wird er im Assemblercodeabschnitt unter der Bezeichnung %1 genutzt */
      : "cc"       /* Gibt an, dass die Statusanzeige (durch die Befehle add und inc) verändert wurde. */
  );

  /* Hier geht's mit C Code weiter. */
  printf("Ergebnis: %i\n", bar);
  return 0;
}

List of compilers with inline assembler support

Examples of compilers with inline assembler (alphabetical):

Web links

Wikibooks: Assembler programming for x86 processors  - learning and teaching materials

Individual evidence

  1. GCC-Inline-Assembly-HOWTO - Section 5.1 Assembler Template (English)
  2. MMX, SSE, and SSE2 Intrinsics . Microsoft Developer Network (English)