Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Extended-Asm.html 60KB

3 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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>Extended Asm (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Extended Asm (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Extended 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="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.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="Extended-Asm"></a>
  62. <div class="header">
  63. <p>
  64. 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>
  65. </div>
  66. <hr>
  67. <a name="Extended-Asm-_002d-Assembler-Instructions-with-C-Expression-Operands"></a>
  68. <h4 class="subsection">6.47.2 Extended Asm - Assembler Instructions with C Expression Operands</h4>
  69. <a name="index-extended-asm"></a>
  70. <a name="index-assembly-language-in-C_002c-extended"></a>
  71. <p>With extended <code>asm</code> you can read and write C variables from
  72. assembler and perform jumps from assembler code to C labels.
  73. Extended <code>asm</code> syntax uses colons (&lsquo;<samp>:</samp>&rsquo;) to delimit
  74. the operand parameters after the assembler template:
  75. </p>
  76. <div class="example">
  77. <pre class="example">asm <var>asm-qualifiers</var> ( <var>AssemblerTemplate</var>
  78. : <var>OutputOperands</var>
  79. <span class="roman">[</span> : <var>InputOperands</var>
  80. <span class="roman">[</span> : <var>Clobbers</var> <span class="roman">]</span> <span class="roman">]</span>)
  81. asm <var>asm-qualifiers</var> ( <var>AssemblerTemplate</var>
  82. :
  83. : <var>InputOperands</var>
  84. : <var>Clobbers</var>
  85. : <var>GotoLabels</var>)
  86. </pre></div>
  87. <p>where in the last form, <var>asm-qualifiers</var> contains <code>goto</code> (and in the
  88. first form, not).
  89. </p>
  90. <p>The <code>asm</code> keyword is a GNU extension.
  91. When writing code that can be compiled with <samp>-ansi</samp> and the
  92. various <samp>-std</samp> options, use <code>__asm__</code> instead of
  93. <code>asm</code> (see <a href="Alternate-Keywords.html#Alternate-Keywords">Alternate Keywords</a>).
  94. </p>
  95. <a name="Qualifiers-2"></a>
  96. <h4 class="subsubheading">Qualifiers</h4>
  97. <dl compact="compact">
  98. <dt><code>volatile</code></dt>
  99. <dd><p>The typical use of extended <code>asm</code> statements is to manipulate input
  100. values to produce output values. However, your <code>asm</code> statements may
  101. also produce side effects. If so, you may need to use the <code>volatile</code>
  102. qualifier to disable certain optimizations. See <a href="#Volatile">Volatile</a>.
  103. </p>
  104. </dd>
  105. <dt><code>inline</code></dt>
  106. <dd><p>If you use the <code>inline</code> qualifier, then for inlining purposes the size
  107. of the <code>asm</code> statement is taken as the smallest size possible
  108. (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-and-Scratch-Registers">Clobbers and Scratch Registers</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.47.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 the <code>volatile</code> qualifier 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 <code>volatile 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 <code>volatile asm</code> statement,
  271. as in the 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 <code>volatile asm</code>
  278. statement. To make it work as expected, add an artificial dependency to
  279. the <code>asm</code> by referencing a variable in the subsequent code, for
  280. example:
  281. </p>
  282. <div class="example">
  283. <pre class="example">asm volatile (&quot;mtfsf 255,%1&quot; : &quot;=X&quot; (sum) : &quot;f&quot; (fpenv));
  284. sum = x + y;
  285. </pre></div>
  286. <p>Under certain circumstances, GCC may duplicate (or remove duplicates of) your
  287. assembly code when optimizing. This can lead to unexpected duplicate symbol
  288. errors during compilation if your <code>asm</code> code defines symbols or labels.
  289. Using &lsquo;<samp>%=</samp>&rsquo;
  290. (see <a href="#AssemblerTemplate">AssemblerTemplate</a>) may help resolve this problem.
  291. </p>
  292. <a name="AssemblerTemplate"></a><a name="Assembler-Template"></a>
  293. <h4 class="subsubsection">6.47.2.2 Assembler Template</h4>
  294. <a name="index-asm-assembler-template"></a>
  295. <p>An assembler template is a literal string containing assembler instructions.
  296. The compiler replaces tokens in the template that refer
  297. to inputs, outputs, and goto labels,
  298. and then outputs the resulting string to the assembler. The
  299. string can contain any instructions recognized by the assembler, including
  300. directives. GCC does not parse the assembler instructions
  301. themselves and does not know what they mean or even whether they are valid
  302. assembler input. However, it does count the statements
  303. (see <a href="Size-of-an-asm.html#Size-of-an-asm">Size of an asm</a>).
  304. </p>
  305. <p>You may place multiple assembler instructions together in a single <code>asm</code>
  306. string, separated by the characters normally used in assembly code for the
  307. system. A combination that works in most places is a newline to break the
  308. line, plus a tab character to move to the instruction field (written as
  309. &lsquo;<samp>\n\t</samp>&rsquo;).
  310. Some assemblers allow semicolons as a line separator. However, note
  311. that some assembler dialects use semicolons to start a comment.
  312. </p>
  313. <p>Do not expect a sequence of <code>asm</code> statements to remain perfectly
  314. consecutive after compilation, even when you are using the <code>volatile</code>
  315. qualifier. If certain instructions need to remain consecutive in the output,
  316. put them in a single multi-instruction <code>asm</code> statement.
  317. </p>
  318. <p>Accessing data from C programs without using input/output operands (such as
  319. by using global symbols directly from the assembler template) may not work as
  320. expected. Similarly, calling functions directly from an assembler template
  321. requires a detailed understanding of the target assembler and ABI.
  322. </p>
  323. <p>Since GCC does not parse the assembler template,
  324. it has no visibility of any
  325. symbols it references. This may result in GCC discarding those symbols as
  326. unreferenced unless they are also listed as input, output, or goto operands.
  327. </p>
  328. <a name="Special-format-strings"></a>
  329. <h4 class="subsubheading">Special format strings</h4>
  330. <p>In addition to the tokens described by the input, output, and goto operands,
  331. these tokens have special meanings in the assembler template:
  332. </p>
  333. <dl compact="compact">
  334. <dt>&lsquo;<samp>%%</samp>&rsquo;</dt>
  335. <dd><p>Outputs a single &lsquo;<samp>%</samp>&rsquo; into the assembler code.
  336. </p>
  337. </dd>
  338. <dt>&lsquo;<samp>%=</samp>&rsquo;</dt>
  339. <dd><p>Outputs a number that is unique to each instance of the <code>asm</code>
  340. statement in the entire compilation. This option is useful when creating local
  341. labels and referring to them multiple times in a single template that
  342. generates multiple assembler instructions.
  343. </p>
  344. </dd>
  345. <dt>&lsquo;<samp>%{</samp>&rsquo;</dt>
  346. <dt>&lsquo;<samp>%|</samp>&rsquo;</dt>
  347. <dt>&lsquo;<samp>%}</samp>&rsquo;</dt>
  348. <dd><p>Outputs &lsquo;<samp>{</samp>&rsquo;, &lsquo;<samp>|</samp>&rsquo;, and &lsquo;<samp>}</samp>&rsquo; characters (respectively)
  349. into the assembler code. When unescaped, these characters have special
  350. meaning to indicate multiple assembler dialects, as described below.
  351. </p></dd>
  352. </dl>
  353. <a name="Multiple-assembler-dialects-in-asm-templates"></a>
  354. <h4 class="subsubheading">Multiple assembler dialects in <code>asm</code> templates</h4>
  355. <p>On targets such as x86, GCC supports multiple assembler dialects.
  356. The <samp>-masm</samp> option controls which dialect GCC uses as its
  357. default for inline assembler. The target-specific documentation for the
  358. <samp>-masm</samp> option contains the list of supported dialects, as well as the
  359. default dialect if the option is not specified. This information may be
  360. important to understand, since assembler code that works correctly when
  361. compiled using one dialect will likely fail if compiled using another.
  362. See <a href="x86-Options.html#x86-Options">x86 Options</a>.
  363. </p>
  364. <p>If your code needs to support multiple assembler dialects (for example, if
  365. you are writing public headers that need to support a variety of compilation
  366. options), use constructs of this form:
  367. </p>
  368. <div class="example">
  369. <pre class="example">{ dialect0 | dialect1 | dialect2... }
  370. </pre></div>
  371. <p>This construct outputs <code>dialect0</code>
  372. when using dialect #0 to compile the code,
  373. <code>dialect1</code> for dialect #1, etc. If there are fewer alternatives within the
  374. braces than the number of dialects the compiler supports, the construct
  375. outputs nothing.
  376. </p>
  377. <p>For example, if an x86 compiler supports two dialects
  378. (&lsquo;<samp>att</samp>&rsquo;, &lsquo;<samp>intel</samp>&rsquo;), an
  379. assembler template such as this:
  380. </p>
  381. <div class="example">
  382. <pre class="example">&quot;bt{l %[Offset],%[Base] | %[Base],%[Offset]}; jc %l2&quot;
  383. </pre></div>
  384. <p>is equivalent to one of
  385. </p>
  386. <div class="example">
  387. <pre class="example">&quot;btl %[Offset],%[Base] ; jc %l2&quot; <span class="roman">/* att dialect */</span>
  388. &quot;bt %[Base],%[Offset]; jc %l2&quot; <span class="roman">/* intel dialect */</span>
  389. </pre></div>
  390. <p>Using that same compiler, this code:
  391. </p>
  392. <div class="example">
  393. <pre class="example">&quot;xchg{l}\t{%%}ebx, %1&quot;
  394. </pre></div>
  395. <p>corresponds to either
  396. </p>
  397. <div class="example">
  398. <pre class="example">&quot;xchgl\t%%ebx, %1&quot; <span class="roman">/* att dialect */</span>
  399. &quot;xchg\tebx, %1&quot; <span class="roman">/* intel dialect */</span>
  400. </pre></div>
  401. <p>There is no support for nesting dialect alternatives.
  402. </p>
  403. <a name="OutputOperands"></a><a name="Output-Operands"></a>
  404. <h4 class="subsubsection">6.47.2.3 Output Operands</h4>
  405. <a name="index-asm-output-operands"></a>
  406. <p>An <code>asm</code> statement has zero or more output operands indicating the names
  407. of C variables modified by the assembler code.
  408. </p>
  409. <p>In this i386 example, <code>old</code> (referred to in the template string as
  410. <code>%0</code>) and <code>*Base</code> (as <code>%1</code>) are outputs and <code>Offset</code>
  411. (<code>%2</code>) is an input:
  412. </p>
  413. <div class="example">
  414. <pre class="example">bool old;
  415. __asm__ (&quot;btsl %2,%1\n\t&quot; // Turn on zero-based bit #Offset in Base.
  416. &quot;sbb %0,%0&quot; // Use the CF to calculate old.
  417. : &quot;=r&quot; (old), &quot;+rm&quot; (*Base)
  418. : &quot;Ir&quot; (Offset)
  419. : &quot;cc&quot;);
  420. return old;
  421. </pre></div>
  422. <p>Operands are separated by commas. Each operand has this format:
  423. </p>
  424. <div class="example">
  425. <pre class="example"><span class="roman">[</span> [<var>asmSymbolicName</var>] <span class="roman">]</span> <var>constraint</var> (<var>cvariablename</var>)
  426. </pre></div>
  427. <dl compact="compact">
  428. <dt><var>asmSymbolicName</var></dt>
  429. <dd><p>Specifies a symbolic name for the operand.
  430. Reference the name in the assembler template
  431. by enclosing it in square brackets
  432. (i.e. &lsquo;<samp>%[Value]</samp>&rsquo;). The scope of the name is the <code>asm</code> statement
  433. that contains the definition. Any valid C variable name is acceptable,
  434. including names already defined in the surrounding code. No two operands
  435. within the same <code>asm</code> statement can use the same symbolic name.
  436. </p>
  437. <p>When not using an <var>asmSymbolicName</var>, use the (zero-based) position
  438. of the operand
  439. in the list of operands in the assembler template. For example if there are
  440. three output operands, use &lsquo;<samp>%0</samp>&rsquo; in the template to refer to the first,
  441. &lsquo;<samp>%1</samp>&rsquo; for the second, and &lsquo;<samp>%2</samp>&rsquo; for the third.
  442. </p>
  443. </dd>
  444. <dt><var>constraint</var></dt>
  445. <dd><p>A string constant specifying constraints on the placement of the operand;
  446. See <a href="Constraints.html#Constraints">Constraints</a>, for details.
  447. </p>
  448. <p>Output constraints must begin with either &lsquo;<samp>=</samp>&rsquo; (a variable overwriting an
  449. existing value) or &lsquo;<samp>+</samp>&rsquo; (when reading and writing). When using
  450. &lsquo;<samp>=</samp>&rsquo;, do not assume the location contains the existing value
  451. on entry to the <code>asm</code>, except
  452. when the operand is tied to an input; see <a href="#InputOperands">Input Operands</a>.
  453. </p>
  454. <p>After the prefix, there must be one or more additional constraints
  455. (see <a href="Constraints.html#Constraints">Constraints</a>) that describe where the value resides. Common
  456. constraints include &lsquo;<samp>r</samp>&rsquo; for register and &lsquo;<samp>m</samp>&rsquo; for memory.
  457. When you list more than one possible location (for example, <code>&quot;=rm&quot;</code>),
  458. the compiler chooses the most efficient one based on the current context.
  459. If you list as many alternates as the <code>asm</code> statement allows, you permit
  460. the optimizers to produce the best possible code.
  461. If you must use a specific register, but your Machine Constraints do not
  462. provide sufficient control to select the specific register you want,
  463. local register variables may provide a solution (see <a href="Local-Register-Variables.html#Local-Register-Variables">Local Register Variables</a>).
  464. </p>
  465. </dd>
  466. <dt><var>cvariablename</var></dt>
  467. <dd><p>Specifies a C lvalue expression to hold the output, typically a variable name.
  468. The enclosing parentheses are a required part of the syntax.
  469. </p>
  470. </dd>
  471. </dl>
  472. <p>When the compiler selects the registers to use to
  473. represent the output operands, it does not use any of the clobbered registers
  474. (see <a href="#Clobbers-and-Scratch-Registers">Clobbers and Scratch Registers</a>).
  475. </p>
  476. <p>Output operand expressions must be lvalues. The compiler cannot check whether
  477. the operands have data types that are reasonable for the instruction being
  478. executed. For output expressions that are not directly addressable (for
  479. example a bit-field), the constraint must allow a register. In that case, GCC
  480. uses the register as the output of the <code>asm</code>, and then stores that
  481. register into the output.
  482. </p>
  483. <p>Operands using the &lsquo;<samp>+</samp>&rsquo; constraint modifier count as two operands
  484. (that is, both as input and output) towards the total maximum of 30 operands
  485. per <code>asm</code> statement.
  486. </p>
  487. <p>Use the &lsquo;<samp>&amp;</samp>&rsquo; constraint modifier (see <a href="Modifiers.html#Modifiers">Modifiers</a>) on all output
  488. operands that must not overlap an input. Otherwise,
  489. GCC may allocate the output operand in the same register as an unrelated
  490. input operand, on the assumption that the assembler code consumes its
  491. inputs before producing outputs. This assumption may be false if the assembler
  492. code actually consists of more than one instruction.
  493. </p>
  494. <p>The same problem can occur if one output parameter (<var>a</var>) allows a register
  495. constraint and another output parameter (<var>b</var>) allows a memory constraint.
  496. The code generated by GCC to access the memory address in <var>b</var> can contain
  497. registers which <em>might</em> be shared by <var>a</var>, and GCC considers those
  498. registers to be inputs to the asm. As above, GCC assumes that such input
  499. registers are consumed before any outputs are written. This assumption may
  500. result in incorrect behavior if the <code>asm</code> statement writes to <var>a</var>
  501. before using
  502. <var>b</var>. Combining the &lsquo;<samp>&amp;</samp>&rsquo; modifier with the register constraint on <var>a</var>
  503. ensures that modifying <var>a</var> does not affect the address referenced by
  504. <var>b</var>. Otherwise, the location of <var>b</var>
  505. is undefined if <var>a</var> is modified before using <var>b</var>.
  506. </p>
  507. <p><code>asm</code> supports operand modifiers on operands (for example &lsquo;<samp>%k2</samp>&rsquo;
  508. instead of simply &lsquo;<samp>%2</samp>&rsquo;). Typically these qualifiers are hardware
  509. dependent. The list of supported modifiers for x86 is found at
  510. <a href="#x86Operandmodifiers">x86 Operand modifiers</a>.
  511. </p>
  512. <p>If the C code that follows the <code>asm</code> makes no use of any of the output
  513. operands, use <code>volatile</code> for the <code>asm</code> statement to prevent the
  514. optimizers from discarding the <code>asm</code> statement as unneeded
  515. (see <a href="#Volatile">Volatile</a>).
  516. </p>
  517. <p>This code makes no use of the optional <var>asmSymbolicName</var>. Therefore it
  518. references the first output operand as <code>%0</code> (were there a second, it
  519. would be <code>%1</code>, etc). The number of the first input operand is one greater
  520. than that of the last output operand. In this i386 example, that makes
  521. <code>Mask</code> referenced as <code>%1</code>:
  522. </p>
  523. <div class="example">
  524. <pre class="example">uint32_t Mask = 1234;
  525. uint32_t Index;
  526. asm (&quot;bsfl %1, %0&quot;
  527. : &quot;=r&quot; (Index)
  528. : &quot;r&quot; (Mask)
  529. : &quot;cc&quot;);
  530. </pre></div>
  531. <p>That code overwrites the variable <code>Index</code> (&lsquo;<samp>=</samp>&rsquo;),
  532. placing the value in a register (&lsquo;<samp>r</samp>&rsquo;).
  533. Using the generic &lsquo;<samp>r</samp>&rsquo; constraint instead of a constraint for a specific
  534. register allows the compiler to pick the register to use, which can result
  535. in more efficient code. This may not be possible if an assembler instruction
  536. requires a specific register.
  537. </p>
  538. <p>The following i386 example uses the <var>asmSymbolicName</var> syntax.
  539. It produces the
  540. same result as the code above, but some may consider it more readable or more
  541. maintainable since reordering index numbers is not necessary when adding or
  542. removing operands. The names <code>aIndex</code> and <code>aMask</code>
  543. are only used in this example to emphasize which
  544. names get used where.
  545. It is acceptable to reuse the names <code>Index</code> and <code>Mask</code>.
  546. </p>
  547. <div class="example">
  548. <pre class="example">uint32_t Mask = 1234;
  549. uint32_t Index;
  550. asm (&quot;bsfl %[aMask], %[aIndex]&quot;
  551. : [aIndex] &quot;=r&quot; (Index)
  552. : [aMask] &quot;r&quot; (Mask)
  553. : &quot;cc&quot;);
  554. </pre></div>
  555. <p>Here are some more examples of output operands.
  556. </p>
  557. <div class="example">
  558. <pre class="example">uint32_t c = 1;
  559. uint32_t d;
  560. uint32_t *e = &amp;c;
  561. asm (&quot;mov %[e], %[d]&quot;
  562. : [d] &quot;=rm&quot; (d)
  563. : [e] &quot;rm&quot; (*e));
  564. </pre></div>
  565. <p>Here, <code>d</code> may either be in a register or in memory. Since the compiler
  566. might already have the current value of the <code>uint32_t</code> location
  567. pointed to by <code>e</code>
  568. in a register, you can enable it to choose the best location
  569. for <code>d</code> by specifying both constraints.
  570. </p>
  571. <a name="FlagOutputOperands"></a><a name="Flag-Output-Operands"></a>
  572. <h4 class="subsubsection">6.47.2.4 Flag Output Operands</h4>
  573. <a name="index-asm-flag-output-operands"></a>
  574. <p>Some targets have a special register that holds the &ldquo;flags&rdquo; for the
  575. result of an operation or comparison. Normally, the contents of that
  576. register are either unmodifed by the asm, or the <code>asm</code> statement is
  577. considered to clobber the contents.
  578. </p>
  579. <p>On some targets, a special form of output operand exists by which
  580. conditions in the flags register may be outputs of the asm. The set of
  581. conditions supported are target specific, but the general rule is that
  582. the output variable must be a scalar integer, and the value is boolean.
  583. When supported, the target defines the preprocessor symbol
  584. <code>__GCC_ASM_FLAG_OUTPUTS__</code>.
  585. </p>
  586. <p>Because of the special nature of the flag output operands, the constraint
  587. may not include alternatives.
  588. </p>
  589. <p>Most often, the target has only one flags register, and thus is an implied
  590. operand of many instructions. In this case, the operand should not be
  591. referenced within the assembler template via <code>%0</code> etc, as there&rsquo;s
  592. no corresponding text in the assembly language.
  593. </p>
  594. <dl compact="compact">
  595. <dt>ARM</dt>
  596. <dt>AArch64</dt>
  597. <dd><p>The flag output constraints for the ARM family are of the form
  598. &lsquo;<samp>=@cc<var>cond</var></samp>&rsquo; where <var>cond</var> is one of the standard
  599. conditions defined in the ARM ARM for <code>ConditionHolds</code>.
  600. </p>
  601. <dl compact="compact">
  602. <dt><code>eq</code></dt>
  603. <dd><p>Z flag set, or equal
  604. </p></dd>
  605. <dt><code>ne</code></dt>
  606. <dd><p>Z flag clear or not equal
  607. </p></dd>
  608. <dt><code>cs</code></dt>
  609. <dt><code>hs</code></dt>
  610. <dd><p>C flag set or unsigned greater than equal
  611. </p></dd>
  612. <dt><code>cc</code></dt>
  613. <dt><code>lo</code></dt>
  614. <dd><p>C flag clear or unsigned less than
  615. </p></dd>
  616. <dt><code>mi</code></dt>
  617. <dd><p>N flag set or &ldquo;minus&rdquo;
  618. </p></dd>
  619. <dt><code>pl</code></dt>
  620. <dd><p>N flag clear or &ldquo;plus&rdquo;
  621. </p></dd>
  622. <dt><code>vs</code></dt>
  623. <dd><p>V flag set or signed overflow
  624. </p></dd>
  625. <dt><code>vc</code></dt>
  626. <dd><p>V flag clear
  627. </p></dd>
  628. <dt><code>hi</code></dt>
  629. <dd><p>unsigned greater than
  630. </p></dd>
  631. <dt><code>ls</code></dt>
  632. <dd><p>unsigned less than equal
  633. </p></dd>
  634. <dt><code>ge</code></dt>
  635. <dd><p>signed greater than equal
  636. </p></dd>
  637. <dt><code>lt</code></dt>
  638. <dd><p>signed less than
  639. </p></dd>
  640. <dt><code>gt</code></dt>
  641. <dd><p>signed greater than
  642. </p></dd>
  643. <dt><code>le</code></dt>
  644. <dd><p>signed less than equal
  645. </p></dd>
  646. </dl>
  647. <p>The flag output constraints are not supported in thumb1 mode.
  648. </p>
  649. </dd>
  650. <dt>x86 family</dt>
  651. <dd><p>The flag output constraints for the x86 family are of the form
  652. &lsquo;<samp>=@cc<var>cond</var></samp>&rsquo; where <var>cond</var> is one of the standard
  653. conditions defined in the ISA manual for <code>j<var>cc</var></code> or
  654. <code>set<var>cc</var></code>.
  655. </p>
  656. <dl compact="compact">
  657. <dt><code>a</code></dt>
  658. <dd><p>&ldquo;above&rdquo; or unsigned greater than
  659. </p></dd>
  660. <dt><code>ae</code></dt>
  661. <dd><p>&ldquo;above or equal&rdquo; or unsigned greater than or equal
  662. </p></dd>
  663. <dt><code>b</code></dt>
  664. <dd><p>&ldquo;below&rdquo; or unsigned less than
  665. </p></dd>
  666. <dt><code>be</code></dt>
  667. <dd><p>&ldquo;below or equal&rdquo; or unsigned less than or equal
  668. </p></dd>
  669. <dt><code>c</code></dt>
  670. <dd><p>carry flag set
  671. </p></dd>
  672. <dt><code>e</code></dt>
  673. <dt><code>z</code></dt>
  674. <dd><p>&ldquo;equal&rdquo; or zero flag set
  675. </p></dd>
  676. <dt><code>g</code></dt>
  677. <dd><p>signed greater than
  678. </p></dd>
  679. <dt><code>ge</code></dt>
  680. <dd><p>signed greater than or equal
  681. </p></dd>
  682. <dt><code>l</code></dt>
  683. <dd><p>signed less than
  684. </p></dd>
  685. <dt><code>le</code></dt>
  686. <dd><p>signed less than or equal
  687. </p></dd>
  688. <dt><code>o</code></dt>
  689. <dd><p>overflow flag set
  690. </p></dd>
  691. <dt><code>p</code></dt>
  692. <dd><p>parity flag set
  693. </p></dd>
  694. <dt><code>s</code></dt>
  695. <dd><p>sign flag set
  696. </p></dd>
  697. <dt><code>na</code></dt>
  698. <dt><code>nae</code></dt>
  699. <dt><code>nb</code></dt>
  700. <dt><code>nbe</code></dt>
  701. <dt><code>nc</code></dt>
  702. <dt><code>ne</code></dt>
  703. <dt><code>ng</code></dt>
  704. <dt><code>nge</code></dt>
  705. <dt><code>nl</code></dt>
  706. <dt><code>nle</code></dt>
  707. <dt><code>no</code></dt>
  708. <dt><code>np</code></dt>
  709. <dt><code>ns</code></dt>
  710. <dt><code>nz</code></dt>
  711. <dd><p>&ldquo;not&rdquo; <var>flag</var>, or inverted versions of those above
  712. </p></dd>
  713. </dl>
  714. </dd>
  715. </dl>
  716. <a name="InputOperands"></a><a name="Input-Operands"></a>
  717. <h4 class="subsubsection">6.47.2.5 Input Operands</h4>
  718. <a name="index-asm-input-operands"></a>
  719. <a name="index-asm-expressions"></a>
  720. <p>Input operands make values from C variables and expressions available to the
  721. assembly code.
  722. </p>
  723. <p>Operands are separated by commas. Each operand has this format:
  724. </p>
  725. <div class="example">
  726. <pre class="example"><span class="roman">[</span> [<var>asmSymbolicName</var>] <span class="roman">]</span> <var>constraint</var> (<var>cexpression</var>)
  727. </pre></div>
  728. <dl compact="compact">
  729. <dt><var>asmSymbolicName</var></dt>
  730. <dd><p>Specifies a symbolic name for the operand.
  731. Reference the name in the assembler template
  732. by enclosing it in square brackets
  733. (i.e. &lsquo;<samp>%[Value]</samp>&rsquo;). The scope of the name is the <code>asm</code> statement
  734. that contains the definition. Any valid C variable name is acceptable,
  735. including names already defined in the surrounding code. No two operands
  736. within the same <code>asm</code> statement can use the same symbolic name.
  737. </p>
  738. <p>When not using an <var>asmSymbolicName</var>, use the (zero-based) position
  739. of the operand
  740. in the list of operands in the assembler template. For example if there are
  741. two output operands and three inputs,
  742. use &lsquo;<samp>%2</samp>&rsquo; in the template to refer to the first input operand,
  743. &lsquo;<samp>%3</samp>&rsquo; for the second, and &lsquo;<samp>%4</samp>&rsquo; for the third.
  744. </p>
  745. </dd>
  746. <dt><var>constraint</var></dt>
  747. <dd><p>A string constant specifying constraints on the placement of the operand;
  748. See <a href="Constraints.html#Constraints">Constraints</a>, for details.
  749. </p>
  750. <p>Input constraint strings may not begin with either &lsquo;<samp>=</samp>&rsquo; or &lsquo;<samp>+</samp>&rsquo;.
  751. When you list more than one possible location (for example, &lsquo;<samp>&quot;irm&quot;</samp>&rsquo;),
  752. the compiler chooses the most efficient one based on the current context.
  753. If you must use a specific register, but your Machine Constraints do not
  754. provide sufficient control to select the specific register you want,
  755. local register variables may provide a solution (see <a href="Local-Register-Variables.html#Local-Register-Variables">Local Register Variables</a>).
  756. </p>
  757. <p>Input constraints can also be digits (for example, <code>&quot;0&quot;</code>). This indicates
  758. that the specified input must be in the same place as the output constraint
  759. at the (zero-based) index in the output constraint list.
  760. When using <var>asmSymbolicName</var> syntax for the output operands,
  761. you may use these names (enclosed in brackets &lsquo;<samp>[]</samp>&rsquo;) instead of digits.
  762. </p>
  763. </dd>
  764. <dt><var>cexpression</var></dt>
  765. <dd><p>This is the C variable or expression being passed to the <code>asm</code> statement
  766. as input. The enclosing parentheses are a required part of the syntax.
  767. </p>
  768. </dd>
  769. </dl>
  770. <p>When the compiler selects the registers to use to represent the input
  771. operands, it does not use any of the clobbered registers
  772. (see <a href="#Clobbers-and-Scratch-Registers">Clobbers and Scratch Registers</a>).
  773. </p>
  774. <p>If there are no output operands but there are input operands, place two
  775. consecutive colons where the output operands would go:
  776. </p>
  777. <div class="example">
  778. <pre class="example">__asm__ (&quot;some instructions&quot;
  779. : /* No outputs. */
  780. : &quot;r&quot; (Offset / 8));
  781. </pre></div>
  782. <p><strong>Warning:</strong> Do <em>not</em> modify the contents of input-only operands
  783. (except for inputs tied to outputs). The compiler assumes that on exit from
  784. the <code>asm</code> statement these operands contain the same values as they
  785. had before executing the statement.
  786. It is <em>not</em> possible to use clobbers
  787. to inform the compiler that the values in these inputs are changing. One
  788. common work-around is to tie the changing input variable to an output variable
  789. that never gets used. Note, however, that if the code that follows the
  790. <code>asm</code> statement makes no use of any of the output operands, the GCC
  791. optimizers may discard the <code>asm</code> statement as unneeded
  792. (see <a href="#Volatile">Volatile</a>).
  793. </p>
  794. <p><code>asm</code> supports operand modifiers on operands (for example &lsquo;<samp>%k2</samp>&rsquo;
  795. instead of simply &lsquo;<samp>%2</samp>&rsquo;). Typically these qualifiers are hardware
  796. dependent. The list of supported modifiers for x86 is found at
  797. <a href="#x86Operandmodifiers">x86 Operand modifiers</a>.
  798. </p>
  799. <p>In this example using the fictitious <code>combine</code> instruction, the
  800. constraint <code>&quot;0&quot;</code> for input operand 1 says that it must occupy the same
  801. location as output operand 0. Only input operands may use numbers in
  802. constraints, and they must each refer to an output operand. Only a number (or
  803. the symbolic assembler name) in the constraint can guarantee that one operand
  804. is in the same place as another. The mere fact that <code>foo</code> is the value of
  805. both operands is not enough to guarantee that they are in the same place in
  806. the generated assembler code.
  807. </p>
  808. <div class="example">
  809. <pre class="example">asm (&quot;combine %2, %0&quot;
  810. : &quot;=r&quot; (foo)
  811. : &quot;0&quot; (foo), &quot;g&quot; (bar));
  812. </pre></div>
  813. <p>Here is an example using symbolic names.
  814. </p>
  815. <div class="example">
  816. <pre class="example">asm (&quot;cmoveq %1, %2, %[result]&quot;
  817. : [result] &quot;=r&quot;(result)
  818. : &quot;r&quot; (test), &quot;r&quot; (new), &quot;[result]&quot; (old));
  819. </pre></div>
  820. <a name="Clobbers-and-Scratch-Registers"></a><a name="Clobbers-and-Scratch-Registers-1"></a>
  821. <h4 class="subsubsection">6.47.2.6 Clobbers and Scratch Registers</h4>
  822. <a name="index-asm-clobbers"></a>
  823. <a name="index-asm-scratch-registers"></a>
  824. <p>While the compiler is aware of changes to entries listed in the output
  825. operands, the inline <code>asm</code> code may modify more than just the outputs. For
  826. example, calculations may require additional registers, or the processor may
  827. overwrite a register as a side effect of a particular assembler instruction.
  828. In order to inform the compiler of these changes, list them in the clobber
  829. list. Clobber list items are either register names or the special clobbers
  830. (listed below). Each clobber list item is a string constant
  831. enclosed in double quotes and separated by commas.
  832. </p>
  833. <p>Clobber descriptions may not in any way overlap with an input or output
  834. operand. For example, you may not have an operand describing a register class
  835. with one member when listing that register in the clobber list. Variables
  836. declared to live in specific registers (see <a href="Explicit-Register-Variables.html#Explicit-Register-Variables">Explicit Register Variables</a>) and used
  837. as <code>asm</code> input or output operands must have no part mentioned in the
  838. clobber description. In particular, there is no way to specify that input
  839. operands get modified without also specifying them as output operands.
  840. </p>
  841. <p>When the compiler selects which registers to use to represent input and output
  842. operands, it does not use any of the clobbered registers. As a result,
  843. clobbered registers are available for any use in the assembler code.
  844. </p>
  845. <p>Another restriction is that the clobber list should not contain the
  846. stack pointer register. This is because the compiler requires the
  847. value of the stack pointer to be the same after an <code>asm</code>
  848. statement as it was on entry to the statement. However, previous
  849. versions of GCC did not enforce this rule and allowed the stack
  850. pointer to appear in the list, with unclear semantics. This behavior
  851. is deprecated and listing the stack pointer may become an error in
  852. future versions of GCC.
  853. </p>
  854. <p>Here is a realistic example for the VAX showing the use of clobbered
  855. registers:
  856. </p>
  857. <div class="example">
  858. <pre class="example">asm volatile (&quot;movc3 %0, %1, %2&quot;
  859. : /* No outputs. */
  860. : &quot;g&quot; (from), &quot;g&quot; (to), &quot;g&quot; (count)
  861. : &quot;r0&quot;, &quot;r1&quot;, &quot;r2&quot;, &quot;r3&quot;, &quot;r4&quot;, &quot;r5&quot;, &quot;memory&quot;);
  862. </pre></div>
  863. <p>Also, there are two special clobber arguments:
  864. </p>
  865. <dl compact="compact">
  866. <dt><code>&quot;cc&quot;</code></dt>
  867. <dd><p>The <code>&quot;cc&quot;</code> clobber indicates that the assembler code modifies the flags
  868. register. On some machines, GCC represents the condition codes as a specific
  869. hardware register; <code>&quot;cc&quot;</code> serves to name this register.
  870. On other machines, condition code handling is different,
  871. and specifying <code>&quot;cc&quot;</code> has no effect. But
  872. it is valid no matter what the target.
  873. </p>
  874. </dd>
  875. <dt><code>&quot;memory&quot;</code></dt>
  876. <dd><p>The <code>&quot;memory&quot;</code> clobber tells the compiler that the assembly code
  877. performs memory
  878. reads or writes to items other than those listed in the input and output
  879. operands (for example, accessing the memory pointed to by one of the input
  880. parameters). To ensure memory contains correct values, GCC may need to flush
  881. specific register values to memory before executing the <code>asm</code>. Further,
  882. the compiler does not assume that any values read from memory before an
  883. <code>asm</code> remain unchanged after that <code>asm</code>; it reloads them as
  884. needed.
  885. Using the <code>&quot;memory&quot;</code> clobber effectively forms a read/write
  886. memory barrier for the compiler.
  887. </p>
  888. <p>Note that this clobber does not prevent the <em>processor</em> from doing
  889. speculative reads past the <code>asm</code> statement. To prevent that, you need
  890. processor-specific fence instructions.
  891. </p>
  892. </dd>
  893. </dl>
  894. <p>Flushing registers to memory has performance implications and may be
  895. an issue for time-sensitive code. You can provide better information
  896. to GCC to avoid this, as shown in the following examples. At a
  897. minimum, aliasing rules allow GCC to know what memory <em>doesn&rsquo;t</em>
  898. need to be flushed.
  899. </p>
  900. <p>Here is a fictitious sum of squares instruction, that takes two
  901. pointers to floating point values in memory and produces a floating
  902. point register output.
  903. Notice that <code>x</code>, and <code>y</code> both appear twice in the <code>asm</code>
  904. parameters, once to specify memory accessed, and once to specify a
  905. base register used by the <code>asm</code>. You won&rsquo;t normally be wasting a
  906. register by doing this as GCC can use the same register for both
  907. purposes. However, it would be foolish to use both <code>%1</code> and
  908. <code>%3</code> for <code>x</code> in this <code>asm</code> and expect them to be the
  909. same. In fact, <code>%3</code> may well not be a register. It might be a
  910. symbolic memory reference to the object pointed to by <code>x</code>.
  911. </p>
  912. <div class="smallexample">
  913. <pre class="smallexample">asm (&quot;sumsq %0, %1, %2&quot;
  914. : &quot;+f&quot; (result)
  915. : &quot;r&quot; (x), &quot;r&quot; (y), &quot;m&quot; (*x), &quot;m&quot; (*y));
  916. </pre></div>
  917. <p>Here is a fictitious <code>*z++ = *x++ * *y++</code> instruction.
  918. Notice that the <code>x</code>, <code>y</code> and <code>z</code> pointer registers
  919. must be specified as input/output because the <code>asm</code> modifies
  920. them.
  921. </p>
  922. <div class="smallexample">
  923. <pre class="smallexample">asm (&quot;vecmul %0, %1, %2&quot;
  924. : &quot;+r&quot; (z), &quot;+r&quot; (x), &quot;+r&quot; (y), &quot;=m&quot; (*z)
  925. : &quot;m&quot; (*x), &quot;m&quot; (*y));
  926. </pre></div>
  927. <p>An x86 example where the string memory argument is of unknown length.
  928. </p>
  929. <div class="smallexample">
  930. <pre class="smallexample">asm(&quot;repne scasb&quot;
  931. : &quot;=c&quot; (count), &quot;+D&quot; (p)
  932. : &quot;m&quot; (*(const char (*)[]) p), &quot;0&quot; (-1), &quot;a&quot; (0));
  933. </pre></div>
  934. <p>If you know the above will only be reading a ten byte array then you
  935. could instead use a memory input like:
  936. <code>&quot;m&quot; (*(const char (*)[10]) p)</code>.
  937. </p>
  938. <p>Here is an example of a PowerPC vector scale implemented in assembly,
  939. complete with vector and condition code clobbers, and some initialized
  940. offset registers that are unchanged by the <code>asm</code>.
  941. </p>
  942. <div class="smallexample">
  943. <pre class="smallexample">void
  944. dscal (size_t n, double *x, double alpha)
  945. {
  946. asm (&quot;/* lots of asm here */&quot;
  947. : &quot;+m&quot; (*(double (*)[n]) x), &quot;+&amp;r&quot; (n), &quot;+b&quot; (x)
  948. : &quot;d&quot; (alpha), &quot;b&quot; (32), &quot;b&quot; (48), &quot;b&quot; (64),
  949. &quot;b&quot; (80), &quot;b&quot; (96), &quot;b&quot; (112)
  950. : &quot;cr0&quot;,
  951. &quot;vs32&quot;,&quot;vs33&quot;,&quot;vs34&quot;,&quot;vs35&quot;,&quot;vs36&quot;,&quot;vs37&quot;,&quot;vs38&quot;,&quot;vs39&quot;,
  952. &quot;vs40&quot;,&quot;vs41&quot;,&quot;vs42&quot;,&quot;vs43&quot;,&quot;vs44&quot;,&quot;vs45&quot;,&quot;vs46&quot;,&quot;vs47&quot;);
  953. }
  954. </pre></div>
  955. <p>Rather than allocating fixed registers via clobbers to provide scratch
  956. registers for an <code>asm</code> statement, an alternative is to define a
  957. variable and make it an early-clobber output as with <code>a2</code> and
  958. <code>a3</code> in the example below. This gives the compiler register
  959. allocator more freedom. You can also define a variable and make it an
  960. output tied to an input as with <code>a0</code> and <code>a1</code>, tied
  961. respectively to <code>ap</code> and <code>lda</code>. Of course, with tied
  962. outputs your <code>asm</code> can&rsquo;t use the input value after modifying the
  963. output register since they are one and the same register. What&rsquo;s
  964. more, if you omit the early-clobber on the output, it is possible that
  965. GCC might allocate the same register to another of the inputs if GCC
  966. could prove they had the same value on entry to the <code>asm</code>. This
  967. is why <code>a1</code> has an early-clobber. Its tied input, <code>lda</code>
  968. might conceivably be known to have the value 16 and without an
  969. early-clobber share the same register as <code>%11</code>. On the other
  970. hand, <code>ap</code> can&rsquo;t be the same as any of the other inputs, so an
  971. early-clobber on <code>a0</code> is not needed. It is also not desirable in
  972. this case. An early-clobber on <code>a0</code> would cause GCC to allocate
  973. a separate register for the <code>&quot;m&quot; (*(const double (*)[]) ap)</code>
  974. input. Note that tying an input to an output is the way to set up an
  975. initialized temporary register modified by an <code>asm</code> statement.
  976. An input not tied to an output is assumed by GCC to be unchanged, for
  977. example <code>&quot;b&quot; (16)</code> below sets up <code>%11</code> to 16, and GCC might
  978. use that register in following code if the value 16 happened to be
  979. needed. You can even use a normal <code>asm</code> output for a scratch if
  980. all inputs that might share the same register are consumed before the
  981. scratch is used. The VSX registers clobbered by the <code>asm</code>
  982. statement could have used this technique except for GCC&rsquo;s limit on the
  983. number of <code>asm</code> parameters.
  984. </p>
  985. <div class="smallexample">
  986. <pre class="smallexample">static void
  987. dgemv_kernel_4x4 (long n, const double *ap, long lda,
  988. const double *x, double *y, double alpha)
  989. {
  990. double *a0;
  991. double *a1;
  992. double *a2;
  993. double *a3;
  994. __asm__
  995. (
  996. /* lots of asm here */
  997. &quot;#n=%1 ap=%8=%12 lda=%13 x=%7=%10 y=%0=%2 alpha=%9 o16=%11\n&quot;
  998. &quot;#a0=%3 a1=%4 a2=%5 a3=%6&quot;
  999. :
  1000. &quot;+m&quot; (*(double (*)[n]) y),
  1001. &quot;+&amp;r&quot; (n), // 1
  1002. &quot;+b&quot; (y), // 2
  1003. &quot;=b&quot; (a0), // 3
  1004. &quot;=&amp;b&quot; (a1), // 4
  1005. &quot;=&amp;b&quot; (a2), // 5
  1006. &quot;=&amp;b&quot; (a3) // 6
  1007. :
  1008. &quot;m&quot; (*(const double (*)[n]) x),
  1009. &quot;m&quot; (*(const double (*)[]) ap),
  1010. &quot;d&quot; (alpha), // 9
  1011. &quot;r&quot; (x), // 10
  1012. &quot;b&quot; (16), // 11
  1013. &quot;3&quot; (ap), // 12
  1014. &quot;4&quot; (lda) // 13
  1015. :
  1016. &quot;cr0&quot;,
  1017. &quot;vs32&quot;,&quot;vs33&quot;,&quot;vs34&quot;,&quot;vs35&quot;,&quot;vs36&quot;,&quot;vs37&quot;,
  1018. &quot;vs40&quot;,&quot;vs41&quot;,&quot;vs42&quot;,&quot;vs43&quot;,&quot;vs44&quot;,&quot;vs45&quot;,&quot;vs46&quot;,&quot;vs47&quot;
  1019. );
  1020. }
  1021. </pre></div>
  1022. <a name="GotoLabels"></a><a name="Goto-Labels"></a>
  1023. <h4 class="subsubsection">6.47.2.7 Goto Labels</h4>
  1024. <a name="index-asm-goto-labels"></a>
  1025. <p><code>asm goto</code> allows assembly code to jump to one or more C labels. The
  1026. <var>GotoLabels</var> section in an <code>asm goto</code> statement contains
  1027. a comma-separated
  1028. list of all C labels to which the assembler code may jump. GCC assumes that
  1029. <code>asm</code> execution falls through to the next statement (if this is not the
  1030. case, consider using the <code>__builtin_unreachable</code> intrinsic after the
  1031. <code>asm</code> statement). Optimization of <code>asm goto</code> may be improved by
  1032. using the <code>hot</code> and <code>cold</code> label attributes (see <a href="Label-Attributes.html#Label-Attributes">Label Attributes</a>).
  1033. </p>
  1034. <p>An <code>asm goto</code> statement cannot have outputs.
  1035. This is due to an internal restriction of
  1036. the compiler: control transfer instructions cannot have outputs.
  1037. If the assembler code does modify anything, use the <code>&quot;memory&quot;</code> clobber
  1038. to force the
  1039. optimizers to flush all register values to memory and reload them if
  1040. necessary after the <code>asm</code> statement.
  1041. </p>
  1042. <p>Also note that an <code>asm goto</code> statement is always implicitly
  1043. considered volatile.
  1044. </p>
  1045. <p>To reference a label in the assembler template,
  1046. prefix it with &lsquo;<samp>%l</samp>&rsquo; (lowercase &lsquo;<samp>L</samp>&rsquo;) followed
  1047. by its (zero-based) position in <var>GotoLabels</var> plus the number of input
  1048. operands. For example, if the <code>asm</code> has three inputs and references two
  1049. labels, refer to the first label as &lsquo;<samp>%l3</samp>&rsquo; and the second as &lsquo;<samp>%l4</samp>&rsquo;).
  1050. </p>
  1051. <p>Alternately, you can reference labels using the actual C label name enclosed
  1052. in brackets. For example, to reference a label named <code>carry</code>, you can
  1053. use &lsquo;<samp>%l[carry]</samp>&rsquo;. The label must still be listed in the <var>GotoLabels</var>
  1054. section when using this approach.
  1055. </p>
  1056. <p>Here is an example of <code>asm goto</code> for i386:
  1057. </p>
  1058. <div class="example">
  1059. <pre class="example">asm goto (
  1060. &quot;btl %1, %0\n\t&quot;
  1061. &quot;jc %l2&quot;
  1062. : /* No outputs. */
  1063. : &quot;r&quot; (p1), &quot;r&quot; (p2)
  1064. : &quot;cc&quot;
  1065. : carry);
  1066. return 0;
  1067. carry:
  1068. return 1;
  1069. </pre></div>
  1070. <p>The following example shows an <code>asm goto</code> that uses a memory clobber.
  1071. </p>
  1072. <div class="example">
  1073. <pre class="example">int frob(int x)
  1074. {
  1075. int y;
  1076. asm goto (&quot;frob %%r5, %1; jc %l[error]; mov (%2), %%r5&quot;
  1077. : /* No outputs. */
  1078. : &quot;r&quot;(x), &quot;r&quot;(&amp;y)
  1079. : &quot;r5&quot;, &quot;memory&quot;
  1080. : error);
  1081. return y;
  1082. error:
  1083. return -1;
  1084. }
  1085. </pre></div>
  1086. <a name="x86Operandmodifiers"></a><a name="x86-Operand-Modifiers"></a>
  1087. <h4 class="subsubsection">6.47.2.8 x86 Operand Modifiers</h4>
  1088. <p>References to input, output, and goto operands in the assembler template
  1089. of extended <code>asm</code> statements can use
  1090. modifiers to affect the way the operands are formatted in
  1091. the code output to the assembler. For example, the
  1092. following code uses the &lsquo;<samp>h</samp>&rsquo; and &lsquo;<samp>b</samp>&rsquo; modifiers for x86:
  1093. </p>
  1094. <div class="example">
  1095. <pre class="example">uint16_t num;
  1096. asm volatile (&quot;xchg %h0, %b0&quot; : &quot;+a&quot; (num) );
  1097. </pre></div>
  1098. <p>These modifiers generate this assembler code:
  1099. </p>
  1100. <div class="example">
  1101. <pre class="example">xchg %ah, %al
  1102. </pre></div>
  1103. <p>The rest of this discussion uses the following code for illustrative purposes.
  1104. </p>
  1105. <div class="example">
  1106. <pre class="example">int main()
  1107. {
  1108. int iInt = 1;
  1109. top:
  1110. asm volatile goto (&quot;some assembler instructions here&quot;
  1111. : /* No outputs. */
  1112. : &quot;q&quot; (iInt), &quot;X&quot; (sizeof(unsigned char) + 1), &quot;i&quot; (42)
  1113. : /* No clobbers. */
  1114. : top);
  1115. }
  1116. </pre></div>
  1117. <p>With no modifiers, this is what the output from the operands would be
  1118. for the &lsquo;<samp>att</samp>&rsquo; and &lsquo;<samp>intel</samp>&rsquo; dialects of assembler:
  1119. </p>
  1120. <table>
  1121. <thead><tr><th>Operand</th><th>&lsquo;<samp>att</samp>&rsquo;</th><th>&lsquo;<samp>intel</samp>&rsquo;</th></tr></thead>
  1122. <tr><td><code>%0</code></td><td><code>%eax</code></td><td><code>eax</code></td></tr>
  1123. <tr><td><code>%1</code></td><td><code>$2</code></td><td><code>2</code></td></tr>
  1124. <tr><td><code>%3</code></td><td><code>$.L3</code></td><td><code>OFFSET FLAT:.L3</code></td></tr>
  1125. </table>
  1126. <p>The table below shows the list of supported modifiers and their effects.
  1127. </p>
  1128. <table>
  1129. <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>
  1130. <tr><td><code>A</code></td><td>Print an absolute memory reference.</td><td><code>%A0</code></td><td><code>*%rax</code></td><td><code>rax</code></td></tr>
  1131. <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>
  1132. <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>
  1133. <tr><td><code>E</code></td><td>Print the address in Double Integer (DImode) mode (8 bytes) when the target is 64-bit.
  1134. Otherwise mode is unspecified (VOIDmode).</td><td><code>%E1</code></td><td><code>%(rax)</code></td><td><code>[rax]</code></td></tr>
  1135. <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>
  1136. <tr><td><code>H</code></td><td>Add 8 bytes to an offsettable memory reference. Useful when accessing the
  1137. high 8 bytes of SSE values. For a memref in (%rax), it generates</td><td><code>%H0</code></td><td><code>8(%rax)</code></td><td><code>8[rax]</code></td></tr>
  1138. <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>
  1139. <tr><td><code>l</code></td><td>Print the label name with no punctuation.</td><td><code>%l3</code></td><td><code>.L3</code></td><td><code>.L3</code></td></tr>
  1140. <tr><td><code>p</code></td><td>Print raw symbol name (without syntax-specific prefixes).</td><td><code>%p2</code></td><td><code>42</code></td><td><code>42</code></td></tr>
  1141. <tr><td><code>P</code></td><td>If used for a function, print the PLT suffix and generate PIC code.
  1142. For example, emit <code>foo@PLT</code> instead of &rsquo;foo&rsquo; for the function
  1143. foo(). If used for a constant, drop all syntax-specific prefixes and
  1144. issue the bare constant. See <code>p</code> above.</td></tr>
  1145. <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>
  1146. <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>
  1147. <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>
  1148. </table>
  1149. <p><code>V</code> is a special modifier which prints the name of the full integer
  1150. register without <code>%</code>.
  1151. </p>
  1152. <a name="x86floatingpointasmoperands"></a><a name="x86-Floating_002dPoint-asm-Operands"></a>
  1153. <h4 class="subsubsection">6.47.2.9 x86 Floating-Point <code>asm</code> Operands</h4>
  1154. <p>On x86 targets, there are several rules on the usage of stack-like registers
  1155. in the operands of an <code>asm</code>. These rules apply only to the operands
  1156. that are stack-like registers:
  1157. </p>
  1158. <ol>
  1159. <li> Given a set of input registers that die in an <code>asm</code>, it is
  1160. necessary to know which are implicitly popped by the <code>asm</code>, and
  1161. which must be explicitly popped by GCC.
  1162. <p>An input register that is implicitly popped by the <code>asm</code> must be
  1163. explicitly clobbered, unless it is constrained to match an
  1164. output operand.
  1165. </p>
  1166. </li><li> For any input register that is implicitly popped by an <code>asm</code>, it is
  1167. necessary to know how to adjust the stack to compensate for the pop.
  1168. If any non-popped input is closer to the top of the reg-stack than
  1169. the implicitly popped register, it would not be possible to know what the
  1170. stack looked like&mdash;it&rsquo;s not clear how the rest of the stack &ldquo;slides
  1171. up&rdquo;.
  1172. <p>All implicitly popped input registers must be closer to the top of
  1173. the reg-stack than any input that is not implicitly popped.
  1174. </p>
  1175. <p>It is possible that if an input dies in an <code>asm</code>, the compiler might
  1176. use the input register for an output reload. Consider this example:
  1177. </p>
  1178. <div class="smallexample">
  1179. <pre class="smallexample">asm (&quot;foo&quot; : &quot;=t&quot; (a) : &quot;f&quot; (b));
  1180. </pre></div>
  1181. <p>This code says that input <code>b</code> is not popped by the <code>asm</code>, and that
  1182. the <code>asm</code> pushes a result onto the reg-stack, i.e., the stack is one
  1183. deeper after the <code>asm</code> than it was before. But, it is possible that
  1184. reload may think that it can use the same register for both the input and
  1185. the output.
  1186. </p>
  1187. <p>To prevent this from happening,
  1188. if any input operand uses the &lsquo;<samp>f</samp>&rsquo; constraint, all output register
  1189. constraints must use the &lsquo;<samp>&amp;</samp>&rsquo; early-clobber modifier.
  1190. </p>
  1191. <p>The example above is correctly written as:
  1192. </p>
  1193. <div class="smallexample">
  1194. <pre class="smallexample">asm (&quot;foo&quot; : &quot;=&amp;t&quot; (a) : &quot;f&quot; (b));
  1195. </pre></div>
  1196. </li><li> Some operands need to be in particular places on the stack. All
  1197. output operands fall in this category&mdash;GCC has no other way to
  1198. know which registers the outputs appear in unless you indicate
  1199. this in the constraints.
  1200. <p>Output operands must specifically indicate which register an output
  1201. appears in after an <code>asm</code>. &lsquo;<samp>=f</samp>&rsquo; is not allowed: the operand
  1202. constraints must select a class with a single register.
  1203. </p>
  1204. </li><li> Output operands may not be &ldquo;inserted&rdquo; between existing stack registers.
  1205. Since no 387 opcode uses a read/write operand, all output operands
  1206. are dead before the <code>asm</code>, and are pushed by the <code>asm</code>.
  1207. It makes no sense to push anywhere but the top of the reg-stack.
  1208. <p>Output operands must start at the top of the reg-stack: output
  1209. operands may not &ldquo;skip&rdquo; a register.
  1210. </p>
  1211. </li><li> Some <code>asm</code> statements may need extra stack space for internal
  1212. calculations. This can be guaranteed by clobbering stack registers
  1213. unrelated to the inputs and outputs.
  1214. </li></ol>
  1215. <p>This <code>asm</code>
  1216. takes one input, which is internally popped, and produces two outputs.
  1217. </p>
  1218. <div class="smallexample">
  1219. <pre class="smallexample">asm (&quot;fsincos&quot; : &quot;=t&quot; (cos), &quot;=u&quot; (sin) : &quot;0&quot; (inp));
  1220. </pre></div>
  1221. <p>This <code>asm</code> takes two inputs, which are popped by the <code>fyl2xp1</code> opcode,
  1222. and replaces them with one output. The <code>st(1)</code> clobber is necessary
  1223. for the compiler to know that <code>fyl2xp1</code> pops both inputs.
  1224. </p>
  1225. <div class="smallexample">
  1226. <pre class="smallexample">asm (&quot;fyl2xp1&quot; : &quot;=t&quot; (result) : &quot;0&quot; (x), &quot;u&quot; (y) : &quot;st(1)&quot;);
  1227. </pre></div>
  1228. <hr>
  1229. <div class="header">
  1230. <p>
  1231. 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>
  1232. </div>
  1233. </body>
  1234. </html>