0% found this document useful (0 votes)
40 views14 pages

UNIT IV Macro Processor

The document discusses macro processors and their functions. Some key points: - A macro represents commonly used groups of statements and is expanded by a macro processor. This allows shorthand programming. - A macro processor replaces each macro invocation with the corresponding statements defined in the macro. It handles macro definition, invocation, and expansion. - Data structures like definition, name, and argument tables are used to implement a one-pass macro processor that can handle nested macros. Macro definitions and invocations are processed in a single pass.

Uploaded by

elias ferhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views14 pages

UNIT IV Macro Processor

The document discusses macro processors and their functions. Some key points: - A macro represents commonly used groups of statements and is expanded by a macro processor. This allows shorthand programming. - A macro processor replaces each macro invocation with the corresponding statements defined in the macro. It handles macro definition, invocation, and expansion. - Data structures like definition, name, and argument tables are used to implement a one-pass macro processor that can handle nested macros. Macro definitions and invocations are processed in a single pass.

Uploaded by

elias ferhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

UNIT IV.

MACRO PROCESSORS

4.1 MACRO PREPROCESSOR

Macro

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. Macro instructions allow the
programmer to write a shorthand version of a program, and leave the mechanical details to be
handled by the macro processor.

A macro (or macro instruction)

➢ It is simply a notational convenience for the programmer.


➢ It allows the programmer to write shorthand version of a program
➢ It represents a commonly used group of statements in the source program.

For example: Suppose a program needs to add two numbers frequently. This requires a sequence

of instructions. We can define and use a macro called SUM, to represent this

sequence of instructions.

SUM MACRO &X,&Y

LDA &X

MOV B

LDA &Y

ADD B

MEND

1
Macro Preprocessor

The macro pre-processor (or macro processor) is a system software which replaces each macro-
instruction with the corresponding group of source language statements. This operation is called
expanding the macro.

• It does not concern the meaning of the involved statements during macro expansion.
• The design of a macro processor generally is machine independent.

The most common use of macro processors is in assembler language programming. However,
macro processors can also be used with high-level programming languages, operating system
command languages, etc.

BASIC MACRO PROCESSOR FUNCTIONS

The fundamental functions common to all macro processors are:-

o Macro Definition

o Macro Invocation

o Macro Expansion

Macro Definition

➢ Macro definitions are typically located at the start of a program.


➢ A macro definition is enclosed between a macro header statement(MACRO) and a

macro end statement(MEND)

2
Format of macro definition

macroname MACRO parameters

body

MEND

A macro definition consist of macro prototype statement and body of macro. A macro prototype
statement declares the name of a macro and its parameters. It has following format:

macroname MACRO parameters

, where macroname indicates the name of macro, MACRO indicates the beginning of macro
definition and parameters indicates the list of formal parameters. parameters is of the form
&parameter1, &parameter2,…Each parameter begins with ‘&’. Whenever we use the term macro
prototype it simply means the macro name along with its parameters.

Body of macro consist of statements that will generated as the expansion of macro.

Consider the following macro definition:

SUM MACRO &X,&Y

LDA &X

MOV B

LDA &Y

ADD B

MEND

Here, the macro named SUM is used to find the sum of two variables passed to it.

3
Macro Invocation (or Macro Call)

A macro invocation statement (a macro call) gives the name of the macro instruction being invoked
and the arguments to be used in expanding the macro.

➢ The format of macro invocation

macroname p1, p2,...pn

➢ The above defined macro can be called as SUM P,Q

Macro Expansion

➢ Each macro invocation statement will be expanded into the statements that form the body
of the macro.
➢ Arguments from the macro invocation are substituted for the parameters in the macro
prototype.
➢ The arguments and parameters are associated with one another according to their positions.
The first argument in the macro invocation corresponds to the first parameter in the macro
prototype, etc.
➢ Comment lines within the macro body have been deleted, but comments on individ ua l
statements have been retained. Macro invocation statement itself has been included as a
comment line.
➢ Consider the example for macro expansion on next page:

In this example, the macro named SUM is defined at the start of the program. This macro is
invoked with the macro call SUM P, Q and the macro is expanded as

