Extended-Asm.html 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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): Extended Asm</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Extended Asm">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Extended 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="Constraints.html#Constraints" rel="next" title="Constraints">
  31. <link href="Basic-Asm.html#Basic-Asm" rel="prev" title="Basic Asm">
  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="Extended-Asm"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Constraints.html#Constraints" accesskey="n" rel="next">Constraints</a>, Previous: <a href="Basic-Asm.html#Basic-Asm" accesskey="p" rel="prev">Basic 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="Extended-Asm-_002d-Assembler-Instructions-with-C-Expression-Operands"></a>
  69. <h4 class="subsection">6.45.2 Extended Asm - Assembler Instructions with C Expression Operands</h4>
  70. <a name="index-extended-asm"></a>
  71. <a name="index-assembly-language-in-C_002c-extended"></a>
  72. <p>With extended <code>asm</code> you can read and write C variables from
  73. assembler and perform jumps from assembler code to C labels.
  74. Extended <code>asm</code> syntax uses colons (&lsquo;<samp>:</samp>&rsquo;) to delimit
  75. the operand parameters after the assembler template:
  76. </p>
  77. <div class="example">
  78. <pre class="example">asm <var>asm-qualifiers</var> ( <var>AssemblerTemplate</var>
  79. : <var>OutputOperands</var>
  80. <span class="roman">[</span> : <var>InputOperands</var>
  81. <span class="roman">[</span> : <var>Clobbers</var> <span class="roman">]</span> <span class="roman">]</span>)
  82. asm <var>asm-qualifiers</var> ( <var>AssemblerTemplate</var>
  83. :
  84. : <var>InputOperands</var>
  85. : <var>Clobbers</var>
  86. : <var>GotoLabels</var>)
  87. </pre></div>
  88. <p>where in the last form, <var>asm-qualifiers</var> contains <code>goto</code> (and in the
  89. first form, not).
  90. </p>
  91. <p>The <code>asm</code> keyword is a GNU extension.
  92. When writing code that can be compiled with <samp>-ansi</samp> and the
  93. various <samp>-std</samp> options, use <code>__asm__</code> instead of
  94. <code>asm</code> (see <a href="Alternate-Keywords.html#Alternate-Keywords">Alternate Keywords</a>).
  95. </p>
  96. <a name="Qualifiers-2"></a>
  97. <h4 class="subsubheading">Qualifiers</h4>
  98. <dl compact="compact">
  99. <dt><code>volatile</code></dt>
  100. <dd><p>The typical use of extended <code>asm</code> statements is to manipulate input
  101. values to produce output values. However, your <code>asm</code> statements may
  102. also produce side effects. If so, you may need to use the <code>volatile</code>
  103. qualifier to disable certain optimizations. See <a href="#Volatile">Volatile</a>.
  104. </p>
  105. </dd>
  106. <dt><code>inline</code></dt>
  107. <dd><p>If you use the <code>inline</code> qualifier, then for inlining purposes the size
  108. 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>).
  109. </p>
  110. </dd>
  111. <dt><code>goto</code></dt>
  112. <dd><p>This qualifier informs the compiler that the <code>asm</code> statement may
  113. perform a jump to one of the labels listed in the <var>GotoLabels</var>.
  114. See <a href="#GotoLabels">GotoLabels</a>.
  115. </p></dd>
  116. </dl>
  117. <a name="Parameters-1"></a>
  118. <h4 class="subsubheading">Parameters</h4>
  119. <dl compact="compact">
  120. <dt><var>AssemblerTemplate</var></dt>
  121. <dd><p>This is a literal string that is the template for the assembler code. It is a
  122. combination of fixed text and tokens that refer to the input, output,
  123. and goto parameters. See <a href="#AssemblerTemplate">AssemblerTemplate</a>.
  124. </p>
  125. </dd>
  126. <dt><var>OutputOperands</var></dt>
  127. <dd><p>A comma-separated list of the C variables modified by the instructions in the
  128. <var>AssemblerTemplate</var>. An empty list is permitted. See <a href="#OutputOperands">OutputOperands</a>.
  129. </p>
  130. </dd>
  131. <dt><var>InputOperands</var></dt>
  132. <dd><p>A comma-separated list of C expressions read by the instructions in the
  133. <var>AssemblerTemplate</var>. An empty list is permitted. See <a href="#InputOperands">InputOperands</a>.
  134. </p>
  135. </dd>
  136. <dt><var>Clobbers</var></dt>
  137. <dd><p>A comma-separated list of registers or other values changed by the
  138. <var>AssemblerTemplate</var>, beyond those listed as outputs.
  139. An empty list is permitted. See <a href="#Clobbers">Clobbers</a>.
  140. </p>
  141. </dd>
  142. <dt><var>GotoLabels</var></dt>
  143. <dd><p>When you are using the <code>goto</code> form of <code>asm</code>, this section contains
  144. the list of all C labels to which the code in the
  145. <var>AssemblerTemplate</var> may jump.
  146. See <a href="#GotoLabels">GotoLabels</a>.
  147. </p>
  148. <p><code>asm</code> statements may not perform jumps into other <code>asm</code> statements,
  149. only to the listed <var>GotoLabels</var>.
  150. GCC&rsquo;s optimizers do not know about other jumps; therefore they cannot take
  151. account of them when deciding how to optimize.
  152. </p></dd>
  153. </dl>
  154. <p>The total number of input + output + goto operands is limited to 30.
  155. </p>
  156. <a name="Remarks-1"></a>
  157. <h4 class="subsubheading">Remarks</h4>
  158. <p>The <code>asm</code> statement allows you to include assembly instructions directly
  159. within C code. This may help you to maximize performance in time-sensitive
  160. code or to access assembly instructions that are not readily available to C
  161. programs.
  162. </p>
  163. <p>Note that extended <code>asm</code> statements must be inside a function. Only
  164. basic <code>asm</code> may be outside functions (see <a href="Basic-Asm.html#Basic-Asm">Basic Asm</a>).
  165. Functions declared with the <code>naked</code> attribute also require basic
  166. <code>asm</code> (see <a href="Function-Attributes.html#Function-Attributes">Function Attributes</a>).
  167. </p>
  168. <p>While the uses of <code>asm</code> are many and varied, it may help to think of an
  169. <code>asm</code> statement as a series of low-level instructions that convert input
  170. parameters to output parameters. So a simple (if not particularly useful)
  171. example for i386 using <code>asm</code> might look like this:
  172. </p>
  173. <div class="example">
  174. <pre class="example">int src = 1;
  175. int dst;
  176. asm (&quot;mov %1, %0\n\t&quot;
  177. &quot;add $1, %0&quot;
  178. : &quot;=r&quot; (dst)
  179. : &quot;r&quot; (src));
  180. printf(&quot;%d\n&quot;, dst);
  181. </pre></div>
  182. <p>This code copies <code>src</code> to <code>dst</code> and add 1 to <code>dst</code>.
  183. </p>
  184. <a name="Volatile"></a><a name="Volatile-1"></a>
  185. <h4 class="subsubsection">6.45.2.1 Volatile</h4>
  186. <a name="index-volatile-asm"></a>
  187. <a name="index-asm-volatile"></a>
  188. <p>GCC&rsquo;s optimizers sometimes discard <code>asm</code> statements if they determine
  189. there is no need for the output variables. Also, the optimizers may move
  190. code out of loops if they believe that the code will always return the same
  191. result (i.e. none of its input values change between calls). Using the
  192. <code>volatile</code> qualifier disables these optimizations. <code>asm</code> statements
  193. that have no output operands, including <code>asm goto</code> statements,
  194. are implicitly volatile.
  195. </p>
  196. <p>This i386 code demonstrates a case that does not use (or require) the
  197. <code>volatile</code> qualifier. If it is performing assertion checking, this code
  198. uses <code>asm</code> to perform the validation. Otherwise, <code>dwRes</code> is
  199. unreferenced by any code. As a result, the optimizers can discard the
  200. <code>asm</code> statement, which in turn removes the need for the entire
  201. <code>DoCheck</code> routine. By omitting the <code>volatile</code> qualifier when it
  202. isn&rsquo;t needed you allow the optimizers to produce the most efficient code
  203. possible.
  204. </p>
  205. <div class="example">
  206. <pre class="example">void DoCheck(uint32_t dwSomeValue)
  207. {
  208. uint32_t dwRes;
  209. // Assumes dwSomeValue is not zero.
  210. asm (&quot;bsfl %1,%0&quot;
  211. : &quot;=r&quot; (dwRes)
  212. : &quot;r&quot; (dwSomeValue)
  213. : &quot;cc&quot;);
  214. assert(dwRes &gt; 3);
  215. }
  216. </pre></div>
  217. <p>The next example shows a case where the optimizers can recognize that the input
  218. (<code>dwSomeValue</code>) never changes during the execution of the function and can
  219. therefore move the <code>asm</code> outside the loop to produce more efficient code.
  220. Again, using <code>volatile</code> disables this type of optimization.
  221. </p>
  222. <div class="example">
  223. <pre class="example">void do_print(uint32_t dwSomeValue)
  224. {
  225. uint32_t dwRes;
  226. for (uint32_t x=0; x &lt; 5; x++)
  227. {
  228. // Assumes dwSomeValue is not zero.
  229. asm (&quot;bsfl %1,%0&quot;
  230. : &quot;=r&quot; (dwRes)
  231. : &quot;r&quot; (dwSomeValue)
  232. : &quot;cc&quot;);
  233. printf(&quot;%u: %u %u\n&quot;, x, dwSomeValue, dwRes);
  234. }
  235. }
  236. </pre></div>
  237. <p>The following example demonstrates a case where you need to use the
  238. <code>volatile</code> qualifier.
  239. It uses the x86 <code>rdtsc</code> instruction, which reads
  240. the computer&rsquo;s time-stamp counter. Without the <code>volatile</code> qualifier,
  241. the optimizers might assume that the <code>asm</code> block will always return the
  242. same value and therefore optimize away the second call.
  243. </p>
  244. <div class="example">
  245. <pre class="example">uint64_t msr;
  246. asm volatile ( &quot;rdtsc\n\t&quot; // Returns the time in EDX:EAX.
  247. &quot;shl $32, %%rdx\n\t&quot; // Shift the upper bits left.
  248. &quot;or %%rdx, %0&quot; // 'Or' in the lower bits.
  249. : &quot;=a&quot; (msr)
  250. :
  251. : &quot;rdx&quot;);
  252. printf(&quot;msr: %llx\n&quot;, msr);
  253. // Do other work...
  254. // Reprint the timestamp
  255. asm volatile ( &quot;rdtsc\n\t&quot; // Returns the time in EDX:EAX.
  256. &quot;shl $32, %%rdx\n\t&quot; // Shift the upper bits left.
  257. &quot;or %%rdx, %0&quot; // 'Or' in the lower bits.
  258. : &quot;=a&quot; (msr)
  259. :
  260. : &quot;rdx&quot;);
  261. printf(&quot;msr: %llx\n&quot;, msr);
  262. </pre></div>
  263. <p>GCC&rsquo;s optimizers do not treat this code like the non-volatile code in the
  264. earlier examples. They do not move it out of loops or omit it on the
  265. assumption that the result from a previous call is still valid.
  266. </p>
  267. <p>Note that the compiler can move even volatile <code>asm</code> instructions relative
  268. to other code, including across jump instructions. For example, on many
  269. targets there is a system register that controls the rounding mode of
  270. floating-point operations. Setting it with a volatile <code>asm</code>, as in the
  271. following PowerPC example, does not work reliably.
  272. </p>
  273. <div class="example">
  274. <pre class="example">asm volatile(&quot;mtfsf 255, %0&quot; : : &quot;f&quot; (fpenv));
  275. sum = x + y;
  276. </pre></div>
  277. <p>The compiler may move the addition back before the volatile <code>asm</code>. To
  278. make it work as expected, add an artificial dependency to the <code>asm</code> by
  279. referencing a variable in the subsequent code, for example:
  280. </p>
  281. <div class="example">
  282. <pre class="example">asm volatile (&quot;mtfsf 255,%1&quot; : &quot;=X&quot; (sum) : &quot;f&quot; (fpenv));
  283. sum = x + y;
  284. </pre></div>
  285. <p>Under certain circumstances, GCC may duplicate (or remove duplicates of) your
  286. assembly code when optimizing. This can lead to unexpected duplicate symbol
  287. errors during compilation if your asm code defines symbols or labels.
  288. Using &lsquo;<samp>%=</samp>&rsquo;
  289. (see <a href="#AssemblerTemplate">AssemblerTemplate</a>) may help resolve this problem.
  290. </p>
  291. <a name="AssemblerTemplate"></a><a name="Assembler-Template"></a>
  292. <h4 class="subsubsection">6.45.2.2 Assembler Template</h4>
  293. <a name="index-asm-assembler-template"></a>
  294. <p>An assembler template is a literal string containing assembler instructions.
  295. The compiler replaces tokens in the template that refer
  296. to inputs, outputs, and goto labels,
  297. and then outputs the resulting string to the assembler. The
  298. string can contain any instructions recognized by the assembler, including
  299. directives. GCC does not parse the assembler instructions
  300. themselves and does not know what they mean or even whether they are valid
  301. assembler input. However, it does count the statements
  302. (see <a href="Size-of-an-asm.html#Size-of-an-asm">Size of an asm</a>).
  303. </p>
  304. <p>You may place multiple assembler instructions together in a single <code>asm</code>
  305. string, separated by the characters normally used in assembly code for the
  306. system. A combination that works in most places is a newline to break the
  307. line, plus a tab character to move to the instruction field (written as
  308. &lsquo;<samp>\n\t</samp>&rsquo;).
  309. Some assemblers allow semicolons as a line separator. However, note
  310. that some assembler dialects use semicolons to start a comment.
  311. </p>
  312. <p>Do not expect a sequence of <code>asm</code> statements to remain perfectly
  313. consecutive after compilation, even when you are using the <code>volatile</code>
  314. qualifier. If certain instructions need to remain consecutive in the output,
  315. put them in a single multi-instruction asm statement.
  316. </p>
  317. <p>Accessing data from C programs without using input/output operands (such as
  318. by using global symbols directly from the assembler template) may not work as
  319. expected. Similarly, calling functions directly from an assembler template
  320. requires a detailed understanding of the target assembler and ABI.
  321. </p>
  322. <p>Since GCC does not parse the assembler template,
  323. it has no visibility of any
  324. symbols it references. This may result in GCC discarding those symbols as
  325. unreferenced unless they are also listed as input, output, or goto operands.
  326. </p>
  327. <a name="Special-format-strings"></a>
  328. <h4 class="subsubheading">Special format strings</h4>
  329. <p>In addition to the tokens described by the input, output, and goto operands,
  330. these tokens have special meanings in the assembler template:
  331. </p>
  332. <dl compact="compact">
  333. <dt>&lsquo;<samp>%%</samp>&rsquo;</dt>
  334. <dd><p>Outputs a single &lsquo;<samp>%</samp>&rsquo; into the assembler code.
  335. </p>
  336. </dd>
  337. <dt>&lsquo;<samp>%=</samp>&rsquo;</dt>
  338. <dd><p>Outputs a number that is unique to each instance of the <code>asm</code>
  339. statement in the entire compilation. This option is useful when creating local
  340. labels and referring to them multiple times in a single template that
  341. generates multiple assembler instructions.
  342. </p>
  343. </dd>
  344. <dt>&lsquo;<samp>%{</samp>&rsquo;</dt>
  345. <dt>&lsquo;<samp>%|</samp>&rsquo;</dt>
  346. <dt>&lsquo;<samp>%}</samp>&rsquo;</dt>
  347. <dd><p>Outputs &lsquo;<samp>{</samp>&rsquo;, &lsquo;<samp>|</samp>&rsquo;, and &lsquo;<samp>}</samp>&rsquo; characters (respectively)
  348. into the assembler code. When unescaped, these characters have special
  349. meaning to indicate multiple assembler dialects, as described below.
  350. </p></dd>
  351. </dl>
  352. <a name="Multiple-assembler-dialects-in-asm-templates"></a>
  353. <h4 class="subsubheading">Multiple assembler dialects in <code>asm</code> templates</h4>
  354. <p>On targets such as x86, GCC supports multiple assembler dialects.
  355. The <samp>-masm</samp> option controls which dialect GCC uses as its
  356. default for inline assembler. The target-specific documentation for the
  357. <samp>-masm</samp> option contains the list of supported dialects, as well as the
  358. default dialect if the option is not specified. This information may be
  359. important to understand, since assembler code that works correctly when
  360. compiled using one dialect will likely fail if compiled using another.
  361. See <a href="x86-Options.html#x86-Options">x86 Options</a>.
  362. </p>
  363. <p>If your code needs to support multiple assembler dialects (for example, if
  364. you are writing public headers that need to support a variety of compilation
  365. options), use constructs of this form:
  366. </p>
  367. <div class="example">
  368. <pre class="example">{ dialect0 | dialect1 | dialect2... }
  369. </pre></div>
  370. <p>This construct outputs <code>dialect0</code>
  371. when using dialect #0 to compile the code,
  372. <code>dialect1</code> for dialect #1, etc. If there are fewer alternatives within the
  373. braces than the number of dialects the compiler supports, the construct
  374. outputs nothing.
  375. </p>
  376. <p>For example, if an x86 compiler supports two dialects
  377. (&lsquo;<samp>att</samp>&rsquo;, &lsquo;<samp>intel</samp>&rsquo;), an
  378. assembler template such as this:
  379. </p>
  380. <div class="example">
  381. <pre class="example">&quot;bt{l %[Offset],%[Base] | %[Base],%[Offset]}; jc %l2&quot;
  382. </pre></div>
  383. <p>is equivalent to one of
  384. </p>
  385. <div class="example">
  386. <pre class="example">&quot;btl %[Offset],%[Base] ; jc %l2&quot; <span class="roman">/* att dialect */</span>
  387. &quot;bt %[Base],%[Offset]; jc %l2&quot; <span class="roman">/* intel dialect */</span>
  388. </pre></div>
  389. <p>Using that same compiler, this code:
  390. </p>
  391. <div class="example">
  392. <pre class="example">&quot;xchg{l}\t{%%}ebx, %1&quot;
  393. </pre></div>
  394. <p>corresponds to either
  395. </p>
  396. <div class="example">
  397. <pre class="example">&quot;xchgl\t%%ebx, %1&quot; <span class="roman">/* att dialect */</span>
  398. &quot;xchg\tebx, %1&quot; <span class="roman">/* intel dialect */</span>
  399. </pre></div>
  400. <p>There is no support for nesting dialect alternatives.
  401. </p>
  402. <a name="OutputOperands"></a><a name="Output-Operands"></a>
  403. <h4 class="subsubsection">6.45.2.3 Output Operands</h4>
  404. <a name="index-asm-output-operands"></a>
  405. <p>An <code>asm</code> statement has zero or more output operands indicating the names
  406. of C variables modified by the assembler code.
  407. </p>
  408. <p>In this i386 example, <code>old</code> (referred to in the template string as
  409. <code>%0</code>) and <code>*Base</code> (as <code>%1</code>) are outputs and <code>Offset</code>
  410. (<code>%2</code>) is an input:
  411. </p>
  412. <div class="example">
  413. <pre class="example">bool old;
  414. __asm__ (&quot;btsl %2,%1\n\t&quot; // Turn on zero-based bit #Offset in Base.
  415. &quot;sbb %0,%0&quot; // Use the CF to calculate old.
  416. : &quot;=r&quot; (old), &quot;+rm&quot; (*Base)
  417. : &quot;Ir&quot; (Offset)
  418. : &quot;cc&quot;);
  419. return old;
  420. </pre></div>
  421. <p>Operands are separated by commas. Each operand has this format:
  422. </p>
  423. <div class="example">
  424. <pre class="example"><span class="roman">[</span> [<var>asmSymbolicName</var>] <span class="roman">]</span> <var>constraint</var> (<var>cvariablename</var>)
  425. </pre></div>
  426. <dl compact="compact">
  427. <dt><var>asmSymbolicName</var></dt>
  428. <dd><p>Specifies a symbolic name for the operand.
  429. Reference the name in the assembler template
  430. by enclosing it in square brackets
  431. (i.e. &lsquo;<samp>%[Value]</samp>&rsquo;). The scope of the name is the <code>asm</code> statement
  432. that contains the definition. Any valid C variable name is acceptable,
  433. including names already defined in the surrounding code. No two operands
  434. within the same <code>asm</code> statement can use the same symbolic name.
  435. </p>
  436. <p>When not using an <var>asmSymbolicName</var>, use the (zero-based) position
  437. of the operand
  438. in the list of operands in the assembler template. For example if there are
  439. three output operands, use &lsquo;<samp>%0</samp>&rsquo; in the template to refer to the first,
  440. &lsquo;<samp>%1</samp>&rsquo; for the second, and &lsquo;<samp>%2</samp>&rsquo; for the third.
  441. </p>
  442. </dd>
  443. <dt><var>constraint</var></dt>
  444. <dd><p>A string constant specifying constraints on the placement of the operand;
  445. See <a href="Constraints.html#Constraints">Constraints</a>, for details.
  446. </p>
  447. <p>Output constraints must begin with either &lsquo;<samp>=</samp>&rsquo; (a variable overwriting an
  448. existing value) or &lsquo;<samp>+</samp>&rsquo; (when reading and writing). When using
  449. &lsquo;<samp>=</samp>&rsquo;, do not assume the location contains the existing value
  450. on entry to the <code>asm</code>, except
  451. when the operand is tied to an input; see <a href="#InputOperands">Input Operands</a>.
  452. </p>
  453. <p>After the prefix, there must be one or more additional constraints
  454. (see <a href="Constraints.html#Constraints">Constraints</a>) that describe where the value resides. Common
  455. constraints include &lsquo;<samp>r</samp>&rsquo; for register and &lsquo;<samp>m</samp>&rsquo; for memory.
  456. When you list more than one possible location (for example, <code>&quot;=rm&quot;</code>),
  457. the compiler chooses the most efficient one based on the current context.
  458. If you list as many alternates as the <code>asm</code> statement allows, you permit
  459. the optimizers to produce the best possible code.
  460. If you must use a specific register, but your Machine Constraints do not
  461. provide sufficient control to select the specific register you want,
  462. local register variables may provide a solution (see <a href="Local-Register-Variables.html#Local-Register-Variables">Local Register Variables</a>).
  463. </p>
  464. </dd>
  465. <dt><var>cvariablename</var></dt>
  466. <dd><p>Specifies a C lvalue expression to hold the output, typically a variable name.
  467. The enclosing parentheses are a required part of the syntax.
  468. </p>
  469. </dd>
  470. </dl>
  471. <p>When the compiler selects the registers to use to
  472. represent the output operands, it does not use any of the clobbered registers
  473. (see <a href="#Clobbers">Clobbers</a>).
  474. </p>
  475. <p>Output operand expressions must be lvalues. The compiler cannot check whether
  476. the operands have data types that are reasonable for the instruction being
  477. executed. For output expressions that are not directly addressable (for
  478. example a bit-field), the constraint must allow a register. In that case, GCC
  479. uses the register as the output of the <code>asm</code>, and then stores that
  480. register into the output.
  481. </p>
  482. <p>Operands using the &lsquo;<samp>+</samp>&rsquo; constraint modifier count as two operands
  483. (that is, both as input and output) towards the total maximum of 30 operands
  484. per <code>asm</code> statement.
  485. </p>
  486. <p>Use the &lsquo;<samp>&amp;</samp>&rsquo; constraint modifier (see <a href="Modifiers.html#Modifiers">Modifiers</a>) on all output
  487. operands that must not overlap an input. Otherwise,
  488. GCC may allocate the output operand in the same register as an unrelated
  489. input operand, on the assumption that the assembler code consumes its
  490. inputs before producing outputs. This assumption may be false if the assembler
  491. code actually consists of more than one instruction.
  492. </p>
  493. <p>The same problem can occur if one output parameter (<var>a</var>) allows a register
  494. constraint and another output parameter (<var>b</var>) allows a memory constraint.
  495. The code generated by GCC to access the memory address in <var>b</var> can contain
  496. registers which <em>might</em> be shared by <var>a</var>, and GCC considers those
  497. registers to be inputs to the asm. As above, GCC assumes that such input
  498. registers are consumed before any outputs are written. This assumption may
  499. result in incorrect behavior if the asm writes to <var>a</var> before using
  500. <var>b</var>. Combining the &lsquo;<samp>&amp;</samp>&rsquo; modifier with the register constraint on <var>a</var>
  501. ensures that modifying <var>a</var> does not affect the address referenced by
  502. <var>b</var>. Otherwise, the location of <var>b</var>
  503. is undefined if <var>a</var> is modified before using <var>b</var>.
  504. </p>
  505. <p><code>asm</code> supports operand modifiers on operands (for example &lsquo;<samp>%k2</samp>&rsquo;
  506. instead of simply &lsquo;<samp>%2</samp>&rsquo;). Typically these qualifiers are hardware
  507. dependent. The list of supported modifiers for x86 is found at
  508. <a href="#x86Operandmodifiers">x86 Operand modifiers</a>.
  509. </p>
  510. <p>If the C code that follows the <code>asm</code> makes no use of any of the output
  511. operands, use <code>volatile</code> for the <code>asm</code> statement to prevent the
  512. optimizers from discarding the <code>asm</code> statement as unneeded
  513. (see <a href="#Volatile">Volatile</a>).
  514. </p>
  515. <p>This code makes no use of the optional <var>asmSymbolicName</var>. Therefore it
  516. references the first output operand as <code>%0</code> (were there a second, it
  517. would be <code>%1</code>, etc). The number of the first input operand is one greater
  518. than that of the last output operand. In this i386 example, that makes
  519. <code>Mask</code> referenced as <code>%1</code>:
  520. </p>
  521. <div class="example">
  522. <pre class="example">uint32_t Mask = 1234;
  523. uint32_t Index;
  524. asm (&quot;bsfl %1, %0&quot;
  525. : &quot;=r&quot; (Index)
  526. : &quot;r&quot; (Mask)
  527. : &quot;cc&quot;);
  528. </pre></div>
  529. <p>That code overwrites the variable <code>Index</code> (&lsquo;<samp>=</samp>&rsquo;),
  530. placing the value in a register (&lsquo;<samp>r</samp>&rsquo;).
  531. Using the generic &lsquo;<samp>r</samp>&rsquo; constraint instead of a constraint for a specific
  532. register allows the compiler to pick the register to use, which can result
  533. in more efficient code. This may not be possible if an assembler instruction
  534. requires a specific register.
  535. </p>
  536. <p>The following i386 example uses the <var>asmSymbolicName</var> syntax.
  537. It produces the
  538. same result as the code above, but some may consider it more readable or more
  539. maintainable since reordering index numbers is not necessary when adding or
  540. removing operands. The names <code>aIndex</code> and <code>aMask</code>
  541. are only used in this example to emphasize which
  542. names get used where.
  543. It is acceptable to reuse the names <code>Index</code> and <code>Mask</code>.
  544. </p>
  545. <div class="example">
  546. <pre class="example">uint32_t Mask = 1234;
  547. uint32_t Index;
  548. asm (&quot;bsfl %[aMask], %[aIndex]&quot;
  549. : [aIndex] &quot;=r&quot; (Index)
  550. : [aMask] &quot;r&quot; (Mask)
  551. : &quot;cc&quot;);
  552. </pre></div>
  553. <p>Here are some more examples of output operands.
  554. </p>
  555. <div class="example">
  556. <pre class="example">uint32_t c = 1;
  557. uint32_t d;
  558. uint32_t *e = &amp;c;
  559. asm (&quot;mov %[e], %[d]&quot;
  560. : [d] &quot;=rm&quot; (d)
  561. : [e] &quot;rm&quot; (*e));
  562. </pre></div>
  563. <p>Here, <code>d</code> may either be in a register or in memory. Since the compiler
  564. might already have the current value of the <code>uint32_t</code> location
  565. pointed to by <code>e</code>
  566. in a register, you can enable it to choose the best location
  567. for <code>d</code> by specifying both constraints.
  568. </p>
  569. <a name="FlagOutputOperands"></a><a name="Flag-Output-Operands"></a>
  570. <h4 class="subsubsection">6.45.2.4 Flag Output Operands</h4>
  571. <a name="index-asm-flag-output-operands"></a>
  572. <p>Some targets have a special register that holds the &ldquo;flags&rdquo; for the
  573. result of an operation or comparison. Normally, the contents of that
  574. register are either unmodifed by the asm, or the asm is considered to
  575. clobber the contents.
  576. </p>
  577. <p>On some targets, a special form of output operand exists by which
  578. conditions in the flags register may be outputs of the asm. The set of
  579. conditions supported are target specific, but the general rule is that
  580. the output variable must be a scalar integer, and the value is boolean.
  581. When supported, the target defines the preprocessor symbol
  582. <code>__GCC_ASM_FLAG_OUTPUTS__</code>.
  583. </p>
  584. <p>Because of the special nature of the flag output operands, the constraint
  585. may not include alternatives.
  586. </p>
  587. <p>Most often, the target has only one flags register, and thus is an implied
  588. operand of many instructions. In this case, the operand should not be
  589. referenced within the assembler template via <code>%0</code> etc, as there&rsquo;s
  590. no corresponding text in the assembly language.
  591. </p>
  592. <dl compact="compact">
  593. <dt>x86 family</dt>
  594. <dd><p>The flag output constraints for the x86 family are of the form
  595. &lsquo;<samp>=@cc<var>cond</var></samp>&rsquo; where <var>cond</var> is one of the standard
  596. conditions defined in the ISA manual for <code>j<var>cc</var></code> or
  597. <code>set<var>cc</var></code>.
  598. </p>
  599. <dl compact="compact">
  600. <dt><code>a</code></dt>
  601. <dd><p>&ldquo;above&rdquo; or unsigned greater than
  602. </p></dd>
  603. <dt><code>ae</code></dt>
  604. <dd><p>&ldquo;above or equal&rdquo; or unsigned greater than or equal
  605. </p></dd>
  606. <dt><code>b</code></dt>
  607. <dd><p>&ldquo;below&rdquo; or unsigned less than
  608. </p></dd>
  609. <dt><code>be</code></dt>
  610. <dd><p>&ldquo;below or equal&rdquo; or unsigned less than or equal
  611. </p></dd>
  612. <dt><code>c</code></dt>
  613. <dd><p>carry flag set
  614. </p></dd>
  615. <dt><code>e</code></dt>
  616. <dt><code>z</code></dt>
  617. <dd><p>&ldquo;equal&rdquo; or zero flag set
  618. </p></dd>
  619. <dt><code>g</code></dt>
  620. <dd><p>signed greater than
  621. </p></dd>
  622. <dt><code>ge</code></dt>
  623. <dd><p>signed greater than or equal
  624. </p></dd>
  625. <dt><code>l</code></dt>
  626. <dd><p>signed less than
  627. </p></dd>
  628. <dt><code>le</code></dt>
  629. <dd><p>signed less than or equal
  630. </p></dd>
  631. <dt><code>o</code></dt>
  632. <dd><p>overflow flag set
  633. </p></dd>
  634. <dt><code>p</code></dt>
  635. <dd><p>parity flag set
  636. </p></dd>
  637. <dt><code>s</code></dt>
  638. <dd><p>sign flag set
  639. </p></dd>
  640. <dt><code>na</code></dt>
  641. <dt><code>nae</code></dt>
  642. <dt><code>nb</code></dt>
  643. <dt><code>nbe</code></dt>
  644. <dt><code>nc</code></dt>
  645. <dt><code>ne</code></dt>
  646. <dt><code>ng</code></dt>
  647. <dt><code>nge</code></dt>
  648. <dt><code>nl</code></dt>
  649. <dt><code>nle</code></dt>
  650. <dt><code>no</code></dt>
  651. <dt><code>np</code></dt>
  652. <dt><code>ns</code></dt>
  653. <dt><code>nz</code></dt>
  654. <dd><p>&ldquo;not&rdquo; <var>flag</var>, or inverted versions of those above
  655. </p></dd>
  656. </dl>
  657. </dd>
  658. </dl>
  659. <a name="InputOperands"></a><a name="Input-Operands"></a>
  660. <h4 class="subsubsection">6.45.2.5 Input Operands</h4>
  661. <a name="index-asm-input-operands"></a>
  662. <a name="index-asm-expressions"></a>
  663. <p>Input operands make values from C variables and expressions available to the
  664. assembly code.
  665. </p>
  666. <p>Operands are separated by commas. Each operand has this format:
  667. </p>
  668. <div class="example">
  669. <pre class="example"><span class="roman">[</span> [<var>asmSymbolicName</var>] <span class="roman">]</span> <var>constraint</var> (<var>cexpression</var>)
  670. </pre></div>
  671. <dl compact="compact">
  672. <dt><var>asmSymbolicName</var></dt>
  673. <dd><p>Specifies a symbolic name for the operand.
  674. Reference the name in the assembler template
  675. by enclosing it in square brackets
  676. (i.e. &lsquo;<samp>%[Value]</samp>&rsquo;). The scope of the name is the <code>asm</code> statement
  677. that contains the definition. Any valid C variable name is acceptable,
  678. including names already defined in the surrounding code. No two operands
  679. within the same <code>asm</code> statement can use the same symbolic name.
  680. </p>
  681. <p>When not using an <var>asmSymbolicName</var>, use the (zero-based) position
  682. of the operand
  683. in the list of operands in the assembler template. For example if there are
  684. two output operands and three inputs,
  685. use &lsquo;<samp>%2</samp>&rsquo; in the template to refer to the first input operand,
  686. &lsquo;<samp>%3</samp>&rsquo; for the second, and &lsquo;<samp>%4</samp>&rsquo; for the third.
  687. </p>
  688. </dd>
  689. <dt><var>constraint</var></dt>
  690. <dd><p>A string constant specifying constraints on the placement of the operand;
  691. See <a href="Constraints.html#Constraints">Constraints</a>, for details.
  692. </p>
  693. <p>Input constraint strings may not begin with either &lsquo;<samp>=</samp>&rsquo; or &lsquo;<samp>+</samp>&rsquo;.
  694. When you list more than one possible location (for example, &lsquo;<samp>&quot;irm&quot;</samp>&rsquo;),
  695. the compiler chooses the most efficient one based on the current context.
  696. If you must use a specific register, but your Machine Constraints do not
  697. provide sufficient control to select the specific register you want,
  698. local register variables may provide a solution (see <a href="Local-Register-Variables.html#Local-Register-Variables">Local Register Variables</a>).
  699. </p>
  700. <p>Input constraints can also be digits (for example, <code>&quot;0&quot;</code>). This indicates
  701. that the specified input must be in the same place as the output constraint
  702. at the (zero-based) index in the output constraint list.
  703. When using <var>asmSymbolicName</var> syntax for the output operands,
  704. you may use these names (enclosed in brackets &lsquo;<samp>[]</samp>&rsquo;) instead of digits.
  705. </p>
  706. </dd>
  707. <dt><var>cexpression</var></dt>
  708. <dd><p>This is the C variable or expression being passed to the <code>asm</code> statement
  709. as input. The enclosing parentheses are a required part of the syntax.
  710. </p>
  711. </dd>
  712. </dl>
  713. <p>When the compiler selects the registers to use to represent the input
  714. operands, it does not use any of the clobbered registers (see <a href="#Clobbers">Clobbers</a>).
  715. </p>
  716. <p>If there are no output operands but there are input operands, place two
  717. consecutive colons where the output operands would go:
  718. </p>
  719. <div class="example">
  720. <pre class="example">__asm__ (&quot;some instructions&quot;
  721. : /* No outputs. */
  722. : &quot;r&quot; (Offset / 8));
  723. </pre></div>
  724. <p><strong>Warning:</strong> Do <em>not</em> modify the contents of input-only operands
  725. (except for inputs tied to outputs). The compiler assumes that on exit from
  726. the <code>asm</code> statement these operands contain the same values as they
  727. had before executing the statement.
  728. It is <em>not</em> possible to use clobbers
  729. to inform the compiler that the values in these inputs are changing. One
  730. common work-around is to tie the changing input variable to an output variable
  731. that never gets used. Note, however, that if the code that follows the
  732. <code>asm</code> statement makes no use of any of the output operands, the GCC
  733. optimizers may discard the <code>asm</code> statement as unneeded
  734. (see <a href="#Volatile">Volatile</a>).
  735. </p>
  736. <p><code>asm</code> supports operand modifiers on operands (for example &lsquo;<samp>%k2</samp>&rsquo;
  737. instead of simply &lsquo;<samp>%2</samp>&rsquo;). Typically these qualifiers are hardware
  738. dependent. The list of supported modifiers for x86 is found at
  739. <a href="#x86Operandmodifiers">x86 Operand modifiers</a>.
  740. </p>
  741. <p>In this example using the fictitious <code>combine</code> instruction, the
  742. constraint <code>&quot;0&quot;</code> for input operand 1 says that it must occupy the same
  743. location as output operand 0. Only input operands may use numbers in
  744. constraints, and they must each refer to an output operand. Only a number (or
  745. the symbolic assembler name) in the constraint can guarantee that one operand
  746. is in the same place as another. The mere fact that <code>foo</code> is the value of
  747. both operands is not enough to guarantee that they are in the same place in
  748. the generated assembler code.
  749. </p>
  750. <div class="example">
  751. <pre class="example">asm (&quot;combine %2, %0&quot;
  752. : &quot;=r&quot; (foo)
  753. : &quot;0&quot; (foo), &quot;g&quot; (bar));
  754. </pre></div>
  755. <p>Here is an example using symbolic names.
  756. </p>
  757. <div class="example">
  758. <pre class="example">asm (&quot;cmoveq %1, %2, %[result]&quot;
  759. : [result] &quot;=r&quot;(result)
  760. : &quot;r&quot; (test), &quot;r&quot; (new), &quot;[result]&quot; (old));
  761. </pre></div>
  762. <a name="Clobbers"></a><a name="Clobbers-1"></a>
  763. <h4 class="subsubsection">6.45.2.6 Clobbers</h4>
  764. <a name="index-asm-clobbers"></a>
  765. <p>While the compiler is aware of changes to entries listed in the output
  766. operands, the inline <code>asm</code> code may modify more than just the outputs. For
  767. example, calculations may require additional registers, or the processor may
  768. overwrite a register as a side effect of a particular assembler instruction.
  769. In order to inform the compiler of these changes, list them in the clobber
  770. list. Clobber list items are either register names or the special clobbers
  771. (listed below). Each clobber list item is a string constant
  772. enclosed in double quotes and separated by commas.
  773. </p>
  774. <p>Clobber descriptions may not in any way overlap with an input or output
  775. operand. For example, you may not have an operand describing a register class
  776. with one member when listing that register in the clobber list. Variables
  777. declared to live in specific registers (see <a href="Explicit-Register-Variables.html#Explicit-Register-Variables">Explicit Register Variables</a>) and used
  778. as <code>asm</code> input or output operands must have no part mentioned in the
  779. clobber description. In particular, there is no way to specify that input
  780. operands get modified without also specifying them as output operands.
  781. </p>
  782. <p>When the compiler selects which registers to use to represent input and output
  783. operands, it does not use any of the clobbered registers. As a result,
  784. clobbered registers are available for any use in the assembler code.
  785. </p>
  786. <p>Here is a realistic example for the VAX showing the use of clobbered
  787. registers:
  788. </p>
  789. <div class="example">
  790. <pre class="example">asm volatile (&quot;movc3 %0, %1, %2&quot;
  791. : /* No outputs. */
  792. : &quot;g&quot; (from), &quot;g&quot; (to), &quot;g&quot; (count)
  793. : &quot;r0&quot;, &quot;r1&quot;, &quot;r2&quot;, &quot;r3&quot;, &quot;r4&quot;, &quot;r5&quot;);
  794. </pre></div>
  795. <p>Also, there are two special clobber arguments:
  796. </p>
  797. <dl compact="compact">
  798. <dt><code>&quot;cc&quot;</code></dt>
  799. <dd><p>The <code>&quot;cc&quot;</code> clobber indicates that the assembler code modifies the flags
  800. register. On some machines, GCC represents the condition codes as a specific
  801. hardware register; <code>&quot;cc&quot;</code> serves to name this register.
  802. On other machines, condition code handling is different,
  803. and specifying <code>&quot;cc&quot;</code> has no effect. But
  804. it is valid no matter what the target.
  805. </p>
  806. </dd>
  807. <dt><code>&quot;memory&quot;</code></dt>
  808. <dd><p>The <code>&quot;memory&quot;</code> clobber tells the compiler that the assembly code
  809. performs memory
  810. reads or writes to items other than those listed in the input and output
  811. operands (for example, accessing the memory pointed to by one of the input
  812. parameters). To ensure memory contains correct values, GCC may need to flush
  813. specific register values to memory before executing the <code>asm</code>. Further,
  814. the compiler does not assume that any values read from memory before an
  815. <code>asm</code> remain unchanged after that <code>asm</code>; it reloads them as
  816. needed.
  817. Using the <code>&quot;memory&quot;</code> clobber effectively forms a read/write
  818. memory barrier for the compiler.
  819. </p>
  820. <p>Note that this clobber does not prevent the <em>processor</em> from doing
  821. speculative reads past the <code>asm</code> statement. To prevent that, you need
  822. processor-specific fence instructions.
  823. </p>
  824. <p>Flushing registers to memory has performance implications and may be an issue
  825. for time-sensitive code. You can use a trick to avoid this if the size of
  826. the memory being accessed is known at compile time. For example, if accessing
  827. ten bytes of a string, use a memory input like:
  828. </p>
  829. <p><code>{&quot;m&quot;( ({ struct { char x[10]; } *p = (void *)ptr ; *p; }) )}</code>.
  830. </p>
  831. </dd>
  832. </dl>
  833. <a name="GotoLabels"></a><a name="Goto-Labels"></a>
  834. <h4 class="subsubsection">6.45.2.7 Goto Labels</h4>
  835. <a name="index-asm-goto-labels"></a>
  836. <p><code>asm goto</code> allows assembly code to jump to one or more C labels. The
  837. <var>GotoLabels</var> section in an <code>asm goto</code> statement contains
  838. a comma-separated
  839. list of all C labels to which the assembler code may jump. GCC assumes that
  840. <code>asm</code> execution falls through to the next statement (if this is not the
  841. case, consider using the <code>__builtin_unreachable</code> intrinsic after the
  842. <code>asm</code> statement). Optimization of <code>asm goto</code> may be improved by
  843. using the <code>hot</code> and <code>cold</code> label attributes (see <a href="Label-Attributes.html#Label-Attributes">Label Attributes</a>).
  844. </p>
  845. <p>An <code>asm goto</code> statement cannot have outputs.
  846. This is due to an internal restriction of
  847. the compiler: control transfer instructions cannot have outputs.
  848. If the assembler code does modify anything, use the <code>&quot;memory&quot;</code> clobber
  849. to force the
  850. optimizers to flush all register values to memory and reload them if
  851. necessary after the <code>asm</code> statement.
  852. </p>
  853. <p>Also note that an <code>asm goto</code> statement is always implicitly
  854. considered volatile.
  855. </p>
  856. <p>To reference a label in the assembler template,
  857. prefix it with &lsquo;<samp>%l</samp>&rsquo; (lowercase &lsquo;<samp>L</samp>&rsquo;) followed
  858. by its (zero-based) position in <var>GotoLabels</var> plus the number of input
  859. operands. For example, if the <code>asm</code> has three inputs and references two
  860. labels, refer to the first label as &lsquo;<samp>%l3</samp>&rsquo; and the second as &lsquo;<samp>%l4</samp>&rsquo;).
  861. </p>
  862. <p>Alternately, you can reference labels using the actual C label name enclosed
  863. in brackets. For example, to reference a label named <code>carry</code>, you can
  864. use &lsquo;<samp>%l[carry]</samp>&rsquo;. The label must still be listed in the <var>GotoLabels</var>
  865. section when using this approach.
  866. </p>
  867. <p>Here is an example of <code>asm goto</code> for i386:
  868. </p>
  869. <div class="example">
  870. <pre class="example">asm goto (
  871. &quot;btl %1, %0\n\t&quot;
  872. &quot;jc %l2&quot;
  873. : /* No outputs. */
  874. : &quot;r&quot; (p1), &quot;r&quot; (p2)
  875. : &quot;cc&quot;
  876. : carry);
  877. return 0;
  878. carry:
  879. return 1;
  880. </pre></div>
  881. <p>The following example shows an <code>asm goto</code> that uses a memory clobber.
  882. </p>
  883. <div class="example">
  884. <pre class="example">int frob(int x)
  885. {
  886. int y;
  887. asm goto (&quot;frob %%r5, %1; jc %l[error]; mov (%2), %%r5&quot;
  888. : /* No outputs. */
  889. : &quot;r&quot;(x), &quot;r&quot;(&amp;y)
  890. : &quot;r5&quot;, &quot;memory&quot;
  891. : error);
  892. return y;
  893. error:
  894. return -1;
  895. }
  896. </pre></div>
  897. <a name="x86Operandmodifiers"></a><a name="x86-Operand-Modifiers"></a>
  898. <h4 class="subsubsection">6.45.2.8 x86 Operand Modifiers</h4>
  899. <p>References to input, output, and goto operands in the assembler template
  900. of extended <code>asm</code> statements can use
  901. modifiers to affect the way the operands are formatted in
  902. the code output to the assembler. For example, the
  903. following code uses the &lsquo;<samp>h</samp>&rsquo; and &lsquo;<samp>b</samp>&rsquo; modifiers for x86:
  904. </p>
  905. <div class="example">
  906. <pre class="example">uint16_t num;
  907. asm volatile (&quot;xchg %h0, %b0&quot; : &quot;+a&quot; (num) );
  908. </pre></div>
  909. <p>These modifiers generate this assembler code:
  910. </p>
  911. <div class="example">
  912. <pre class="example">xchg %ah, %al
  913. </pre></div>
  914. <p>The rest of this discussion uses the following code for illustrative purposes.
  915. </p>
  916. <div class="example">
  917. <pre class="example">int main()
  918. {
  919. int iInt = 1;
  920. top:
  921. asm volatile goto (&quot;some assembler instructions here&quot;
  922. : /* No outputs. */
  923. : &quot;q&quot; (iInt), &quot;X&quot; (sizeof(unsigned char) + 1)
  924. : /* No clobbers. */
  925. : top);
  926. }
  927. </pre></div>
  928. <p>With no modifiers, this is what the output from the operands would be for the
  929. &lsquo;<samp>att</samp>&rsquo; and &lsquo;<samp>intel</samp>&rsquo; dialects of assembler:
  930. </p>
  931. <table>
  932. <thead><tr><th>Operand</th><th>&lsquo;<samp>att</samp>&rsquo;</th><th>&lsquo;<samp>intel</samp>&rsquo;</th></tr></thead>
  933. <tr><td><code>%0</code></td><td><code>%eax</code></td><td><code>eax</code></td></tr>
  934. <tr><td><code>%1</code></td><td><code>$2</code></td><td><code>2</code></td></tr>
  935. <tr><td><code>%2</code></td><td><code>$.L2</code></td><td><code>OFFSET FLAT:.L2</code></td></tr>
  936. </table>
  937. <p>The table below shows the list of supported modifiers and their effects.
  938. </p>
  939. <table>
  940. <thead><tr><th>Modifier</th><th>Description</th><th>Operand</th><th>&lsquo;<samp>att</samp>&rsquo;</th><th>&lsquo;<samp>intel</samp>&rsquo;</th></tr></thead>
  941. <tr><td><code>z</code></td><td>Print the opcode suffix for the size of the current integer operand (one of <code>b</code>/<code>w</code>/<code>l</code>/<code>q</code>).</td><td><code>%z0</code></td><td><code>l</code></td><td></td></tr>
  942. <tr><td><code>b</code></td><td>Print the QImode name of the register.</td><td><code>%b0</code></td><td><code>%al</code></td><td><code>al</code></td></tr>
  943. <tr><td><code>h</code></td><td>Print the QImode name for a &ldquo;high&rdquo; register.</td><td><code>%h0</code></td><td><code>%ah</code></td><td><code>ah</code></td></tr>
  944. <tr><td><code>w</code></td><td>Print the HImode name of the register.</td><td><code>%w0</code></td><td><code>%ax</code></td><td><code>ax</code></td></tr>
  945. <tr><td><code>k</code></td><td>Print the SImode name of the register.</td><td><code>%k0</code></td><td><code>%eax</code></td><td><code>eax</code></td></tr>
  946. <tr><td><code>q</code></td><td>Print the DImode name of the register.</td><td><code>%q0</code></td><td><code>%rax</code></td><td><code>rax</code></td></tr>
  947. <tr><td><code>l</code></td><td>Print the label name with no punctuation.</td><td><code>%l2</code></td><td><code>.L2</code></td><td><code>.L2</code></td></tr>
  948. <tr><td><code>c</code></td><td>Require a constant operand and print the constant expression with no punctuation.</td><td><code>%c1</code></td><td><code>2</code></td><td><code>2</code></td></tr>
  949. </table>
  950. <p><code>V</code> is a special modifier which prints the name of the full integer
  951. register without <code>%</code>.
  952. </p>
  953. <a name="x86floatingpointasmoperands"></a><a name="x86-Floating_002dPoint-asm-Operands"></a>
  954. <h4 class="subsubsection">6.45.2.9 x86 Floating-Point <code>asm</code> Operands</h4>
  955. <p>On x86 targets, there are several rules on the usage of stack-like registers
  956. in the operands of an <code>asm</code>. These rules apply only to the operands
  957. that are stack-like registers:
  958. </p>
  959. <ol>
  960. <li> Given a set of input registers that die in an <code>asm</code>, it is
  961. necessary to know which are implicitly popped by the <code>asm</code>, and
  962. which must be explicitly popped by GCC.
  963. <p>An input register that is implicitly popped by the <code>asm</code> must be
  964. explicitly clobbered, unless it is constrained to match an
  965. output operand.
  966. </p>
  967. </li><li> For any input register that is implicitly popped by an <code>asm</code>, it is
  968. necessary to know how to adjust the stack to compensate for the pop.
  969. If any non-popped input is closer to the top of the reg-stack than
  970. the implicitly popped register, it would not be possible to know what the
  971. stack looked like&mdash;it&rsquo;s not clear how the rest of the stack &ldquo;slides
  972. up&rdquo;.
  973. <p>All implicitly popped input registers must be closer to the top of
  974. the reg-stack than any input that is not implicitly popped.
  975. </p>
  976. <p>It is possible that if an input dies in an <code>asm</code>, the compiler might
  977. use the input register for an output reload. Consider this example:
  978. </p>
  979. <div class="smallexample">
  980. <pre class="smallexample">asm (&quot;foo&quot; : &quot;=t&quot; (a) : &quot;f&quot; (b));
  981. </pre></div>
  982. <p>This code says that input <code>b</code> is not popped by the <code>asm</code>, and that
  983. the <code>asm</code> pushes a result onto the reg-stack, i.e., the stack is one
  984. deeper after the <code>asm</code> than it was before. But, it is possible that
  985. reload may think that it can use the same register for both the input and
  986. the output.
  987. </p>
  988. <p>To prevent this from happening,
  989. if any input operand uses the &lsquo;<samp>f</samp>&rsquo; constraint, all output register
  990. constraints must use the &lsquo;<samp>&amp;</samp>&rsquo; early-clobber modifier.
  991. </p>
  992. <p>The example above is correctly written as:
  993. </p>
  994. <div class="smallexample">
  995. <pre class="smallexample">asm (&quot;foo&quot; : &quot;=&amp;t&quot; (a) : &quot;f&quot; (b));
  996. </pre></div>
  997. </li><li> Some operands need to be in particular places on the stack. All
  998. output operands fall in this category&mdash;GCC has no other way to
  999. know which registers the outputs appear in unless you indicate
  1000. this in the constraints.
  1001. <p>Output operands must specifically indicate which register an output
  1002. appears in after an <code>asm</code>. &lsquo;<samp>=f</samp>&rsquo; is not allowed: the operand
  1003. constraints must select a class with a single register.
  1004. </p>
  1005. </li><li> Output operands may not be &ldquo;inserted&rdquo; between existing stack registers.
  1006. Since no 387 opcode uses a read/write operand, all output operands
  1007. are dead before the <code>asm</code>, and are pushed by the <code>asm</code>.
  1008. It makes no sense to push anywhere but the top of the reg-stack.
  1009. <p>Output operands must start at the top of the reg-stack: output
  1010. operands may not &ldquo;skip&rdquo; a register.
  1011. </p>
  1012. </li><li> Some <code>asm</code> statements may need extra stack space for internal
  1013. calculations. This can be guaranteed by clobbering stack registers
  1014. unrelated to the inputs and outputs.
  1015. </li></ol>
  1016. <p>This <code>asm</code>
  1017. takes one input, which is internally popped, and produces two outputs.
  1018. </p>
  1019. <div class="smallexample">
  1020. <pre class="smallexample">asm (&quot;fsincos&quot; : &quot;=t&quot; (cos), &quot;=u&quot; (sin) : &quot;0&quot; (inp));
  1021. </pre></div>
  1022. <p>This <code>asm</code> takes two inputs, which are popped by the <code>fyl2xp1</code> opcode,
  1023. and replaces them with one output. The <code>st(1)</code> clobber is necessary
  1024. for the compiler to know that <code>fyl2xp1</code> pops both inputs.
  1025. </p>
  1026. <div class="smallexample">
  1027. <pre class="smallexample">asm (&quot;fyl2xp1&quot; : &quot;=t&quot; (result) : &quot;0&quot; (x), &quot;u&quot; (y) : &quot;st(1)&quot;);
  1028. </pre></div>
  1029. <hr>
  1030. <div class="header">
  1031. <p>
  1032. Next: <a href="Constraints.html#Constraints" accesskey="n" rel="next">Constraints</a>, Previous: <a href="Basic-Asm.html#Basic-Asm" accesskey="p" rel="prev">Basic 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>
  1033. </div>
  1034. </body>
  1035. </html>