Inline assembler

Inline assembler (or integrated assembler code ) is an assembly language part, which is integrated in a written in a high level language source code for a program. The transition is by means of a special instruction (often: asm, __ asm__ or _asm_ ) declared the compiler.

Use

Many compilers go when compiling their high-level language anyway the way through assembler source code. Here, the inline assembler source code can be used directly by the compiler. Advantages are that you can write program components in assembly language instead of entire 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 sees the assembly code and its may take into account optimization. When GNU assembler, for example, the communication between high-level language code part and assembly code part on an assembler template allows. It defines the mapping of input and output variables of the high-level language to assembler accessible register as otherwise register used in the assembler part ( Clobber List) which are made ​​known to the Hochsprachkompiler.

The other common variant assembly code parts integrate high-level language programs is on the binary linker level after assembly, the function interface. Advantage is that no compiler support for them is necessary; Disadvantages are the between operating systems varying Calling conventions that hinder portability and the additional computational overhead by the function call. These problems do not occur with the use of inline assembler.

The latest variant is the use of so-called assembly language intrinsics, effective macros that hide the assembler syntax that will allow easier use of SIMD instructions.

X86 example

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

# include   int main ( void) {    int foo = 5;    int bar = 4;      / * Here the inline assembler section begins in AT & T syntax * /    __asm__ (        "add % 1,% 0 \ n \ t" / * Adds the value of operand% 1 to the value of operand% 0 * /        "inc % 0 " / * Increases the value of operand% 0 to 1 * /          / * Definition of the constraints:         * These have the C variables sequentially enumerating a useful for inline assembler code operands         * And to tell the compiler in which way (read and / or write) this in inline assembler code         Can be * used and to which register this is limited. This is a correct and efficient         * Transfer the values ​​of variables guaranteed to and from the assembly code section. * /       : " R " (bar) / * Indicates that the variable read both bar and described ( ' ') is and its value in                      * A general register ( ' r') is to be placed.                      * The first operand it is used in the assembler code section under the name of 0 %. * /       : "G" (foo ) / * Limits the use of the variable foo for reading only. You can in any way ('g ', in                      * Memory is passed in a register or immediate value ) to the assembler part.                      * The second operand, it is used in the assembler code section under the name % 1 * /       : "Cc " / * Indicates that the status indicator (add the commands and inc) has been changed. * /    );      / * Here we go with C code on. * /    printf (" Result:% i \ n", bar);    return 0; } List the compiler with inline assembler support

Examples of compiler with inline assembler (alphabetically ):

  • cc65
  • Free Basic
  • GNU Compiler Collection
  • Microsoft Visual C
  • PowerBASIC
  • PureBasic
  • Turbo Pascal / Borland Delphi
413809
de