summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fasm.html
blob: d222cf593b9b44e17a2618de0b318f7613165781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    <h1 id="firstHeading" class="firstHeading">Inline assembly</h1>            <p>Inline assembly (typically introduced by the <code>asm</code> keyword) gives the ability to embed assembly language source code within a C program.</p>
<p>Unlike in C++, inline assembly is treated as an extension in C. It is conditionally supported and implementation defined, meaning that it may not be present and, even when provided by the implementation, it does not have a fixed meaning.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin">  <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <code>asm (</code> <span class="t-spar">string_literal</span> <code>)</code> <code>;</code> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
</tr> 
</table>  <h3 id="Explanation"> Explanation</h3> <p>This kind of inline assembly syntax is accepted by the C++ standard and called <i>asm-declaration</i> in C++. The <span class="t-spar">string_literal</span> is typically a short program written in assembly language, which is executed whenever this declaration is executed. Different C compilers have wildly varying rules for asm-declarations, and different conventions for the interaction with the surrounding C code.</p>
<p>asm-declaration can appear inside a block (a function body or another compound statement), and, as all other declarations, this declaration can also appear outside a block.</p>
<h3 id="Notes"> Notes</h3> <p>MSVC does not support inline assembly on the ARM and x64 processors, and only support the form introduced by <code>__asm</code> on x86 processors.</p>
<p>When compiling in ISO C mode by GCC or Clang (e.g. with option <code>-std=c11</code>), <code>__asm__</code> must be used instead of <code>asm</code>.</p>
<h3 id="Examples"> Examples</h3> <div class="t-example">
<p>Demonstrates two kinds of inline assembly syntax offered by the GCC compiler. This program will only work correctly on x86-64 platform under Linux. Note that the "standard inline assembly" is also treated as an extension in the C standard.</p>
<div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
 
extern int func(void);
// the definition of func is written in assembly language
__asm__(".globl func\n\t"
        ".type func, @function\n\t"
        "func:\n\t"
        ".cfi_startproc\n\t"
        "movl $7, %eax\n\t"
        "ret\n\t"
        ".cfi_endproc");
 
int main(void)
{
    int n = func();
    // gcc's extended inline assembly
    __asm__ ("leal (%0,%0,4),%0"
           : "=r" (n)
           : "0" (n));
    printf("7*5 = %d\n", n);
    fflush(stdout); // flush is intentional
 
    // standard inline assembly in C++
    __asm__ ("movq $60, %rax\n\t" // the exit syscall number on Linux
             "movq $2,  %rdi\n\t" // this program returns 2
             "syscall");
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">7*5 = 35</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> J.5.10 The asm keyword (p: TBD) </li></ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> J.5.10 The asm keyword (p: 422) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> J.5.10 The asm keyword (p: 580) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> J.5.10 The asm keyword (p: 512) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> G.5.10 The asm keyword </li></ul>
</ul>                   <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/asm" title="cpp/language/asm">C++ documentation</a></span> for <span class=""><span><code>asm</code> declaration</span></span> </td>
</tr> </table> <h3 id="External_links"> External links</h3> <table> <tr style="vertical-align:top;"> <td>1. </td> <td>
<a rel="nofollow" class="external text" href="https://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html">GCC Inline Assembly HOWTO</a> </td>
</tr> <tr style="vertical-align:top;"> <td>2. </td> <td>
<a rel="nofollow" class="external text" href="https://pic.dhe.ibm.com/infocenter/comphelp/v121v141/topic/com.ibm.xlcpp121.aix.doc/language_ref/asm.html">IBM XL C/C++ Inline Assembly</a> </td>
</tr> <tr style="vertical-align:top;"> <td>3. </td> <td>
<a rel="nofollow" class="external text" href="https://software.intel.com/en-us/cpp-compiler-developer-guide-and-reference-inline-assembly">Intel C++ Inline Assembly</a> </td>
</tr> <tr style="vertical-align:top;"> <td>4. </td> <td>
<a rel="nofollow" class="external text" href="https://learn.microsoft.com/en-us/cpp/assembler/inline/inline-assembler.aspx">Visual Studio Inline Assembler</a> </td>
</tr> <tr style="vertical-align:top;"> <td>5. </td> <td>
<a rel="nofollow" class="external text" href="https://blogs.oracle.com/x86be/entry/gcc_style_asm_inlining_support">Sun Studio 12 Asm Statements</a> </td>
</tr> <tr style="vertical-align:top;"> <td>6. </td> <td>
<a rel="nofollow" class="external text" href="https://h21007.www2.hp.com/portal/site/dspp/menuitem.863c3e4cbcdc3f3515b49c108973a801?ciid=4308e2f5bde02110e2f5bde02110275d6e10RCRD">Inline assembly for Itanium-based HP-UX</a> </td>
</tr>
</table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/language/asm" class="_attribution-link">https://en.cppreference.com/w/c/language/asm</a>
  </p>
</div>