III.11.8 Nested Macro Definitions

A macro body may also contain further macro definitions. However, these nested macro definitions aren't valid until the enclosing macro has been expanded! That means, the enclosing macro must have been called, before the nested macros can be called.


Example 1:

A macro, which can be used to define macros with arbitrary names, may look as follows:
 DEFINE MACRO MACNAME
  MACNAME MACRO
	DB	'I am the macro &MACNAME.'
  ENDM
 ENDM
In order not to overload the example with “knowhow”, the nested macro only introduces itself kindly with a suitable character string in ROM. The call
	DEFINE Obiwan
would define the macro
  Obiwan MACRO
	DB	'I am the macro Obiwan.'
  ENDM
and the call
	DEFINE Skywalker
would define the following macro:
  Skywalker MACRO
	DB	'I am the macro Skywalker.'
  ENDM

Example 2:

A macro is to insert a variable number of NOPs into the program. For this, a macro with a nested REPT block seems to be best-suited:
 REPEAT MACRO NOPS
  REPT NOPS
	NOP
  ENDM
 ENDM
The macro call
	REPEAT 4
results in something like that:
        Line  I  Addr  Code		Source

           9+ 1        N      0004	REPT 4
          10+ 1				NOP
          11+ 1				ENDM
          12+ 2  0000  00		NOP
          13+ 2  0001  00		NOP
          14+ 2  0002  00		NOP
          15+ 2  0003  00		NOP