Changes

Jump to: navigation, search

Inline Assembly Language

1,250 bytes added, 16:19, 13 September 2022
Assembler Template
== Syntax ==
Inline assembler is included in a GCC C source file in one of these two forms:
asm(...);
=== Assembler Template ===
The assembler template is a piece of assembler code that is will be pre-processed to fill in register assignments. Registers may be referenced as %0, %1, %2 and so forth, indicating the registers mentioned in the output operands and input operands. For example, if there is one output operand and two input operands, you can refer to the register containing the output operand as <code>%0</code> and the input operands as <code>%1</code> and <code>%2</code>.
Because % is used as a prefix for register numbers(using some assembly language dialects), a double percent-sign must be used to represent a single percent sign in the code. For example, in x86_64 gas assembler, the rax register is written as <code>%rax</code> -- but in a template, it must be written as <code>%%rax</code>.
The template is written as one or more strings enclosed in quotes, with no separator other than whitespace between the strings. Individual statements in the asm code must be separated by semi-colons (;) or explicit newline characters (\n). The sequence \t can be used to indicate a tab character.
asm("mov %1,%0;inc %0");
__asm__ ("mov %1,%0\ninc %0");
__asm__ ("mov %1,%0\n" "inc %0");
__asm__ ("mov %1,%0\n\t"
=== Output and Input Operands ===
Output and input operands, if are optional -- some asm code may do a task without anyinput or output values. If the asm code has input or output values, these operands are specified as an optional name in square brackets, a quoted string containing a constraint, and a C expression in parenthesis.
Constraints are specified as a string of characters. Some commonly use constraints are:
* % - in addition to one of the symbols above, declares that this operand and the following operand are commutable (interchangeable) for optimization. Only one commutable pair may be specified.
The constraint is followed by the a C expression in parenthesis the provides to provide the value (input operand) or receives receive the value (output operand). This is usually a variable name for output operands, but may be a variable name, evaluated expression, or a constant for input operands.
Here are some (trivial) examples in x86_64 assembler:
:
);
 
===== Using a Specific Register Width on AArch64 =====
 
On AArch64, you may need to access a 32-bit version of a register. To do this with templated register names, you can add the 'w' modifier. For example, if %0 is x22 and you need to access w22, use %w0.
 
==== Constraining an Operand to a Specific Register ====
It is sometimes useful to constrain an operand to a particular register to avoid having to perform moves within the asm code (for example, if an operand will be used as the input to a function call or [[Syscalls|syscall]]).
===== Register Constraints using Explicit Register Variables =====
);
If memory is altered by the asm code, the string "memory" should be added to the clobber list. This will cause the compiler to mistrust values that were loaded from memory before the assembly language code was executed -- for example, if register r12 is loaded with an integer from memory before the assembly code is executed, it will be reloaded from memory afterward, because the value in memory may have been modified by the assembly code. If a memory clobber is not specified, the compiler will assume that values previously loaded from memory still match the values in memory. In most cases, the <code>volatile</code> keyword should also be adddedused along with the memory clobber flag. If the [[Register#Status_Register|condition code (status register or flag register)]] is altered, the string "cc" should be added to the clobber list.
== Resources ==

Navigation menu