Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Design of macro processors

Design of macro processors: definitions, data structures (MDT, MNT, ALA), algorithms for definition and expansion, examples of tracing expansions, common mistakes and exam guidance.

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 remove repeated code by expanding shorthand macro calls into full code sequences during assembly or compilation. Engineers use macro processors in assemblers, compilers, and build tools to improve code reuse, reduce programmer error and implement language abstractions before later phases (like assembler or compiler) run.

Key ideas

We start from first principles and define each term before using it.

  • Macro — a macro is a named sequence of source statements that can be inserted (expanded) into program text by a single macro invocation. It is an abbreviation mechanism: the macro name plus actual arguments is replaced by the body with formal parameters substituted.

  • Macro definition — a macro definition is the source text that gives the macro name, its formal parameters (if any), and the sequence of statements that form the macro body. Typical assembler-style boundaries: a MACRO directive starts the definition and MEND ends it.

  • Macro call (invocation) — a macro call is a statement in the source program that names a macro and supplies actual parameters; at macro expansion time the macro body replaces the call.

  • Macro processor — a macro processor is the program (or compiler phase) that reads macro definitions and program source and performs the expansion of macro calls into ordinary source statements. It communicates with the assembler or compiler phase that follows.

  • Positional parameter — a positional parameter is a formal parameter position referenced in the macro body; the actual parameter that appears in that position at the call site is substituted in expansion.

  • Keyword parameter — a keyword parameter is a formal parameter with a name and optional default value; the actual parameter can be supplied using the keyword at the call site.

  • Nested (or hierarchical) macros — nested macros occur when a macro body contains a call to another macro (or itself). The macro processor must support nested expansion rules and often a call stack or recursion guard.

  • Recursive macro — a recursive macro is one that (directly or indirectly) calls itself during expansion. Many macro processors disallow or limit recursion; others use runtime/expansion depth limits.

  • Argument List Array (ALA) — the ALA is the table that stores actual argument textual values for a particular macro call, indexed by the formal parameter position or name. Each macro expansion typically builds an ALA instance.

  • Macro Name Table (MNT) — the MNT records each macro name, the pointer (index) to its definition in the MDT, the number (count) of positional and keyword parameters, and sometimes the pointer to its parameter list in another table.

  • Macro Definition Table (MDT) — the MDT stores the macro body text lines, but with formal parameters replaced by canonical placeholders (for example (P,1), (P,2) or #1, depending on convention). The MNT contains pointers (indices) into the MDT indicating where the macro body begins and ends.

  • Parameter Name Table (PNTAB) — the PNTAB stores the formal parameter names of a macro in order; used to map names to positional indices and to generate placeholders consistently.

  • Keyword Parameter Default Table (KPDTAB) — the KPDTAB stores keyword parameter names and their default values; the MNT typically holds KPD_Start and KP_Count fields to point into this table.

  • Prototype expansion / parameter substitution rule — when expanding a macro, the macro processor replaces each formal parameter reference in the MDT with the corresponding actual argument from the ALA. The substitution can be a simple textual replace or token-level replace depending on the macro processor.

  • Two-pass macro processor — a typical design uses two passes: pass 1 reads source and collects macro definitions, constructing tables such as MNT and MDT (and PNTAB/KPDTAB if keyword parameters are supported), but does not expand calls; pass 2 reads the source again and expands macro calls using the tables built in pass 1. A single-pass macro processor interleaves definition storage and expansion and therefore typically requires macros to be defined before use (or an auxiliary lookahead mechanism).

  • Exact statement (definition used in exams):

    • "A macro processor is a translator which takes a program with macro calls and macro definitions and outputs an equivalent program with macro calls replaced by their expanded bodies, performing parameter substitution and managing nested calls according to the macro definition semantics." State any assumption such as: "Assume macro definitions are non-overlapping, macros are defined before use (for single-pass), parameter lists are comma-separated, and body lines end with a unique MEND directive."
  • Data structure fields to draw and label (for an exam diagram): draw three rectangular tables side-by-side labelled MNT, MDT, ALA. For MNT: columns — MacroName | MDT_Start | MDT_End | PP_Count [and for keyword parameters: KPD_Start | KP_Count]. For MDT: rows numbered starting 1, each row contains the macro body line with parameter placeholders. For ALA (per expansion): columns — ParamIndex | ActualText. Optionally show PNTAB listing formal names mapped to indices, and KPDTAB listing keyword parameters with defaults. Draw arrows from MNT.MDT_Start to a specific row in MDT, and from parameter placeholders in MDT to entries in ALA during expansion.

  • Algorithms (high level):

    1. Definition processing: on seeing MACRO, read header and formal parameters, add entry to MNT with MDT start pointer, read each body line replacing formal parameter names with placeholders and store in MDT until MEND. Record NumParams.
    2. Expansion processing: on seeing a macro call, fetch MNT entry, build ALA by mapping actuals to indices, copy MDT lines from MDT_Start for NumLines, replacing placeholders by ALA entries, output the replaced lines. Handle nested calls by pushing return point (or using recursion) and avoiding infinite recursion.

Assumptions to state in answers: parameter tokens are clearly delimited, macros do not modify global symbols unless specified, and line concatenation rules are simple textual substitution.

Formulas

For this programming topic, list the key syntax and table field formats to remember. Each item on its own line in backticks. MACRO <MacroName> <formal1>,<formal2>,... — start macro definition; <formal> denotes formal parameter name (no whitespace). <body line with formal parameter references> — body lines; assembler-style replace formal with placeholder like #1 or (P,1). MEND — end macro definition. <MacroName> <actual1>,<actual2>,... — macro invocation (call) syntax. MNT entry format: (MacroName, MDT_Start, MDT_End, PP_Count[, KP_Count, KPD_Start]) — Macro Name Table fields; include keyword-related fields if supported. MDT row format: (LineNumber, TextWithPlaceholders) — Macro Definition Table stores body lines with placeholders. ALA format (per expansion): (ParamIndex, ActualText) — Argument List Array maps placeholders to actual values. <MacroName> <key1>=<val1>,<key2>=<val2>,... — keyword-parameter invocation; unspecified keywords take their defaults from KPDTAB. PNTAB entry format: (Index, FormalName) — Parameter Name Table mapping formal names to indices. KPDTAB entry format: (KeywordName, DefaultValue) — Keyword Parameter Default Table entries; MNT points to a slice via (KPD_Start, KP_Count).

Worked examples

Example 1 — Simple positional macro (trace building tables and expansion) Source (lines numbered):

  1. MACRO
  2. INCR &A
  3. LDA &A
  4. ADD #1
  5. STA &A
  6. MEND
  7. START
  8. INCR X
  9. END

Assume placeholder convention: (P,1) replaces &A in the MDT at storage time. Steps (numbered):

  1. Read line 1 MACRO → enter definition mode.
  2. Header INCR &A: add MNT entry ('INCR', MDT_Start=1, NumParams=1).
  3. Body lines store in MDT replacing &A with (P,1): MDT[1] = LDA (P,1) MDT[2] = ADD #1 MDT[3] = STA (P,1)
  4. MEND ends definition.
  5. Later, line 8 INCR X is a macro call. Build ALA: ALA[1] = X.
  6. Expand by copying MDT lines and substituting (P,1) → X: Output lines: LDA X ADD #1 STA X Final expanded code (replace call at line 8): LDA X ; ADD #1 ; STA X

Example 2 — Nested macros and expansion trace Source:

  1. MACRO
  2. SWAP &A,&B
  3. TEMP = &A
  4. &A = &B
  5. &B = TEMP
  6. MEND
  7. MACRO
  8. ROTATE &X,&Y,&Z
  9. SWAP &X,&Y
  10. SWAP &X,&Z
  11. MEND
  12. START
  13. ROTATE R1,R2,R3
  14. END

Steps:

  1. Store SWAP in MNT with MDT_Start=1, MDT rows: MDT1: TEMP = #1 MDT2: #1 = #2 MDT3: #2 = TEMP
  2. Store ROTATE in MNT with MDT_Start=4, MDT rows: MDT4: SWAP #1,#2 (placeholders for &X,&Y) MDT5: SWAP #1,#3 (placeholders for &X,&Z)
  3. Call ROTATE R1,R2,R3: build ALA for ROTATE: ALA_ROT[1]=R1, [2]=R2, [3]=R3.
  4. Expand ROTATE: read MDT4 line SWAP #1,#2 → substitute → SWAP R1,R2. This is a macro call inside expansion → handle nested expansion:
    • For nested SWAP R1,R2: build ALA_SWAP [1]=R1,[2]=R2; expand its MDT1-3 substituting placeholders → produce TEMP = R1 R1 = R2 R2 = TEMP
  5. Next line MDT5 SWAP #1,#3 → substitute → SWAP R1,R3 → expand similarly producing TEMP = R1 R1 = R3 R3 = TEMP
  6. Final expanded sequence replacing the single ROTATE call is the concatenation of the two expanded SWAP bodies: TEMP = R1 ; R1 = R2 ; R2 = TEMP ; TEMP = R1 ; R1 = R3 ; R3 = TEMP

Notes on the examples: we used (P,n) placeholders in Example 1 to avoid conflict with the assembler's immediate-constant #, and #n placeholders in Example 2; real assemblers may use either or ?n syntax. If parameter passing semantics are different (by reference vs by value), results differ; state assumed textual substitution semantics.

Common mistakes

  • Confusing positional and keyword parameters: not matching actuals to correct formals when order differs.
  • Ignoring the need for unique temporary names in macros (macro reentrancy problem): using a fixed temporary symbol like TEMP leads to name clashes when macro is expanded multiple times in same program.
  • Assuming macros are like functions with local scope; in simple macro processors, symbol names in macro bodies are not locally renamed unless the macro system implements hygienic renaming.
  • Not guarding against recursion: infinite expansion if a macro calls itself without a stopping condition.

For your exam

Types of questions you will see:

  • Definitions and short notes: define macro, macro processor, MNT, MDT, ALA, explain two-pass vs single-pass macro processors. Full-marks answer: give the definition, list assumptions (parameter delimiter, MEND marker), and draw a small diagram of MNT/MDT/ALA with column names.
  • Algorithms and derivations: describe step-by-step how macro definition is stored and how expansion is done, including handling of nested calls. Full-marks answer: give pseudo-code for definition-phase and expansion-phase and an example trace showing MNT/MDT/ALA updates.
  • Numerical / tracing problems (actually algorithmic traces): given a source with macros, construct MNT and MDT and show final expanded code. Full-marks answer: show tables with exact entries and the substituted output lines, numbering and matching tokens.
  • Small programming/implementation question: write pseudocode for building MDT and expanding a call or to detect recursion. Full-marks answer: correct pseudocode with handling for nested calls (stack or recursion), ALA construction, and a recursion depth guard.
  • Diagrams: draw MNT, MDT, ALA and show pointers. Full-marks answer: correctly labelled tables and arrows, and brief explanation.

Exam tips: always state the placeholder convention you use; show the ALA for each expansion; when using temporary labels in macros, show how to make them unique (e.g., append expansion count).

Quick check

  1. What table stores macro body lines with placeholders?
  2. What must you build at a macro call to map actual arguments to formals?
  3. Name one danger of using an unqualified symbol like TEMP inside a macro body.

Answers: MDT; ALA (Argument List Array); name clash / accidental symbol collision (requires unique temporaries or hygiene).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?