Macro.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- This file documents the GNU Assembler "as".
  4. Copyright (C) 1991-2017 Free Software Foundation, Inc.
  5. Permission is granted to copy, distribute and/or modify this document
  6. under the terms of the GNU Free Documentation License, Version 1.3
  7. or any later version published by the Free Software Foundation;
  8. with no Invariant Sections, with no Front-Cover Texts, and with no
  9. Back-Cover Texts. A copy of the license is included in the
  10. section entitled "GNU Free Documentation License".
  11. -->
  12. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  13. <head>
  14. <title>Using as: Macro</title>
  15. <meta name="description" content="Using as: Macro">
  16. <meta name="keywords" content="Using as: Macro">
  17. <meta name="resource-type" content="document">
  18. <meta name="distribution" content="global">
  19. <meta name="Generator" content="makeinfo">
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  21. <link href="index.html#Top" rel="start" title="Top">
  22. <link href="AS-Index.html#AS-Index" rel="index" title="AS Index">
  23. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  24. <link href="Pseudo-Ops.html#Pseudo-Ops" rel="up" title="Pseudo Ops">
  25. <link href="MRI.html#MRI" rel="next" title="MRI">
  26. <link href="Long.html#Long" rel="prev" title="Long">
  27. <style type="text/css">
  28. <!--
  29. a.summary-letter {text-decoration: none}
  30. blockquote.smallquotation {font-size: smaller}
  31. div.display {margin-left: 3.2em}
  32. div.example {margin-left: 3.2em}
  33. div.indentedblock {margin-left: 3.2em}
  34. div.lisp {margin-left: 3.2em}
  35. div.smalldisplay {margin-left: 3.2em}
  36. div.smallexample {margin-left: 3.2em}
  37. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  38. div.smalllisp {margin-left: 3.2em}
  39. kbd {font-style:oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. pre.smalldisplay {font-family: inherit; font-size: smaller}
  45. pre.smallexample {font-size: smaller}
  46. pre.smallformat {font-family: inherit; font-size: smaller}
  47. pre.smalllisp {font-size: smaller}
  48. span.nocodebreak {white-space:nowrap}
  49. span.nolinebreak {white-space:nowrap}
  50. span.roman {font-family:serif; font-weight:normal}
  51. span.sansserif {font-family:sans-serif; font-weight:normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  57. <a name="Macro"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="MRI.html#MRI" accesskey="n" rel="next">MRI</a>, Previous: <a href="Long.html#Long" accesskey="p" rel="prev">Long</a>, Up: <a href="Pseudo-Ops.html#Pseudo-Ops" accesskey="u" rel="up">Pseudo Ops</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
  61. </div>
  62. <hr>
  63. <a name="g_t_002emacro"></a>
  64. <h3 class="section">7.58 <code>.macro</code></h3>
  65. <a name="index-macros"></a>
  66. <p>The commands <code>.macro</code> and <code>.endm</code> allow you to define macros that
  67. generate assembly output. For example, this definition specifies a macro
  68. <code>sum</code> that puts a sequence of numbers into memory:
  69. </p>
  70. <div class="example">
  71. <pre class="example"> .macro sum from=0, to=5
  72. .long \from
  73. .if \to-\from
  74. sum &quot;(\from+1)&quot;,\to
  75. .endif
  76. .endm
  77. </pre></div>
  78. <p>With that definition, &lsquo;<samp>SUM 0,5</samp>&rsquo; is equivalent to this assembly input:
  79. </p>
  80. <div class="example">
  81. <pre class="example"> .long 0
  82. .long 1
  83. .long 2
  84. .long 3
  85. .long 4
  86. .long 5
  87. </pre></div>
  88. <dl compact="compact">
  89. <dt><code>.macro <var>macname</var></code>
  90. <a name="index-_002emacro-macname"></a>
  91. </dt>
  92. <dt><code>.macro <var>macname</var> <var>macargs</var> &hellip;</code>
  93. <a name="index-_002emacro-macname-macargs-_2026"></a>
  94. </dt>
  95. <dd><a name="index-macro-directive"></a>
  96. <p>Begin the definition of a macro called <var>macname</var>. If your macro
  97. definition requires arguments, specify their names after the macro name,
  98. separated by commas or spaces. You can qualify the macro argument to
  99. indicate whether all invocations must specify a non-blank value (through
  100. &lsquo;<samp>:<code>req</code></samp>&rsquo;), or whether it takes all of the remaining arguments
  101. (through &lsquo;<samp>:<code>vararg</code></samp>&rsquo;). You can supply a default value for any
  102. macro argument by following the name with &lsquo;<samp>=<var>deflt</var></samp>&rsquo;. You
  103. cannot define two macros with the same <var>macname</var> unless it has been
  104. subject to the <code>.purgem</code> directive (see <a href="Purgem.html#Purgem">Purgem</a>) between the two
  105. definitions. For example, these are all valid <code>.macro</code> statements:
  106. </p>
  107. <dl compact="compact">
  108. <dt><code>.macro comm</code></dt>
  109. <dd><p>Begin the definition of a macro called <code>comm</code>, which takes no
  110. arguments.
  111. </p>
  112. </dd>
  113. <dt><code>.macro plus1 p, p1</code></dt>
  114. <dt><code>.macro plus1 p p1</code></dt>
  115. <dd><p>Either statement begins the definition of a macro called <code>plus1</code>,
  116. which takes two arguments; within the macro definition, write
  117. &lsquo;<samp>\p</samp>&rsquo; or &lsquo;<samp>\p1</samp>&rsquo; to evaluate the arguments.
  118. </p>
  119. </dd>
  120. <dt><code>.macro reserve_str p1=0 p2</code></dt>
  121. <dd><p>Begin the definition of a macro called <code>reserve_str</code>, with two
  122. arguments. The first argument has a default value, but not the second.
  123. After the definition is complete, you can call the macro either as
  124. &lsquo;<samp>reserve_str <var>a</var>,<var>b</var></samp>&rsquo; (with &lsquo;<samp>\p1</samp>&rsquo; evaluating to
  125. <var>a</var> and &lsquo;<samp>\p2</samp>&rsquo; evaluating to <var>b</var>), or as &lsquo;<samp>reserve_str
  126. ,<var>b</var></samp>&rsquo; (with &lsquo;<samp>\p1</samp>&rsquo; evaluating as the default, in this case
  127. &lsquo;<samp>0</samp>&rsquo;, and &lsquo;<samp>\p2</samp>&rsquo; evaluating to <var>b</var>).
  128. </p>
  129. </dd>
  130. <dt><code>.macro m p1:req, p2=0, p3:vararg</code></dt>
  131. <dd><p>Begin the definition of a macro called <code>m</code>, with at least three
  132. arguments. The first argument must always have a value specified, but
  133. not the second, which instead has a default value. The third formal
  134. will get assigned all remaining arguments specified at invocation time.
  135. </p>
  136. <p>When you call a macro, you can specify the argument values either by
  137. position, or by keyword. For example, &lsquo;<samp>sum 9,17</samp>&rsquo; is equivalent to
  138. &lsquo;<samp>sum to=17, from=9</samp>&rsquo;.
  139. </p>
  140. </dd>
  141. </dl>
  142. <p>Note that since each of the <var>macargs</var> can be an identifier exactly
  143. as any other one permitted by the target architecture, there may be
  144. occasional problems if the target hand-crafts special meanings to certain
  145. characters when they occur in a special position. For example, if the colon
  146. (<code>:</code>) is generally permitted to be part of a symbol name, but the
  147. architecture specific code special-cases it when occurring as the final
  148. character of a symbol (to denote a label), then the macro parameter
  149. replacement code will have no way of knowing that and consider the whole
  150. construct (including the colon) an identifier, and check only this
  151. identifier for being the subject to parameter substitution. So for example
  152. this macro definition:
  153. </p>
  154. <div class="example">
  155. <pre class="example"> .macro label l
  156. \l:
  157. .endm
  158. </pre></div>
  159. <p>might not work as expected. Invoking &lsquo;<samp>label foo</samp>&rsquo; might not create a label
  160. called &lsquo;<samp>foo</samp>&rsquo; but instead just insert the text &lsquo;<samp>\l:</samp>&rsquo; into the
  161. assembler source, probably generating an error about an unrecognised
  162. identifier.
  163. </p>
  164. <p>Similarly problems might occur with the period character (&lsquo;<samp>.</samp>&rsquo;)
  165. which is often allowed inside opcode names (and hence identifier names). So
  166. for example constructing a macro to build an opcode from a base name and a
  167. length specifier like this:
  168. </p>
  169. <div class="example">
  170. <pre class="example"> .macro opcode base length
  171. \base.\length
  172. .endm
  173. </pre></div>
  174. <p>and invoking it as &lsquo;<samp>opcode store l</samp>&rsquo; will not create a &lsquo;<samp>store.l</samp>&rsquo;
  175. instruction but instead generate some kind of error as the assembler tries to
  176. interpret the text &lsquo;<samp>\base.\length</samp>&rsquo;.
  177. </p>
  178. <p>There are several possible ways around this problem:
  179. </p>
  180. <dl compact="compact">
  181. <dt><code>Insert white space</code></dt>
  182. <dd><p>If it is possible to use white space characters then this is the simplest
  183. solution. eg:
  184. </p>
  185. <div class="example">
  186. <pre class="example"> .macro label l
  187. \l :
  188. .endm
  189. </pre></div>
  190. </dd>
  191. <dt><code>Use &lsquo;<samp>\()</samp>&rsquo;</code></dt>
  192. <dd><p>The string &lsquo;<samp>\()</samp>&rsquo; can be used to separate the end of a macro argument from
  193. the following text. eg:
  194. </p>
  195. <div class="example">
  196. <pre class="example"> .macro opcode base length
  197. \base\().\length
  198. .endm
  199. </pre></div>
  200. </dd>
  201. <dt><code>Use the alternate macro syntax mode</code></dt>
  202. <dd><p>In the alternative macro syntax mode the ampersand character (&lsquo;<samp>&amp;</samp>&rsquo;) can be
  203. used as a separator. eg:
  204. </p>
  205. <div class="example">
  206. <pre class="example"> .altmacro
  207. .macro label l
  208. l&amp;:
  209. .endm
  210. </pre></div>
  211. </dd>
  212. </dl>
  213. <p>Note: this problem of correctly identifying string parameters to pseudo ops
  214. also applies to the identifiers used in <code>.irp</code> (see <a href="Irp.html#Irp">Irp</a>)
  215. and <code>.irpc</code> (see <a href="Irpc.html#Irpc">Irpc</a>) as well.
  216. </p>
  217. </dd>
  218. <dt><code>.endm</code>
  219. <a name="index-_002eendm"></a>
  220. </dt>
  221. <dd><a name="index-endm-directive"></a>
  222. <p>Mark the end of a macro definition.
  223. </p>
  224. </dd>
  225. <dt><code>.exitm</code>
  226. <a name="index-_002eexitm"></a>
  227. </dt>
  228. <dd><a name="index-exitm-directive"></a>
  229. <p>Exit early from the current macro definition.
  230. </p>
  231. <a name="index-number-of-macros-executed"></a>
  232. <a name="index-macros_002c-count-executed"></a>
  233. </dd>
  234. <dt><code>\@</code>
  235. <a name="index-_005c_0040"></a>
  236. </dt>
  237. <dd><p><code>as</code> maintains a counter of how many macros it has
  238. executed in this pseudo-variable; you can copy that number to your
  239. output with &lsquo;<samp>\@</samp>&rsquo;, but <em>only within a macro definition</em>.
  240. </p>
  241. </dd>
  242. <dt><code>LOCAL <var>name</var> [ , &hellip; ]</code>
  243. <a name="index-LOCAL-name-_005b-_002c-_2026-_005d-1"></a>
  244. </dt>
  245. <dd><p><em>Warning: <code>LOCAL</code> is only available if you select &ldquo;alternate
  246. macro syntax&rdquo; with &lsquo;<samp>--alternate</samp>&rsquo; or <code>.altmacro</code>.</em>
  247. See <a href="Altmacro.html#Altmacro"><code>.altmacro</code></a>.
  248. </p></dd>
  249. </dl>
  250. <hr>
  251. <div class="header">
  252. <p>
  253. Next: <a href="MRI.html#MRI" accesskey="n" rel="next">MRI</a>, Previous: <a href="Long.html#Long" accesskey="p" rel="prev">Long</a>, Up: <a href="Pseudo-Ops.html#Pseudo-Ops" accesskey="u" rel="up">Pseudo Ops</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="AS-Index.html#AS-Index" title="Index" rel="index">Index</a>]</p>
  254. </div>
  255. </body>
  256. </html>