Professional Documents
Culture Documents
Chapter 4 Macro Processors
Chapter 4 Macro Processors
Introduction
A macro instruction (abbreviated to macro) is
simply
i l a notational
i l convenience
i
for
f the
h
programmer.
A macro represents a commonly used group of
statements in the source programming language
Expanding a macros
Replace
p
each macro instruction with the corresponding
p
g
group of source language statements
Introduction (Contd)
E.g.
On SIC/XE requires a sequence of seven instructions to
save the contents of all registers
Write
W it one statement
t t
t like
lik SAVERGS
Macro
Processor
A program without
ih
Macro definitions
Assembler
Object program
Expanded program
Figure
g
4.2
No macro instruction definitions
Each macro invocation statement has been expanded
into the statements that form the body of the macro,
with the arguments from the macro invocation
substituted for the parameters in the prototype
7
Pass
P 22:
All macro invocation statements are expanded
H
However, a ttwo-pass macro processor would
ld nott
allow the body of one macro instruction to contain
d fi iti
definitions
off other
th macros
See Figure 4.3
10
11
14
15
16
Machine-Independent
Machine
Independent Macro
Processor Feature
17
Concatenation of Macro
Parameters
Most macro processors allow parameters to
be concatenated with other character strings
The need of a special catenation operator
LDA X&ID1
LDA X&ID
19
Solution:
Allowing the creation of special types of labels within
macro instructions
See Figure 4.7
20
21
23
24
25
WHILE-ENDW
WHILE ENDW
See Figure 4.9
26
27
29
30
31
32
33
Solution:
These problems are not difficult to solve if the macro
processor is begin written in a programming language that
allows recursive call
34
General-Purpose
General
Purpose Macro
Processors
Macro processors have been developed for
some high-level programming languages
These special
special-purpose
purpose macro processors are
similar in general function and approach;
however the details differ from language to
however,
language
35
General-Purpose
General
Purpose Macro
Processors
The advantages of such a general-purpose
approach to macro processing are obvious
The programmer does not need to learn about a
different macro facility for each compiler or
assembler
asse
b e language,
a guage, so much
uc of
o the
t e time
t e and
a d
expense involved in training are eliminated
A substantial overall saving in software
development cost
36
General-Purpose
General
Purpose Macro
Processors
E.g.
E
g comments should usually be ignored by a
macro processor
37
General-Purpose
General
Purpose Macro
Processors
2. Another difference between programming
l
languages
is
i related
l d to their
h i facilities
f ili i for
f
grouping together terms, expressions, or
statements
38
General-Purpose
General
Purpose Macro
Processors
3. A more general problem involves the tokens
off the
h programming
i language
l
39
General-Purpose
General
Purpose Macro
Processors
4. Another potential problem with generalpurpose macro processors involves
i
l
the
h syntax
used for macro definitions and macro
i
invocation
i statements. With
Wi h most speciali l
purpose macro processors, macro invocations
are very similar
i il in
i form
f
to statements in
i the
h
source programming language
40
41