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

Macro.html 11KB

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