5.9.4 .macro symbol arg1[=default] [, ..., argn [=default] ] ... .endm
Define macros that generate assembly output. A macro accepts optional arguments and can call other macros or even itself recursively.
If a macro definition requires arguments, specify their names after the
macro name, separated by commas or spaces. To refer to arguments within the macro block,
use \arg
or
&arg&
. The second form can be used to combine
an argument with additional characters to create a symbol name.
For example, assembling:
.macro display_int sym
mov \sym,w0
rcall display
.endm
display_int result
is equivalent to assembling:
mov result,w0
rcall display
In the next example, a macro is used to define HI- and LO-word constants for a 32-bit integer.
.macro LCONST name,value
.equ \name,\value
.equ &name&LO,(\value) & 0xFFFF
.equ &name&HI,((\value)>>16) & 0xFFFF
.endm
LCONST seconds_per_day 60*60*24
mov #seconds_per_dayLO,w0
mov #seconds_per_dayHI,w1
xc-dsc-as
maintains a
counter of how many macros have been executed in the psuedo-variable \@
.
This value can be copied to the assembly output, but only within a macro definition. In the
following example, a recursive macro is used to allocate an arbitrary number of labeled
buffers.
.macro make_buffers num,size
BUF\@: .space \size
.if (\num - 1)
make_buffers (\num - 1),\size
.endif
.endm
.bss
make_buffers 4,16 ; create BUF0..BUF3, 16 bytes each