Macro processors
Macro processors let programmers define named text templates (macros) with parameters and substitute them into source code before assembly or compilation, saving repetition and enabling code reuse and simple code generation.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Macro processors are used by systems engineers when building assemblers, compilers, linkers or large assembly programs. A macro processor reduces repetitive code, implements parameterized code-generation templates, and performs source-level transformations that simplify later phases such as assembly and linking.
Key ideas
Start with definitions (each bolded the first time it appears).
- Macro: a named sequence of source statements that can contain parameters; when invoked, it is expanded in place by the macro processor. Define before use for many designs.
- Macro definition: the block in the source that gives a macro's name, its parameter list, and its body. Typical assembler keywords are
MACRO(start) andMENDorENDM(end) — different assemblers use different tokens. - Macro invocation (or macro call): a line in source that names a macro and supplies actual arguments; the processor replaces that line with the macro body with parameters substituted.
- Parameter substitution: replacing formal parameter placeholders in the macro body with the actual arguments from a specific invocation.
- Positional parameter: a formal parameter identified by position (first, second, ...). Often written as
&1,&2or by names listed in definition. - Keyword parameter: a formal parameter given by name in the invocation (e.g.,
COUNT=5); can have defaults. - Nested macro: a macro that invokes another macro or itself (recursion permitted only if the processor supports it). Recursive macro is where a macro calls itself directly or indirectly.
- Macro definition table (MDT): stores macro bodies indexed for expansion.
- Macro name table (MNT): stores macro names along with pointers/indices into the MDT and parameter information.
- Argument list array (ALA): per-invocation table that maps formal parameters to actual arguments during expansion.
- Expansion time vs definition time: Most assembler macro processors perform substitution and nested macro expansion at expansion time (when an invocation is processed), not at definition time. Defaults may be fixed by the definition, and some assemblers provide macro-time directives; check your assembler's rules.
Precise statement (university style): Definition. A macro processor is a text-to-text translator M such that for any source S containing macro definitions D and non-macro text T, M(S) = T' where every macro invocation in S is replaced by its text expansion according to D and the substitution rules. Assumptions: macros are well-formed, parameter lists match or use defaults, and macro names do not collide with normal instruction mnemonics unless explicitly allowed.
Design principles and typical algorithms (two-pass macro processor — common exam topic): Pass 1 (Definition processing): read source sequentially. For each macro definition encountered, assign it an entry in MNT, copy its body lines into MDT, record formal parameters and their positions into ALA template. Remove definition from output (do not expand yet). Output non-macro lines to an intermediate file. Pass 2 (Expansion): read intermediate file. For each macro invocation, consult MNT to locate MDT entry, create a fresh ALA mapping formal -> actual, then copy MDT lines to output replacing formals by actuals. If nested invocation found in expansion, handle by recursive expansion or by queuing depending on implementation.
What to draw (diagram exam): Draw a flowchart with these boxes left-to-right: "Source Program" -> arrow -> "Pass 1: build MNT/MDT/ALA-template" -> arrow -> "Intermediate File" -> arrow -> "Pass 2: expand using MNT/MDT/ALA" -> arrow -> "Output Expanded Source". Below or alongside, draw three tables: MNT (columns: MacroName | MDT Index | ParamCount), MDT (Index | MacroLine), ALA (Formal | Position | Actual). Label arrows showing lookups MNT->MDT and MDT->ALA substitution.
Edge rules often tested:
- Parameter numbering: formal parameters are replaced consistently by ALA entries.
- Default values: if an actual is not provided, use the default from the definition; if missing and no default, it is an error.
- Mixing positional and keyword arguments: many assemblers require all positional arguments first, followed by keyword arguments; omitting a middle positional argument may require a null placeholder.
- Unique labels / token concatenation: assemblers often provide token-pasting or sequence symbols (e.g., && or ##) to generate unique labels per expansion.
- Conditional assembly and macro-time variables/local symbols: directives such as AIF/AGO (conditional/branch), SET (assign), and LCL/GBL (local/global) may be supported; know your assembler’s tokens.
- Recursive expansion: must prevent infinite expansion — typically limit recursion depth.
Formulas
For a programming topic list the syntax to remember. Use these generic forms (actual assembler keywords vary):
MACRO <MacroName> <Formal1>, <Formal2>=<Default>, ... — starts macro definition. Formal names are identifiers. MACRO token literal depends on assembler.
<body lines using &<FormalName> or &1, &2> — body with parameter placeholders. & is the parameter-prefix convention in many assemblers.
MEND or ENDM — end macro definition (assembler dependent).
<MacroName> <Arg1>, <Arg2> — macro invocation using positional arguments.
<MacroName> FormalName=Value — macro invocation using keyword arguments.
Tables and indexing (conceptual):
MNT[MacroName] -> MDT_index, param_count — MNT entry meaning.
MDT[MDT_index + i] -> body_line_i — MDT stores body lines, typically terminated by a MEND/ENDM sentinel that is not copied to the output.
ALA[FormalName] = ActualValue — mapping for current expansion.
Token concatenation/pasting (assembler-dependent): e.g., <prefix>&&<FormalName> or prefix##<FormalName> to build unique names.
Worked examples
Example 1 — simple positional macro expansion (step-by-step) Source (lines): 1: MACRO INCR &ARG 2: LOAD &ARG 3: ADD #1 4: STORE &ARG 5: MEND 6: INCR A 7: STOP
Pass 1: Read lines 1–5 as macro definition. MNT adds entry: INCR -> MDT index 0, param_count 1. MDT lines (index 0..2): LOAD &ARG, ADD #1, STORE &ARG. Intermediate file contains lines 6–7.
Pass 2: Expand line 6 INCR A: create ALA: ALA[&ARG] = A. Copy MDT lines replacing &ARG by A.
Expanded output (final) lines:
1: LOAD A
2: ADD #1
3: STORE A
4: STOP
Final answer: the three body lines appear in place of the single macro invocation. (Check: parameter &ARG substituted by A.)
Example 2 — keyword parameters and defaults Definition: 1: MACRO MOVR &SRC, &DEST=R2 2: MOV &DEST, &SRC 3: MEND Calls: A: MOVR X ; only positional given B: MOVR Y, Z ; positional + explicit destination
Pass 1 builds MNT/MDT. Pass 2 expansions:
- For A: ALA:
&SRC=X,&DEST=R2(default used). Expansion:MOV R2, X. - For B: ALA:
&SRC=Y,&DEST=Z. Expansion:MOV Z, Y.
Example 3 — building MNT and MDT on paper (two-pass trace, exam style) Input lines: 1: MACRO M1 &A,&B 2: LOAD &A 3: M2 &B 4: MEND 5: MACRO M2 &X 6: ADD &X 7: MEND 8: M1 P,Q
Pass1 (definitions processed): MNT entries (example table):
- M1 -> MDT index 0, params 2
- M2 -> MDT index 3, params 1
MDT lines:
0: LOAD &A
1: M2 &B
2: MEND ; sentinel, not copied to output
3: ADD &X
4: MEND ; sentinel, not copied to output
Intermediate file: line 8
M1 P,Q. Pass2 expandM1 P,Q: ALA for M1:&A=P,&B=Q. Copy MDT[0] ->LOAD P. MDT[1] isM2 &Bwhich is a nested invocation. Expand nested call: lookup M2, form ALA for M2&X = Q, MDT for M2 ->ADD Q. Final output:LOAD PthenADD Q.
Show final expanded output lines: LOAD P, ADD Q.
Common mistakes
- Forgetting defaults: when a parameter has a default and caller omits it, students sometimes leave a blank instead of substituting default.
- Ignoring nested expansions: failing to recursively expand calls that appear inside macro bodies or expanding them in the wrong order.
- Infinite recursion oversight: writing macros that call themselves without stopping condition and not noting recursion limits.
- Wrong table pointers: when building MNT/MDT, giving wrong MDT indices by off-by-one errors.
For your exam
Types of questions:
- Definitions and short notes: define macro, MNT, MDT, ALA; explain difference between positional and keyword parameters.
- Diagram/flow questions: draw two-pass macro processor flow and table structures; label MNT/MDT/ALA and arrows.
- Derivation/algorithm: give step-by-step algorithm for pass 1 and pass 2; show table updates.
- Comparison: macros vs subroutines/procedures (inline expansion vs call/return overhead; code size vs speed; binding time).
- Trace / programming question: given a small source with macros, produce MNT/MDT/ALA and final expanded output (common long question). Show intermediate file and each pass's actions.
- Error-detection: identify errors in given macro definitions or calls (missing defaults, wrong parameter counts).
What to include for full marks:
- Correct definitions with bolded terms and clear assumptions.
- A clear diagram or table showing MNT, MDT and ALA with column names and sample entries.
- A full two-pass trace for any expansion question: show pass1 activities (table contents) and pass2 expansions (ALA and final lines) step-by-step.
- For nested/keyword/default problems, show ALA content explicitly for each invocation.
Quick check
- What table stores macro bodies in a two-pass macro processor?
- In a macro call
M Xwith definitionMACRO M &A, &B=R0, what is the value of&Bduring expansion if caller supplies onlyX? - Name two common tokens used to end a macro definition in assemblers.
Answers: MDT; R0; MEND and ENDM (terminator varies by assembler).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?