summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/basic-asm.html
blob: fcb05c6d95b7f017fb910dddfb97698f0ad803b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div class="subsection-level-extent" id="Basic-Asm"> <div class="nav-panel"> <p> Next: <a href="extended-asm" accesskey="n" rel="next">Extended Asm - Assembler Instructions with C Expression Operands</a>, Up: <a href="using-assembly-language-with-c" accesskey="u" rel="up">How to Use Inline Assembly Language in C Code</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div>  <h1 class="subsection" id="Basic-Asm-----Assembler-Instructions-Without-Operands"><span>6.47.1 Basic Asm — Assembler Instructions Without Operands<a class="copiable-link" href="#Basic-Asm-----Assembler-Instructions-Without-Operands"> ¶</a></span></h1>   <p>A basic <code class="code">asm</code> statement has the following syntax: </p> <div class="example"> <pre class="example-preformatted" data-language="cpp">asm <var class="var">asm-qualifiers</var> ( <var class="var">AssemblerInstructions</var> )</pre>
</div> <p>For the C language, the <code class="code">asm</code> keyword is a GNU extension. When writing C code that can be compiled with <samp class="option">-ansi</samp> and the <samp class="option">-std</samp> options that select C dialects without GNU extensions, use <code class="code">__asm__</code> instead of <code class="code">asm</code> (see <a class="pxref" href="alternate-keywords">Alternate Keywords</a>). For the C++ language, <code class="code">asm</code> is a standard keyword, but <code class="code">__asm__</code> can be used for code compiled with <samp class="option">-fno-asm</samp>. </p> <h1 class="subsubheading" id="Qualifiers-1"><span>Qualifiers<a class="copiable-link" href="#Qualifiers-1"> ¶</a></span></h1> <dl class="table"> <dt><code class="code">volatile</code></dt> <dd>
<p>The optional <code class="code">volatile</code> qualifier has no effect. All basic <code class="code">asm</code> blocks are implicitly volatile. </p> </dd> <dt><code class="code">inline</code></dt> <dd><p>If you use the <code class="code">inline</code> qualifier, then for inlining purposes the size of the <code class="code">asm</code> statement is taken as the smallest size possible (see <a class="pxref" href="size-of-an-asm">Size of an <code class="code">asm</code></a>). </p></dd> </dl> <h1 class="subsubheading" id="Parameters"><span>Parameters<a class="copiable-link" href="#Parameters"> ¶</a></span></h1> <dl class="table"> <dt><var class="var">AssemblerInstructions</var></dt> <dd>
<p>This is a literal string that specifies the assembler code. The string can contain any instructions recognized by the assembler, including directives. GCC does not parse the assembler instructions themselves and does not know what they mean or even whether they are valid assembler input. </p> <p>You may place multiple assembler instructions together in a single <code class="code">asm</code> string, separated by the characters normally used in assembly code for the system. A combination that works in most places is a newline to break the line, plus a tab character (written as ‘<samp class="samp">\n\t</samp>’). Some assemblers allow semicolons as a line separator. However, note that some assembler dialects use semicolons to start a comment. </p>
</dd> </dl> <h1 class="subsubheading" id="Remarks"><span>Remarks<a class="copiable-link" href="#Remarks"> ¶</a></span></h1> <p>Using extended <code class="code">asm</code> (see <a class="pxref" href="extended-asm">Extended Asm - Assembler Instructions with C Expression Operands</a>) typically produces smaller, safer, and more efficient code, and in most cases it is a better solution than basic <code class="code">asm</code>. However, there are two situations where only basic <code class="code">asm</code> can be used: </p> <ul class="itemize mark-bullet"> <li>Extended <code class="code">asm</code> statements have to be inside a C function, so to write inline assembly language at file scope (“top-level”), outside of C functions, you must use basic <code class="code">asm</code>. You can use this technique to emit assembler directives, define assembly language macros that can be invoked elsewhere in the file, or write entire functions in assembly language. Basic <code class="code">asm</code> statements outside of functions may not use any qualifiers. </li>
<li>Functions declared with the <code class="code">naked</code> attribute also require basic <code class="code">asm</code> (see <a class="pxref" href="function-attributes">Declaring Attributes of Functions</a>). </li>
</ul> <p>Safely accessing C data and calling functions from basic <code class="code">asm</code> is more complex than it may appear. To access C data, it is better to use extended <code class="code">asm</code>. </p> <p>Do not expect a sequence of <code class="code">asm</code> statements to remain perfectly consecutive after compilation. If certain instructions need to remain consecutive in the output, put them in a single multi-instruction <code class="code">asm</code> statement. Note that GCC’s optimizers can move <code class="code">asm</code> statements relative to other code, including across jumps. </p> <p><code class="code">asm</code> statements may not perform jumps into other <code class="code">asm</code> statements. GCC does not know about these jumps, and therefore cannot take account of them when deciding how to optimize. Jumps from <code class="code">asm</code> to C labels are only supported in extended <code class="code">asm</code>. </p> <p>Under certain circumstances, GCC may duplicate (or remove duplicates of) your assembly code when optimizing. This can lead to unexpected duplicate symbol errors during compilation if your assembly code defines symbols or labels. </p> <p><strong class="strong">Warning:</strong> The C standards do not specify semantics for <code class="code">asm</code>, making it a potential source of incompatibilities between compilers. These incompatibilities may not produce compiler warnings/errors. </p> <p>GCC does not parse basic <code class="code">asm</code>’s <var class="var">AssemblerInstructions</var>, which means there is no way to communicate to the compiler what is happening inside them. GCC has no visibility of symbols in the <code class="code">asm</code> and may discard them as unreferenced. It also does not know about side effects of the assembler code, such as modifications to memory or registers. Unlike some compilers, GCC assumes that no changes to general purpose registers occur. This assumption may change in a future release. </p> <p>To avoid complications from future changes to the semantics and the compatibility issues between compilers, consider replacing basic <code class="code">asm</code> with extended <code class="code">asm</code>. See <a class="uref" href="https://gcc.gnu.org/wiki/ConvertBasicAsmToExtended">How to convert from basic asm to extended asm</a> for information about how to perform this conversion. </p> <p>The compiler copies the assembler instructions in a basic <code class="code">asm</code> verbatim to the assembly language output file, without processing dialects or any of the ‘<samp class="samp">%</samp>’ operators that are available with extended <code class="code">asm</code>. This results in minor differences between basic <code class="code">asm</code> strings and extended <code class="code">asm</code> templates. For example, to refer to registers you might use ‘<samp class="samp">%eax</samp>’ in basic <code class="code">asm</code> and ‘<samp class="samp">%%eax</samp>’ in extended <code class="code">asm</code>. </p> <p>On targets such as x86 that support multiple assembler dialects, all basic <code class="code">asm</code> blocks use the assembler dialect specified by the <samp class="option">-masm</samp> command-line option (see <a class="pxref" href="x86-options">x86 Options</a>). Basic <code class="code">asm</code> provides no mechanism to provide different assembler strings for different dialects. </p> <p>For basic <code class="code">asm</code> with non-empty assembler string GCC assumes the assembler block does not change any general purpose registers, but it may read or write any globally accessible variable. </p> <p>Here is an example of basic <code class="code">asm</code> for i386: </p> <div class="example"> <pre class="example-preformatted" data-language="cpp">/* Note that this code will not compile with -masm=intel */
#define DebugBreak() asm("int $3")</pre>
</div> </div>  <div class="nav-panel"> <p> Next: <a href="extended-asm">Extended Asm - Assembler Instructions with C Expression Operands</a>, Up: <a href="using-assembly-language-with-c">How to Use Inline Assembly Language in C Code</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
  <p class="_attribution-p">
    &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
    <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Basic-Asm.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Basic-Asm.html</a>
  </p>
</div>