Overview of Macro Operators
In assembly language macro processing, special operators provide fine control over macro parameter handling, making macro definitions more flexible and powerful. These operators are processed by the assembler during the macro expansion phase and do not affect the final generated code.
Detailed Explanation of Five Special Macro Operators
1. Forced Replacement Operator <span>&</span>
Function: Explicitly identifies the position of parameter replacement, especially when parameters are adjacent to other characters or embedded in strings.
Example 1: Opcode Parameterization
; Macro Definition
JUMP MACRO COND, LAB
J&COND& LAB
ENDM
; Macro Call
JUMP NZ, HERE
JUMP Z, THERE
; Macro Expansion Result
; JNZ HERE
; JZ THERE
Example 2: Label and String Construction
; Macro Definition
MSGGEN MACRO LAB, NUM, XYZ
LAB&NUM DB 'HELLO MR.&XYZ', 0DH, 0AH, '$'
ENDM
; Macro Call
MSGGEN MSG, 1, TAYLOR
; Macro Expansion Result
; MSG1 DB 'HELLO MR.TAYLOR', 0DH, 0AH, '$'
2. String Literal Passing Operator <span><></span>
Function: Protects string content from being interpreted by the macro processor.
Example 3: Protecting Special Characters
; Macro Definition
SHOW MACRO TEXT
DB <TEXT>, 0
ENDM
; Macro Call
SHOW <Use & symbol normally>
; Macro Expansion Result
; DB 'Use & symbol normally', 0
; (Does not interpret & as an operator)
3. Literal Character Operator <span>!</span>
Function: Treats special characters as ordinary characters.
Example 4: Outputting Special Symbols
; Macro Definition
SYMBOL MACRO CHAR
DB 'This is !&CHAR', 0
ENDM
; Macro Call
SYMBOL &
; Macro Expansion Result
; DB 'This is &', 0
; (Displays & symbol normally)
4. Expression Operator <span>%</span>
Function: Forces expression evaluation and uses the result as a parameter.
Example 5: Dynamically Generating Data
; Macro Definition
ARRAY MACRO COUNT
DB %COUNT+1 DUP(?)
ENDM
; Macro Call
X = 5
ARRAY %X*2
; Macro Expansion Result
; DB 11 DUP(?)
; (Calculates 5*2=10, then 10+1=11)
5. Macro Comment <span>;;</span>
Function: Comments visible only in macro definitions, which do not appear in the expanded code.
Example 6: Hiding Implementation Details
; Macro Definition
DEBUG_PRINT MACRO MSG
;; This comment will not appear in the expanded code
; This comment will be retained
MOV AH, 09H
LEA DX, MSG
INT 21H
ENDM
; Macro Call
DEBUG_PRINT HELLO_MSG
; Macro Expansion Result
; MOV AH, 09H
; LEA DX, HELLO_MSG
; INT 21H
; (Only regular comments are retained)
Advanced Application Examples
Example 7: Comprehensive Use of Multiple Operators
; Define a complex macro
FORMAT_MSG MACRO PREFIX, NUM, TEXT
;; Generate formatted message structure
%PREFIX&_%NUM LABEL BYTE
DB 'ID: %NUM, Text: <&TEXT>', 0DH, 0AH
DB 'Length: %SIZE &PREFIX&_&NUM', 0
;; Calculate length
&PREFIX&_&NUM&_LEN EQU $ - &PREFIX&_&NUM
ENDM
; Call the macro
COUNT = 100
FORMAT_MSG MSG, %COUNT+1, <Test & demonstration>
; Expansion Result
; MSG_101 LABEL BYTE
; DB 'ID: 101, Text: <Test & demonstration>', 0DH, 0AH
; DB 'Length: 35', 0
; MSG_101_LEN EQU 35
Example 8: Conditional Code Generation
; Define a conditional jump macro
COND_JUMP MACRO COND, LAB, DISTANCE
IF (%DISTANCE LT 128) AND (%DISTANCE GT -129)
J&COND& SHORT LAB ;; Short jump
ELSE
J&COND& LAB ;; Near jump
ENDIF
ENDM
; Call the macro
COND_JUMP NZ, TARGET, 100
COND_JUMP Z, FAR_TARGET, 200
; Expansion Result
; JNZ SHORT TARGET
; JZ FAR_TARGET
Practical Development Recommendations
- Parameter Isolation: Always use the
<span>&</span>operator to avoid ambiguity when parameters are adjacent to other text. - String Protection: Use
<span><></span>to wrap strings containing special characters. - Dynamic Calculation: Use the
<span>%</span>operator for values that need to be calculated at runtime. - Comment Management: Use
<span>;;</span>for internal implementation comments, and<span>;</span>for user-visible comments. - Debugging Tips: Use the
<span>/Zm</span>option to view macro expansion results to verify correct processing.
Common Errors and Solutions
-
Parameters Not Expanded:
- Error:
<span>MSG DB 'Hello &NAME'</span>(missing &) - Correction:
<span>MSG DB 'Hello &NAME&'</span>
Special Character Conflicts:
- Error:
<span>DB 'Use % operator'</span>(unexpectedly triggers % operator) - Correction:
<span>DB <Use % operator></span>
Expression Not Evaluated:
- Error:
<span>ARRAY X+1</span>(X+1 passed as a string) - Correction:
<span>ARRAY %X+1</span>
By mastering these special macro operators, highly flexible and powerful assembly macros can be created, significantly improving code reusability and development efficiency.