You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
10KB

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