Macros and macro processor
Macros and macro processor
Processors
Presented by: Foram Thakor
Overview
● Introduction
● Macro Definition and Call
● Macro Expansion
● Nested Macro Calls
● Advanced Macro Facilities
● Design of a Macro Preprocessor
● Design of a Macro Assembler
● Functions of a Macro Processor
● Basic Tasks of Macro Processor
● Design Issues of Macro Processors
Macros
● A macro is a sequence of instructions or statements that can be reused
multiple times in a program.
● Instead of writing the same code repeatedly, you define it as a macro and call
it when needed.
● Macros help in reducing code duplication and improving maintainability.
● A macro is a facility for extending a programming language .
● A macro either defines a new operation or a new method for declaring data .
● The language processor replaces a call on a macro by a sequence of
statements that implements the defined operation or the method of declaring
data.
● Many languages that support macro: C, C++
Macro Definition
● Macro definitions are typically located at the start of a program.
● A macro definition is enclosed between a macro header statement(MACRO)
and a macro end statement(MEND)
● Format of macro definition:
MACRO
body
MEND
Macro Definition (Contd.)
● A macro definition consist of macro prototype statement and body of macro.
● A macro prototype statement declares the name of a macro and its
parameters. MACRO indicates the beginning of macro definition and
parameters indicates the list of formal parameters.
● Parameters is of the form ¶meter1, ¶meter2,…Each parameter
begins with ‘&’.
● Whenever we use the term macro prototype it simply means the macro name
along with its parameters.
● Body of macro consist of statements that will generated as the expansion of
macro.
Macro Definition (Contd.)
● Consider the following macro definition:
MACRO
SUM &X,&Y
LDA &X
MOV B
LDA &Y
ADD B
MEND
● Here, the macro named SUM is used to find the sum of two variables passed to it.
Macro Call (or Macro Invocation)
● A macro invocation statement (a macro call) gives the name of the macro
instruction being invoked and the arguments to be used in expanding the
macro.
● When a macro is invoked in the code, it is referred to as a macro call. The
macro processor replaces this call with the macro body.
● The format of macro invocation:
IF <condition>
ELSE
ENDIF
Advanced Macro Facilities
2. Expansion Time Variables
● These are variables that can be defined within a macro and retain values
during the macro's expansion.
● They allow for dynamic behavior and state management within the macro.
● Useful for maintaining state or counters within a macro, such as keeping track
of how many times a macro has been called.
#SYNTAX
MACRO_NAME MACRO
SET variable = value
; use variable
ENDM
Advanced Macro Facilities
3. Conditional Expansions
● This feature allows certain parts of the macro to be included or excluded based on
conditions evaluated at the time of expansion.
● This is beneficial for generating different code paths based on compile-time
configurations or flags, allowing for more flexible and adaptable code.
#SYNTAX
MACRO_NAME MACRO
IF condition
; statements to include
ENDIF
ENDM
Advanced Macro Facilities
4. Local Variables in Macros
● Local variables (LCL) and global variables (GBL) allow defining variables that
are either local to the macro or accessible globally across multiple macros.
● Use local variables to store temporary values that are only needed during
macro expansion and should not interfere with variables outside the macro.
#SYNTAX
Expansion Time Variable Table (EVT) - varName: Name of the expansion time variable
Data Structures used by Macro Preprocessors
TABLE NAME ENTRY FIELDS
Expansion Time Variable Name Table - varName: Name of the expansion time variable
(EVNT)
Algorithm for Macro Expansion
● Initialization: Set the Macro Expansion Counter (MEC) to the starting point of
the macro.
● Check for MEND: While the statement pointed to by MEC is not a MEND
statement:
○ If the statement is a model statement, expand it and increment MEC.
○ If it is a control statement, update MEC based on the control logic specified.
● Parameter Substitution: Replace formal parameters in the model statements
with actual parameters from the APT.
● Output Generation: Generate the output assembly code by substituting the
expanded statements in place of the macro calls.
● End Expansion: Once all macro calls are processed, the preprocessor
outputs the final assembly program without any macro definitions.
Example of Macro Preprocessor Working
Consider a macro definition and call
MACRO
LCL &M
&M SET 0
MEND
Macro call
CLEARMEM AREA,10
Design of Macro Assembler
● A macro assembler extends the functionality of a standard assembler by
supporting macro instructions.
● These macros are essentially placeholders for code snippets that can be
reused throughout the assembly code.
● The macro assembler handles macro definitions and expansions, providing a
more flexible and modular approach to assembly programming.
● The assembler performs this translation through a series of passes over the
source code, allowing it to resolve symbols and generate the corresponding
machine instructions.
Key Components of a Macro Assembler
1. Macro Definition Table (MDT)
● Stores the definitions of macros, including the macro body with placeholders
for parameters.
● Facilitates macro expansion by providing the code to substitute for each
macro invocation.
1. Macro Name Table (MNT)
● Maintains a list of macro names and their corresponding identifiers or
addresses in the MDT.
● Allows quick lookup of macro names to retrieve their definitions.
1. Argument List Array (ALA)
● Stores the arguments provided for each macro invocation.
● Supplies the macro processor with arguments to replace placeholders in the
macro body.
Different Types of Assemblers
1. Single-Pass Assembler
● Functionality: Scans the source code only once to generate machine code.
● Process:
○ Reads each line of the source code.
○ Translates instructions into machine code immediately.
○ Requires all symbols to be defined before they are used (no forward
references).
● Advantages: Faster, as it only requires one scan of the source code.
● Disadvantages: Limited flexibility due to the need for all symbols to be
defined beforehand.
Different Types of Assemblers
2. Multi-Pass Assembler
● The macro processor can handle nested macro calls, where one macro
invokes another macro within its body.
● It manages the expansion in a recursive manner to handle multiple levels of
macro calls.
Key Functions of Macro Processor (Contd.)
5. Conditional Macro Expansion
6. Error Detection
● The final output from the macro processor is the expanded source code,
which can then be passed to the assembler.
Difference B/W Macro Processor & Macro Assembler
FEATURES MACRO PROCESSORS MACRO ASSEMBLER
Output Produces an assembly program with Generates the final machine code from
time process
Error Handling Checks for macro definition and Handles errors related to
Data Structures Used Utilizes tables like MNT, MDT, Uses symbol tables, opcode
Macro Definition and Clear syntax for defining macros and Consistent format (e.g., MACRO,
Integration with Seamless integration between macro Define clear interfaces for