Changes

Jump to: navigation, search

Inline Assembly Language

47 bytes added, 16:19, 13 September 2022
Assembler Template
=== 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");
==== 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 =====

Navigation menu