9.7.1 Nesting Macro Definitions

Macros can define other macros or can be redefined. MASM does not process nested definitions until the outer macro has been called. Therefore, the inner macros cannot be called until the outer macro has been called. The nesting of macro definitions is limited only by memory.

shifts MACRO opname ;; Macro generates macros

opname&s MACRO operand:REQ, rotates:=<1>

IF rotates LE 2 ;; One at a time is faster

REPEAT rotate ;; for 2 or less

opname operand, 1

ENDM

ELSE ;; Using CL is faster for

mov cl, rotates ;; more than 2

opname operand, cl

ENDIF

ENDM

ENDM

; Call macro to make new macros

shifts ror ; Generates rors

shifts rol ; Generates rols

shifts shr ; Generates shrs

shifts shl ; Generates shls

shifts rcl ; Generates rcls

shifts rcr ; Generates rcrs

shifts sal ; Generates sals

shifts sar ; Generates sars

This macro generates enhanced versions of the shift and rotate instructions. The macros could be called like this:

shrs ax, 5

rols bx, 3

The macro versions handle multiple shifts by generating different code, depending on how many shifts are specified. The example above is optimized for the 8088 and 8086 processors. If you want to enhance for other processors, you can simply change the outer macro; it automatically changes all the inner macros. Code that uses the inner macros benefits from the enhancements but does not change so long as the macro interface doesn't change.