Professional Documents
Culture Documents
Macro Processors
1. Introduction to Macros
2. Features of Macros
3. What is Macro processor
4. Design of Single Pass Macro processor
4.1 Handling Nested Macros
4.2 Handling Conditional Macros
5. Design of Two-Pass Macro Processor
2
1. Introduction to Macro
Concept:
» A macro instruction is a notational convenience for the
programmer
» It allows the programmer to write shorthand version of a
program (module programming)
» The macro processor replaces each macro invocation with
the corresponding sequence of statements (expanding)
» Macro:- A macro is a unit of specification for program
generation through expansion.
» A macro consists of a name, a set of formal parameters and
a body of code.
» The use of macro name with a set of actual parameters is
replaced by code generated from its body.
3
1.Introduction to Macro
4
1. Introduction to Macro
Macro Call:-
A macro is called by writing the macro name in the mnemonic field of an
assembly statement.
Syntax:-
<macro name> [<actual parameter list>]
Example:-
MACRO
TEST &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &Y
AGO .LAST
.ONLY MOVER AREG, &Z
.LAST MEND
Macro Call:-
TEST S,10
5
2. Features of Macros
Conditional Macros
Nested Macros
6
3. Macro Processor
Source
Macro Expanded Compiler or obj
Code
(with macro)
Processor Code Assembler
7
Macro vs. Subroutine (Function)
Macro
» the statement of expansion are generated each time the
macro are invoked
» In macro call, entire macro code is substituted for macro call
Subroutine
» the statement in a subroutine appears only once
» In Function call, control is transferred where it is defined.
8
Macro Definition
copy code
parameter substitution
conditional macro expansion
macro instruction defining macros
9
Copy code -- Example
.
STRG
MEND
{
.
STB
STX
DATA2
DATA3
. STA DATA1
STRG
.
.
{
.
STB
STX
DATA2
DATA3
10
Parameter Substitution -- Example
.
MEND
. STA DATA4
STRG DATA4, DATA5, DATA6
.
.
{
.
STB
STX
DATA5
DATA6
11
Parameter Substitution
Dummy arguments
» Positional argument
STRG DATA1, DATA2, DATA3
GENER ,,DIRECT,,,,,,3
» Keyword argument
STRG &a3=DATA1, &a2=DATA2, &a1=DATA3
GENER TYPE=DIRECT, CHANNEL=3
12
4. One-Pass Macro Processor
Prerequisite
» every macro must be defined before it is called
Sub-procedures
» macro definition: DEFINE
» macro invocation: EXPAND
NAMTAB
PROCESSLINE
CALL EXPAND ARGTAB
13
Nested Macros Definition
14
Nested Macros Definition
15
NO LABEL IN MACRO BODY
Problem of the label in the body of macro:
◦ If the same macro is expanded multiple times at different
places in the program …
◦ There will be duplicate labels, which will be treated as
errors by the assembler.
Solutions:
◦ Do not use labels in the body of macro.
◦ Explicitly use PC-relative addressing instead.
Ex, in RDBUFF and WRBUFF macros,
DESIGN
OF
Sub-procedures
» macro definition: DEFINE
» macro invocation: EXPAND
EXPAND may invoke DEFINE when encounter
macro definition
NAMTAB
MACRO DEFINE
DEFTAB PROCESSLINE
CALL EXPAND
ARGTAB
18
ONE-PASS MACRO PROCESSOR
A one-pass macro processor that alternate
between macro definition and macro expansion
in a recursive way is able to handle recursive
macro definition.
Restriction
◦ The definition of a macro must appear in the
◦ source program before any statements that
◦ invoke that macro.
◦ This restriction does not create any real
◦ inconvenience.
DATA STRUCTURE FOR ONE-PASS MACRO
PROCESSOR
ARGTAB
◦ Stores the arguments of macro invocation
◦ according to their positions in the argument list
◦ As the macro is expanded, arguments from
◦ ARGTAB are substituted for the corresponding
◦ parameters in the macro body.
DATA STRUCTURE
ALGORITHM
ALGORITHM
ALGORITHM
ALGORITHM
1-Pass Macro Processor
DEFINE
MACRO
PROCESSOR
GETLINE
EXPANDING=FALSE
PROCESSLINE
GETLINE EXPAND
PROCESSLINE
EXPANDING=TRUE
GETLINE
GETLINE
PROCESSLINE
EXPANDING FALSE
TRUE
TRUE
◦The macro processor continues to process lines from DEFTAB
until it encounters the next ELSE or ENDIF statement.
◦If ELSE is encountered, then skips to END
FALSE
◦The macro processor skips ahead in DEFTAB until it finds the
next ELSE or ENDIF statement.
IMPLEMENTATION-CONDITIONAL
MACRO EXPANSION
When an IF statement is encountered during the expansion of a
macro, the specified Boolean expression is evaluated.
-TRUE
FALSE
◦ -- The macro processor skips ahead in DEFTAB
until it finds the next ENDW statement and then
resumes normal macro expansion.
KEYWORD MACRO PARAMETERS
Keyword macro parameters
◦ Positional parameters and arguments are associated
according to their positions in the macro prototype
and invocation.
◦ If an argument is to be omitted, a null argument
should be used to maintain the proper order in macro
invocation:
◦ Ex: XXX MACRO &P1, &P2, …., &P20, ….
XXX A1, A2,,,,,,,,,,…,,A20,…..
KEYWORD MACRO PARAMETERS
Keyword Parameters
◦ Each argument is written with the keyword that names
the corresponding parameter.
Two-pass algorithm
◦ Pass1: Recognize macro definitions
◦ Pass2: Recognize macro calls
◦ Nested macro definitions are not allowed
Comparison of Macro Processors Design
Single pass
» every macro must be defined before it is called
» one-pass processor can alternate between macro definition
and macro expansion
» nested macro definitions may be allowed but nested calls
are not
Two pass algorithm
» Pass1: Recognize macro definitions
» Pass2: Recognize macro calls
» nested macro definitions are not allowed
38
Macro Processor Design Options
Recursive Macro expression
General-Purpose Macro Processors
Macro Processing within Language Translators
Recursive Macro Expansion
Macro within macro can be solved if the macro
processor is being written in a programming language
that allows recursive calls
MASM generates the unique names of local labels in the form ??n,
where n is a hexadecimal number in the range 0000 to FFFF
MDTC<-1
MNTC<-1
Read next
source card
MACRO No
Write copy of
pseudo-
source card
op?
Yes
END
No
pseudo-
Read next
op?
source card
Yes
Read next
source card
(copied by
pass 1)
Substitute arguments
from macro call
MEND Write
pseudo- expanded
Yes op? source card
No
Thank You