a
a
asm80
Search…
Macros
Define macro with .macro and .endm. You can use a parametric macros - any parameter is addressable by %%1, %%2, %%3, ... For example, such code:
1
.macro decadd
2
adi %%1
3
daa
4
.endm
5
; Use this macro
6
decadd $22
Copied!
will generate this:
1
0000 ; Use this macro
2
**MACRO UNROLL - DECADD
3
0000 87 22 ADI $22
4
0001 27 DAA
Copied!

Local labels

Macros has no local label mechanism. So if you define a label in a macro, it unrolls to the same label, ending with a "redefine label" error message.
There is a workaround to define unique label for each macro unrolling:
1
.macro xyz
2
loop%%M: inc a
3
dec b
4
jr nz,loop%%M
5
.endm
Copied!
Now it is safe to use this macro repeatedly, because special placeholder "%%M" is replaced by string "M_"+line number. It provides a good enough workaround for local labels.

Compound parameters

Let's imagine a macro:
1
.macro test
2
db %%1
3
dw %%2
4
.endm
Copied!
If you use this macro in such form, everything is OK:
1
test $12, $3456
Copied!
Two parameters is OK. But what if you need the first DB is something like db $de,$ad,$be,$ef? You can use the compound parameter:
1
test {$de,$ad,$be,$ef}, $3456
Copied!

Rich syntax (v2.5.2+)

A macro can be defined by standard syntax as .macro name, or more consistent way as name: .macro or name .macro.

Formal parameters (v2.5.2+)

Now you can name formal parameters too. For example - assume a macro cpymem for copy memory content. Such macro has three arguments - source, destination and length. The old form of macro has mute parameters, just referenced by its number:
1
.macro cpymem
2
LD HL,%%1
3
LD DE,%%2
4
LD BC,%%3
5
LDIR
6
.endm
Copied!
New formal parameters, introduced in revision 2.5.2, allows to write this:
1
.macro cpymem, src, dst, len
2
LD HL,src
3
LD DE,dst
4
LD BC,len
5
LDIR
6
.endm
Copied!
Preprocessor also check if the number of given parameters is sufficient for the macro, i.e. you cannot specify less parameters than formal, like cpymem 100, 200.
All those named parameters are strictly local (in fact, they are replaced with its values before assembly phase).
Last modified 1yr ago