Sei sulla pagina 1di 10

4.

1Basic macro processor


functions

Introduction
z A macro instruction (abbr. macro) is a
notational convenience for the programmer.
z A macro represents a commonly used group of
statements in the source programming
language.
z The macro processor replaces each macro
instruction with the corresponding group of
source language statements.
• This is called expanding the macros.
2

1
Introduction (cont’d)
z The mostly common use of macro
processors is in assembler language
programming.
z The design of a macro processor is not
directly related to the architecture of the
computer on which it is to run.

Macro definition
z Two new assembler directives are used in
macro definition.
macro_name MACRO &para1, &para2
......
MEND
• The macro name and parameters define a pattern or
prototype for the macro instructions used by the
programmer.
• Following the MACRO directive are the statements
that make up the body of the macro definition.

2
Macro invocation
z A macro invocation statement gives the name
of the macro instruction being invoked and the
arguments to be used in expanding the macro.
z The process of macro invocation and
subroutine call are quite different.
• The statements that form the expansion of a macro
are generated (and assembled) each time the macro
in invoked.
• Statements in a subroutine appear only once,
regardless of how many times the subroutine is called.

A macro definition
Use of Macro

Macro invocation

3
Macro expansion
z The macro instruction definitions are deleted
since they are no longer needed after the
macros are expanded.
z Each macro invocation statement is expanded
into the statements that form the body of the
macro, with the arguments from the macro
invocation substituted for the parameters in the
macro prototype.
• Can a label appear in the body of a macro? Yes

Macro expansion (cont’d)


z After macro processing, the expanded
file can be used as input to the
assembler.

Macro processor Assembler


Source Expanded Object
program file program

4
Macro processor algorithm and
data structures

z A simple two-pass macro processor


• all macro definitions are processed during the first pass
• all macro invocation statements are expanded during
the second pass
z However, such a simple two-pass macro processor would not
allow definitions of macros within other macros
• the body of one macro instruction to contain definitions
of other macros.
because all macros would have to be defined during the first
pass before any macro invocations were expanded

10

5
Definitions of macros within other macros
If MACROS is invoked,
the macros RDBUFF
and WRBUFF for SIC
version are expanded.

If MACROX is
invoked, the macros
RDBUFF and
WRBUFF for SIC
version are expanded.

11

One-pass macro processor


z A one-pass macro processor that can
alternate between macro definition and macro
expansion is able to handle macros like
definitions of macros within other macros.
• Requirement:
The definition of a macro must appear in the source
program before any statements that invoke that macro.

12

6
Three main data structures
z DEFTAB
The macro definitions are stored in a definition table (DEFTAB), which
contains the macro prototype and the statements that make up the
macro body.
z NAMTAB
The macro names are enter into NAMTAB, which serves as an index
to DEFTAB.
For each macro instruction defined NAMTAB contains pointers to the
beginning and end of the definition in DEFTAB.
z ARGTAB
An argument table (ARGTAB) is used during the expansion of macro
invocations. When a macro invocation statement is recognized, the
arguments are stored in ARGTAB according to their position in the
argument list. As the macro is expanded, arguments from ARGTAB
are substituted for the corresponding parameters in the macro body.

13

The
Theparameter
parameter&DEV
&DEV
has
has been convertedto
been converted to?1,
?1,
&BUFADR
&BUFADRhas hasbeen
been
converted
convertedto
to?2,
?2,and
and
&RECLTH
&RECLTHhas hasbeen
been
converted
convertedto
to?3.
?3.

Invocation of RDBUFF on line 190

7
Algorithms

15

Algorithms (cont’d)

for MACRO-MEND pairs

16

8
Algorithms (cont’d)

GETLINE
GETLINEreads
readsaaline
lineeither
eitherfrom
frominput
inputfile
fileoror
from
fromDEFTAB.
DEFTAB.

17

A macro definition

MACRO-MEND pair

Macro invocation

9
10