LDA &P

MOV B

LDA &Q

ADD B

MEND

Again the same macro is invoked with the macro call SUM M,N and the macro is expanded as:

4
LDA &M

MOV B

LDA &N

ADD B

MEND

Example for macro expansion :-

MACRO PROCESSOR ALGORITHM AND DATA STRUCTURES


Two pass macro processor

➢ It is easy to design a two-pass macro processor in which all macro definitions are processed
during the first pass and all macro invocation statements are expanded during second pass.
➢ Such a two-pass macro processor cannot handle nested macro definitions. Nested macros
are macros in which definition of one macro contains definition of other macros.

5
➢ Consider the macro definition example given below, which is used to swap two numbers.
The macro named SWAP defines another macro named STORE inside it. These types of
macro are called nested macros.

One pass macro processor

➢ A one-pass macro processor uses only one pass for processing macro definitions and macro
expansions.
➢ It can handle nested macro definitions.
➢ To implement one pass macro processor, the definition of a macro must appear in the
source program before any statements that invoke that macro.

Data Structures involved in the design of one pass macro processor

There are 3 main data structures involved in the design of one pass macro processor:

➢ DEFTAB
➢ NAMTAB
➢ ARGTAB

6
Definition table (DEFTAB)

➢ All the macro definitions in the program are stored in DEFTAB, which includes macro
prototype and macro body statements.
➢ Comment lines from macro definition are not entered into DEFTAB because they will not
be a part of macro expansion.
➢ References to the macro instruction parameters are converted to a positional notation for
efficiency in substituting arguments.

Name table (NAMTAB)

➢ The macro names are entered into NAMTAB


➢ NAMTAB contains pointers to beginning and end of definition in DEFTAB.

Argument table (ARGTAB)

➢ The third data structure is an argument table (ARGTAB), which is used during expansion
of macro invocations.
➢ When macro invocation statements are recognized, the arguments are stored in ARGTAB
according to their position in argument list.
➢ As the macro is expanded, arguments from ARGTAB are substituted for the corresponding
parameters in the macro body.
➢ Example: Consider the following source code

7
➢ When the macro definition for SUM is encountered, the macro name SUM along with its
parameters X and Y are entered into DEFTAB. Then the statements in the body of macro
is also entered into DEFTAB. The positional notation is used for the parameters. The
parameter &X has been converted to ?1, &Y has been converted to ?2.
➢ The macro name SUM is entered into NAMTAB and the beginning and end pointers are
also marked.
➢ On processing the input code, opcode in each statement is compared with the NAMTAB,
to check whether it is a macro call. When the macro call SUM P,Q is recognized, the
arguments P and Q will entered into ARGTAB. The macro is expanded by taking the
statements from DEFTAB using the beginning and end pointers of NAMTAB.
➢ When the ?n notation is recognized in a line from DEFTAB, the corresponding argument
is taken from ARGTAB.

Figure below shows the different data structures used:

8
MACHINE INDEPENDENT MACRO PROCESSOR FEATURES

The features of macro which doesn’t depends on the architecture of machine is called machine
independent macro processor features. The features includes:

1. Concatenation of Macro Parameters

2. Generation of Unique Labels

3. Conditional Macro Expansion

4. Keyword Macro Parameters

1. Concatenation of Macro Parameters

➢ Most macro processor allows parameters to be concatenated with other character


strings.
➢ Suppose that a program contains a series of variables named by the symbols XA1, XA2,
XA3,..., and another series of variables named XB1, XB2, XB3,..., etc.
➢ If similar processing is to be performed on each series of labels, the programmer might
put this as a macro instruction.
➢ The parameter to such a macro instruction could specify the series of variables to be
operated on (A, B, etc.). The macro processor would use this parameter to construct the
symbols required in the macro expansion (XA1, XB1, etc.).
➢ For example, suppose that the parameter to such a macro instruction is named &ID.
The body of the macro definition contain a statement like LDA X&ID1, which means
&ID is concatenated after the string “X” and before the string “1”

