You are on page 1of 10

4.

1Basic macro processor functions

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

This is called expanding the macros.


2

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

Macro definition
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.

Macro invocation
A macro invocation statement gives the name of the macro instruction being invoked and the arguments to be used in expanding the macro. 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

Macro expansion
The macro instruction definitions are deleted since they are no longer needed after the macros are expanded. 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 (contd)


After macro processing, the expanded file can be used as input to the assembler.
Macro processor Expanded file Assembler Object program

Source program

Macro processor algorithm and data structures


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 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

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


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

Three main data structures


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. 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. 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 parameter &DEV The parameter &DEV has been converted to ?1, has been converted to ?1, &BUFADR has been &BUFADR has been converted to ?2, and converted to ?2, and &RECLTH has been &RECLTH has been converted to ?3. converted to ?3.

Invocation of RDBUFF on line 190

Algorithms

15

Algorithms (contd)

for MACRO-MEND pairs

16

Algorithms (contd)

GETLINE reads aaline either from input file or GETLINE reads line either from input file or from DEFTAB. from DEFTAB.

17

A macro definition

MACRO-MEND pair

Macro invocation

10

You might also like