Basic-Asm.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Basic Asm</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Basic Asm">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Basic Asm">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Using-Assembly-Language-with-C.html#Using-Assembly-Language-with-C" rel="up" title="Using Assembly Language with C">
  30. <link href="Extended-Asm.html#Extended-Asm" rel="next" title="Extended Asm">
  31. <link href="Using-Assembly-Language-with-C.html#Using-Assembly-Language-with-C" rel="prev" title="Using Assembly Language with C">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Basic-Asm"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Extended-Asm.html#Extended-Asm" accesskey="n" rel="next">Extended Asm</a>, Up: <a href="Using-Assembly-Language-with-C.html#Using-Assembly-Language-with-C" accesskey="u" rel="up">Using Assembly Language with C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Basic-Asm-_002d_002d_002d-Assembler-Instructions-Without-Operands"></a>
  69. <h4 class="subsection">6.45.1 Basic Asm &mdash; Assembler Instructions Without Operands</h4>
  70. <a name="index-basic-asm"></a>
  71. <a name="index-assembly-language-in-C_002c-basic"></a>
  72. <p>A basic <code>asm</code> statement has the following syntax:
  73. </p>
  74. <div class="example">
  75. <pre class="example">asm <var>asm-qualifiers</var> ( <var>AssemblerInstructions</var> )
  76. </pre></div>
  77. <p>The <code>asm</code> keyword is a GNU extension.
  78. When writing code that can be compiled with <samp>-ansi</samp> and the
  79. various <samp>-std</samp> options, use <code>__asm__</code> instead of
  80. <code>asm</code> (see <a href="Alternate-Keywords.html#Alternate-Keywords">Alternate Keywords</a>).
  81. </p>
  82. <a name="Qualifiers-1"></a>
  83. <h4 class="subsubheading">Qualifiers</h4>
  84. <dl compact="compact">
  85. <dt><code>volatile</code></dt>
  86. <dd><p>The optional <code>volatile</code> qualifier has no effect.
  87. All basic <code>asm</code> blocks are implicitly volatile.
  88. </p>
  89. </dd>
  90. <dt><code>inline</code></dt>
  91. <dd><p>If you use the <code>inline</code> qualifier, then for inlining purposes the size
  92. of the asm is taken as the smallest size possible (see <a href="Size-of-an-asm.html#Size-of-an-asm">Size of an asm</a>).
  93. </p></dd>
  94. </dl>
  95. <a name="Parameters"></a>
  96. <h4 class="subsubheading">Parameters</h4>
  97. <dl compact="compact">
  98. <dt><var>AssemblerInstructions</var></dt>
  99. <dd><p>This is a literal string that specifies the assembler code. The string can
  100. contain any instructions recognized by the assembler, including directives.
  101. GCC does not parse the assembler instructions themselves and
  102. does not know what they mean or even whether they are valid assembler input.
  103. </p>
  104. <p>You may place multiple assembler instructions together in a single <code>asm</code>
  105. string, separated by the characters normally used in assembly code for the
  106. system. A combination that works in most places is a newline to break the
  107. line, plus a tab character (written as &lsquo;<samp>\n\t</samp>&rsquo;).
  108. Some assemblers allow semicolons as a line separator. However,
  109. note that some assembler dialects use semicolons to start a comment.
  110. </p></dd>
  111. </dl>
  112. <a name="Remarks"></a>
  113. <h4 class="subsubheading">Remarks</h4>
  114. <p>Using extended <code>asm</code> (see <a href="Extended-Asm.html#Extended-Asm">Extended Asm</a>) typically produces
  115. smaller, safer, and more efficient code, and in most cases it is a
  116. better solution than basic <code>asm</code>. However, there are two
  117. situations where only basic <code>asm</code> can be used:
  118. </p>
  119. <ul>
  120. <li> Extended <code>asm</code> statements have to be inside a C
  121. function, so to write inline assembly language at file scope (&ldquo;top-level&rdquo;),
  122. outside of C functions, you must use basic <code>asm</code>.
  123. You can use this technique to emit assembler directives,
  124. define assembly language macros that can be invoked elsewhere in the file,
  125. or write entire functions in assembly language.
  126. </li><li> Functions declared
  127. with the <code>naked</code> attribute also require basic <code>asm</code>
  128. (see <a href="Function-Attributes.html#Function-Attributes">Function Attributes</a>).
  129. </li></ul>
  130. <p>Safely accessing C data and calling functions from basic <code>asm</code> is more
  131. complex than it may appear. To access C data, it is better to use extended
  132. <code>asm</code>.
  133. </p>
  134. <p>Do not expect a sequence of <code>asm</code> statements to remain perfectly
  135. consecutive after compilation. If certain instructions need to remain
  136. consecutive in the output, put them in a single multi-instruction <code>asm</code>
  137. statement. Note that GCC&rsquo;s optimizers can move <code>asm</code> statements
  138. relative to other code, including across jumps.
  139. </p>
  140. <p><code>asm</code> statements may not perform jumps into other <code>asm</code> statements.
  141. GCC does not know about these jumps, and therefore cannot take
  142. account of them when deciding how to optimize. Jumps from <code>asm</code> to C
  143. labels are only supported in extended <code>asm</code>.
  144. </p>
  145. <p>Under certain circumstances, GCC may duplicate (or remove duplicates of) your
  146. assembly code when optimizing. This can lead to unexpected duplicate
  147. symbol errors during compilation if your assembly code defines symbols or
  148. labels.
  149. </p>
  150. <p><strong>Warning:</strong> The C standards do not specify semantics for <code>asm</code>,
  151. making it a potential source of incompatibilities between compilers. These
  152. incompatibilities may not produce compiler warnings/errors.
  153. </p>
  154. <p>GCC does not parse basic <code>asm</code>&rsquo;s <var>AssemblerInstructions</var>, which
  155. means there is no way to communicate to the compiler what is happening
  156. inside them. GCC has no visibility of symbols in the <code>asm</code> and may
  157. discard them as unreferenced. It also does not know about side effects of
  158. the assembler code, such as modifications to memory or registers. Unlike
  159. some compilers, GCC assumes that no changes to general purpose registers
  160. occur. This assumption may change in a future release.
  161. </p>
  162. <p>To avoid complications from future changes to the semantics and the
  163. compatibility issues between compilers, consider replacing basic <code>asm</code>
  164. with extended <code>asm</code>. See
  165. <a href="https://gcc.gnu.org/wiki/ConvertBasicAsmToExtended">How to convert
  166. from basic asm to extended asm</a> for information about how to perform this
  167. conversion.
  168. </p>
  169. <p>The compiler copies the assembler instructions in a basic <code>asm</code>
  170. verbatim to the assembly language output file, without
  171. processing dialects or any of the &lsquo;<samp>%</samp>&rsquo; operators that are available with
  172. extended <code>asm</code>. This results in minor differences between basic
  173. <code>asm</code> strings and extended <code>asm</code> templates. For example, to refer to
  174. registers you might use &lsquo;<samp>%eax</samp>&rsquo; in basic <code>asm</code> and
  175. &lsquo;<samp>%%eax</samp>&rsquo; in extended <code>asm</code>.
  176. </p>
  177. <p>On targets such as x86 that support multiple assembler dialects,
  178. all basic <code>asm</code> blocks use the assembler dialect specified by the
  179. <samp>-masm</samp> command-line option (see <a href="x86-Options.html#x86-Options">x86 Options</a>).
  180. Basic <code>asm</code> provides no
  181. mechanism to provide different assembler strings for different dialects.
  182. </p>
  183. <p>For basic <code>asm</code> with non-empty assembler string GCC assumes
  184. the assembler block does not change any general purpose registers,
  185. but it may read or write any globally accessible variable.
  186. </p>
  187. <p>Here is an example of basic <code>asm</code> for i386:
  188. </p>
  189. <div class="example">
  190. <pre class="example">/* Note that this code will not compile with -masm=intel */
  191. #define DebugBreak() asm(&quot;int $3&quot;)
  192. </pre></div>
  193. <hr>
  194. <div class="header">
  195. <p>
  196. Next: <a href="Extended-Asm.html#Extended-Asm" accesskey="n" rel="next">Extended Asm</a>, Up: <a href="Using-Assembly-Language-with-C.html#Using-Assembly-Language-with-C" accesskey="u" rel="up">Using Assembly Language with C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  197. </div>
  198. </body>
  199. </html>