9
➢ Ambiguity Problem: In the statement LDA X&ID1, & is the starting character of the macro
parameter; but the end of the parameter is not marked.
➢ So X&ID1 may mean:
X + &ID + 1 or X +&ID1
➢ Most of the macro processors deal with this problem by providing a special concatenatio n
operator ➔ to specify the end of parameter.
➢ Thus LDA X&ID1 can be rewritten as X&ID ➔1 . So that end of the parameter &ID is
clearly defined. Example:

➢ The example given above shows a macro definition that uses the concatenatio n operator as
previously described. The statement TOTAL A and TOTAL BETA shows the invocatio n
statements and the corresponding macro expansion.
➢ The macro-processor deletes all occurrences of the concatenation operator immedia te ly
after performing parameter substitution, so the symbol will not appear in the macro
expansion.

2. Generation of unique labels

➢ It is not possible to use labels for the instructions in the macro definition, since every
expansion of macro would include the label repeatedly.

10
➢ This results in duplicate labels problem if macro is invoked and expanded multip le
times. (which is not allowed by the assembler).
➢ To avoid this, we can use the technique of generating unique labels for every macro
invocation and expansion.
➢ During macro expansion each $ will be replaced with $XX, where XX is a two-
character alphanumeric counter of the number of macro instructions expansion.
➢ For the first macro expansion in a program, XX will have the value AA. For succeeding
macro expansions XX will be set to AB, AC etc. This allows 1296 macro expansions
in a single program
➢ Consider the following program:

11
In the example, for the first invocation of COPY X, Y the label generated is $AALOOP and for
the second invocation COPY P, Q the label generated is $ABLOOP. Thus, for each invocation of
macro unique label is generated.

3. Conditional Macro Expansion

Arguments in macro invocation can be used to:

➢ Substitute the parameters in the macro body without changing the sequence of statements
expanded.(sequential macro expansion).
➢ Modify the sequence of statements for conditional macro expansion. This capability
increases power and flexibility of a macro language.

Macro-Time Variables:

➢ Macro-time variables (often called as SET Symbol) can be used to store working values
during the macro expansion.
➢ It can also be used to store the evaluation result of Boolean expression
➢ Any symbol that begins with & and not a macro instruction parameter is considered as
macro-time variable. All such variables are initialized to zero.
➢ The value of macro-time variables can be modified by using the macro processor directive
SET
➢ The macro processor must maintain a symbol table to store the value of all macro-time
variables used. The table is used to look up the current value of the macro-time variable
whenever it is required

12
4. Keyword Macro Parameters

The parameters used in macro can be of two types

a) Positional Parameters
b) Keyword Parameters

a) Positional Parameters

Parameters and arguments are associated according to their positions in the macro prototype and
invocation. The programmer must specify the arguments in proper order.

Syntax : In macro definition,

macroname MACRO &parameter1, &parameter2,……

In macro invocation,

Macroname argument1, argument2,….

Example: In macro definition,

EVAL MACRO &X, &Y

In macro invocation,

EVAL P, Q

Here &X receives the value of P and &Y receives the value of Q

➢ If an argument is to be omitted, a null argument should be used to maintain the proper order
in macro invocation statement.
➢ For example: Suppose a macro named EVAL has 5 possible parameters, but in a particular
invocation of the macro only the 1st and 4th parameters are to be specified. Then the macro
call will be EVAL P,,,S,
➢ Positional parameter is not suitable if a macro has a large number of parameters, and only a
few of these are given values in a typical invocation.

13
b) Keyword Parameters

➢ Each argument value is written with a keyword that names the corresponding
parameter.
➢ Arguments may appear in any order. That is not any importance to the position of
arguments.
➢ Null arguments no longer need to be used.
➢ For macros using keyword parameters the macro prototype statement specification is
different. Here each parameter name is followed by equal sign, which identifies a
keyword parameter and a default value is specified for some of the parameters.
➢ Keyword parameters are easier to read and much less error-prone than the positiona l
parameters.
➢ It is of the form
&formal parameter name = <ordinary string>
➢ Consider the example:

14

You might also like