Macro
Macro
Macro
Formally, macro instructions (often called macro) are single-line abbreviations for
groups of instructions.
For every occurrence of this one-line macro instruction within a program, the
instruction must be replaced by the entire block.
The advantages of using macro are as follows:
o Simplify and reduce the amount of repetitive coding.
o Reduce the possibility of errors caused by repetitive coding.
Macro Processors
A processor can be any program that processes its input data to produce output,
which is used as an input to another program.
The outputs of the macro processors are assembly programs that become inputs to
the assembler.
The macro processor may exist independently and be called during the assembling
process or be a part of the assembler implementation itself.
o Model statements: Specify the statements in the body of the macro from which
assembly language statements are to be generated during expansion.
o Macro preprocessor statement: Specifies the statement used for performing
auxiliary function during macro expansion.
Macro Definition
MACRO
MEND
Macro Call
INCR A, B
Macro Expansion
A macro call in a program leads to macro expansion. To expand a macro, the name of the
macro is placed in the operation field, and no special directives are necessary. During macro
expansion, the macro name statement in the program is replaced by the sequence of
assembly statements. Let us consider the following example:
START 100
A DS 1
B DS 1
+ MOVER REG A
+ ADD REG B
+ MOVEM REG A
PRINT A
STOP
END
The statements marked with a ‘+’ sign in the preceding label field denote the expanded
code and differentiate them from the original statements of the program.
Here expansion time control is transferred to the statement having .NEXT field only if the
actual parameter corresponding to the formal parameter length of ' 1'.
The design of a macro preprocessor is influenced by the provisions for performing the
following tasks involved in macro expansion:
Recognize macro calls: A table is maintained to store names of all macros defined in a
program. Such a table is called Macro Name Table (MNT) in which an entry is made for
every macro definition being processed. During processing program statements, a match is
done to compare strings in the mnemonic field with entries in the MNT. A successful match
in the MNT indicates that the statement is a macro call.
Determine the values of formal parameters: A table called Actual Parameter Table (APT)
holds the values of formal parameters during the expansion of a macro call. The entry into
this table will be in pair of the form (, ). A table called Parameter Default Table (PDT)
contains information about default parameters stored as pairs of the form (, ) for each
macro defined in the program. If the programmer does not specify value for any or some
parameters, its corresponding default value is copied from PDT to APT.
Maintain the values of expansion time variables declared in a macro: A table called
Expansion time Variable Table (EVT) maintains information about expansion variables in the
form (, ). It is used when a preprocessor statement or a model statement during expansion
refers to an EV.
Organize expansion time control flow: A table called Macro Definition Table (MDT) is used
to store the body of a macro. The flow of control determines when a model statement from
the MDT is to be visited for expansion during macro expansion. MEC {Macro Expansion
Counter) is defined and initialized to the first statement of the macro body in the MDT. MDT
is updated following an expansion of a model statement by a macro preprocessor.
Determine the values of sequencing symbols: A table called Sequencing Symbols Table
(SST) maintains information about sequencing symbols in pairs of the form
(<sequencing symbol name>, <MDT entry #>)
Where <MDT entry #> denotes the index of the MDT entry containing the model statement
with the sequencing symbol. Entries are made on encountering a statement with the
sequencing symbol in their label field or on reading a reference prior to its definition.
Perform expansion of a model statement: The expansion task has the following steps:
o MEC points to the entry in the MDT table with the model statements.
o APT and EVT provide the values of the formal parameters and EVs, respectively.
o SST enables identifying the model statement and defining sequencing.
Keeps track of the values of expansion time variables and sequencing symbols declared in
a macro.
Handles expansion time control flow and performs expansion of model statements.
Design of Two-pass Macro Preprocessor
Pass 0 of Assembler
2. If MACRO statement, continue reading the source and copy the entire macro definition to
the MDT. Go to Step 1.
3. If the statement is a pass-0 directive, execute it. Go to Step 1. (These directives are
written to the new source file in a unique manner (different from normal directives). They
are only needed for the listing in pass 2.
4. If the statement contains a macro name, it must perform expansion, that is, read model
statements from the MDT corresponding to the call, substitute parameters, and write each
statement to the new source file (or execute it if it is a pass-0 directive). Go to Step 1.
5. For any other statement, write the statement to the new source file. Go to Step 1.
6. If the current statement contains the END directive, stop (end of pass 0).
The assembler will be in one of the three modes:
In the normal mode, the assembler will read statement lines from the source file and write
them to the new source file. There is no translation or any change in the statements. In the
macro definition mode, the assembler will continuously copy the source file to the MDT.
In the macro expansion mode, the assembler will read statements from the MDT,
substitute parameters, and write them to the new source file. Nested macros can be
implemented using the Definition and Expansion (DE) mode.
11. Enter the statement into the MDT and increment the MDT by 1.
12. If MEND pseudo-op found, go to Step 2, else go to Step 10.