Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
47 views25 pages

Macro Processor: (Type Text)

The document discusses macro processors and their functions. It describes how macros allow shorthand representations of code and are expanded during assembly. It covers macro definition, invocation, and expansion. It also discusses the data structures and algorithms used in a one-pass macro processor, as well as extended features like concatenating parameters and generating unique labels.

Uploaded by

gjaj
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)
47 views25 pages

Macro Processor: (Type Text)

The document discusses macro processors and their functions. It describes how macros allow shorthand representations of code and are expanded during assembly. It covers macro definition, invocation, and expansion. It also discusses the data structures and algorithms used in a one-pass macro processor, as well as extended features like concatenating parameters and generating unique labels.

Uploaded by

gjaj
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/ 25

MODULE 5

MACRO PROCESSOR
A Macro represents a commonly used group of statements in the source programming
language.

 A macro instruction (macro) is a notational convenience for theprogrammer


o It allows the programmer to write shorthand version of a program (module
programming)
 The macro processor replaces each macro instruction with the corresponding group of
source language statements(expanding)
o Normally, it performs no analysis of the text ithandles.
o It does not concern the meaning of the involved statements during macro
expansion.
 The design of a macro processor generally is machineindependent!
 Two new assembler directives are used in macrodefinition
o MACRO: identify the beginning of a macrodefinition
o MEND: identify the end of a macrodefinition
 Prototype for the macro
o Each parameter begins with‘&’
 name MACRO parameters
:

body

MEND

o Body: the statements that will be generated as the expansion of the macro.

[Type text]
5.1 Basic Macro Processor Functions:

 Macro Definition andExpansion


 Macro Processor Algorithms and Datastructures

5.1.1 Macro Definition andExpansion:

 Consider the example of an SIC/XE program using macro instructions.This program defines
and uses two macro instructions , RDBUFF and WRBUFF.

 The functions and logic of RDBUFF macro are similar to RDREC subroutine.

[Type text]
 Two new assembler directives (Macro and MEND) are used in macro definitions. The keyword
macro identifies the beginning of the macro definition. The symbol in the label field (RDBUFF)
is the name of the macro and entries in the operand field identify the parameters of the macro.
Each parameter begins with the character & which helps in the substitution of parameters
during macro expansion. Following the macro directive are the statements that make up the
body of the macro definition. These are the statements that will be generated as the expansion
of the macro. The MEND directive marks the end of the macro.
 Macro invocation or call is written in the main program. In macro invocation the name of the
macro is followed by the arguments. Output of the macroprocessor is the expanded program.

[Type text]
Expanded Program

 Another simple example is given below:


 Program with macro
EX1 MACRO &A,&B

LDA &A

STA &B

MEND

SAMPLE START 1000

EX1 N1,N2

N1 RESW 1

N2 RESW 1

END

[Type text]
Expanded program

SAMPLE START 1000

. EX1 N1,N2
LDA N1
STA N2
N1 RESW 1
N2 RESW 1

Macro expansion

 Macro definition statements have been deleted since they are no longer required after the macros
are expanded. Each macro invocation statement has been expanded into the statements that form
the body of the macro with the arguments from the macro invocation is substituted for the
parameters in the macro definition. Macro invocation statement is included as a comment line in
the expanded program.

 After macroprocessing the expanded file can be used as input to the assembler.

 Differences between macro and subroutine: The statements that form the expansion of a macro are
generated and (assembled ) each time the macro is invoked. Statements in a subroutine appear only
once, regardless of how many time the subroutine is called.

[Type text]
5.1.2 Macro Processor Algorithm and DataStructure:

 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 the second pass.

 But such a two pass macro processor would not allow the body of one macro instruction to contain
definitions of other macros.

 Here defining MACROS does not define RDBUFF and WRBUFF. These definitions are processed
only when an invocation of MACROS is expanded.
 A one pass macro processor that can alternate between macro definition and macro expansion is
able to handle these type of macros.
 There are 3 main data structures:-

Dept. of CSE, CCE


- DEFTAB- The macro definitions are stored in a definition table(DEFTAB) which contain the
macro definition and the statements that form the macro body. References to the macro
instruction parameters are converted to positional notation.
- NAMTAB- Macro names are entered 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- is used during the expansion of the macro invocation. When a macro invocation
statement is recognized the arguments are stored in argument table. As the macro is expanded
arguments from ARGTAB are substituted for the corresponding parameters in the macro body.
- Eg

Dept. of CSE, CCE


Macro processor algorithm

Dept. of CSE, CCE


Dept. of CSE, CCE
 Procedure DEFINE which is called when the beginning of a macro definition is recognized makes
the appropriate entries in DEFTAB and NAMTAB.
 EXPAND is called to set up the argument values in ARGTAB and expand a Macro Invocation statement.
 Procedure GETLINE is called to get the next line to be processed either from the DEFTAB or from the
input file.
 Handling of macro definition within macro:- When a macro definition is encountered it is entered
in the DEFTAB. The normal approach is to continue entering till MEND is encountered. If there is a
program having a Macro defined within anotherMacro.While defining in the DEFTAB the very first
MEND is taken as the end of the Macro definition. This does not complete the definition as there is
another outer Macro which completes the definition of Macro as a whole. Therefore the DEFINE
procedure keeps a counter variable LEVEL.Every time a Macro directive is encountered this counter
is incremented by 1. The moment the innermost Macro ends indicated by the directive MEND it starts
decreasing the value of the counter variable by one. The last MEND should make the counter value
set to zero. So when LEVEL becomes zero, the MEND corresponds to the original MACRO
directive.

Dept. of CSE, CCE


5.3Machine-independent Macro-ProcessorFeatures.

The design of macro processor doesn’t depend on the architecture of the machine. We will be
studying some extended feature for this macro processor. These featuresare:

 Concatenation of MacroParameters
 Generation of uniquelabels
 Conditional MacroExpansion
 Keyword MacroParameters

5.3.1Concatenation 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,…, 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
macroinstruction.
 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.).

Dept. of CSE, CCE


 Suppose that the parameter to such a macro instruction is named &ID. The body of the
macro definition might contain a statementlike
 LDA X&ID1
 & is the starting character of the macro instruction; but the end of the parameter is not marked.
So in the case of &ID1, the macro processor could deduce the meaning that was intended.

 If the macro definition contains &ID and &ID1 as parameters, the situation would be
unavoidablyambiguous.
 Most of the macro processors deal with this problem by providing a special
concatenation operator. In the SIC macro language, this operator is the character .
Thus thestatement LDA X&ID1 can be writtenas
LDA X&ID

Dept. of CSE, CCE


 The above figure shows a macro definition that uses the concatenation operator as previously
described. The statement SUM A and SUM BETA shows the invocation statements and the
corresponding macro expansion.

5.3.2Generation of UniqueLabels

 it is not possible to use labels for the instructions in the macro definition, since every
expansion of macro would include the label repeatedly which is not allowed by theassembler.
 We can use the technique of generating unique labels for every macro invocation
andexpansion.
 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 example,

XX = AA, AB, AC…

This allows 1296 macro expansions in a single program.

The following program shows the macro definition with labels to the instruction.

The following figure shows the macro invocation and expansion first time.
 If the macro is invoked second time the labels may be expanded as $ABLOOP $ABEXIT.

5.3.3Conditional MacroExpansion
o IF ELSE
o WHILE loop
 We can modify the sequence of statements generated for a macro expansion depending on
conditions.
IF ELSE ENDIF structure
 Consider the following example.
 Here the definition of RDBUFF has two additional parameters. &EOR(end of record )
&MAXLTH(maximum length of the record that can be read)
 The macro processor directive SET – The statement assigns a value 1 to &EORCK and
&EORCK is known as macrotime variable. A macrotime variable is used to store working
values during the macro expansion. Any symbol that begins with & and that is not a macro
instruction parameter is assumed to be a macro time variable. All such variables are initialized to
a value 0.
 Implementation of Conditional macro expansion- Macro processor maintains a symbol table that
contains the values of all macrotime variables used. Entries in this table are made when SET
statements are processed. The table is used to look up the current value of the variable.
 Testing of Boolean expression in IF statement occurs at the time macros are expanded. By the
time the program is assembled all such decisions are made and conditional macro instruction
directives are removed.
 IF statements are different from COMPR which test data values during program expansion.

Looping-WHILE

 Consider the following example.


 Here the programmer can specify a list of end of record characters.

 In the macro invocation statement there is a list(00,03,04) corresponding to the parameter &EOR.
Any one of these characters is to be considered as end of record.

 The WHILE statement specifies that the following lines until the next ENDW are to be generated
repeatedly as long as the condition is true.

 The testing of these condition and the looping are done while the macro is being expanded.The
conditions do not contain any runtime values.

 %NITEMS is a macroprocessor function that returns as its value the number of members in an
argument list. Here it has the value 3. The value of &CTR is used as a subscript to select the
proper member of the list for each iteration of the loop. &EOR[&CTR] takes the values 00,03,04
.

 Implementation- When a WHILE statement is encountered during a macro expansion the


specified Boolean expression is evaluated , if the value is false the macroprocessor skips ahead in
DEFTAB until it finds the ENDW and then resumes normal macro expansion(not at run time).

5.3.4Keyword MacroParameters
 All the macro instruction definitions used positional parameters. Parameters and
arguments are matched according to their positions in the macro prototype and the
macro invocationstatement.

 The programmer needs to be careful while specifying the arguments. If an argument


is to be omitted the macro invocation statement must contain a null argument
mentioned with twocommas.

 Positional parameters are suitable for the macro invocation. But if the macro
invocation has large number of parameters, and if only few of the values need to be
used in a typical invocation, a different type of parameter specification is required.
 Eg: Consider the macro GENER which has 10 parameters, but in a particular
invocation of a macro only the third and nineth parameters are to be specified. If
positional parameters are used the macro invocation will look like
GENER , , DIRECT, , , , , , 3,
 But using keyword parameters this problem can be solved. We can write
GENER TYPE=DIRECT, CHANNEL=3
Keyword parameters

 Each argument value is written with a keyword that names the corresponding
parameter.
 Arguments may appear in anyorder.
 Null arguments no longer need to beused.
 It is easier to read and much less error-prone than the positionalmethod.

5.4 Macro Processor DesignOptions


5.4.1Recursive MacroExpansion

 We have seen an example of the definition of one macro instruction by another. But we have
not dealt with the invocation of one macro by another. The following example shows the
invocation of one macro by another macro.
Problem of Recursive Expansion

 Previous macro processor design cannot handle such kind of recursive macro
invocation andexpansion
o The procedure EXPAND would be called recursively, thus the invocation
arguments in the ARGTAB will beoverwritten.
o The Boolean variable EXPANDING would be set to FALSE when the “inner”
macro expansion is finished, i.e., the macro process would forget that it had
been in the middle of expanding an “outer”macro.

The procedure EXPAND would be called when the macro was recognized. The arguments from
the macro invocation would be entered into ARGTAB asfollows:

Parameter Value
1 BUFFER
2 LENGTH
3 F1
4 (unused)
- -

The Boolean variable EXPANDING would be set to TRUE, and expansion of the macro
invocation statement would begin. The processing would proceed normally until statement
invoking RDCHAR is processed. This time, ARGTAB would looklike

Value
Parameter
1 F1
2 (Unused)
-- --

At the expansion, when the end of RDCHAR is recognized, EXPANDING would be


set to FALSE. Thus the macro processor would ‘forget’ that it had been in the middle of
expanding a macro when it encountered the RDCHAR statement. In addition, the arguments
from the original macro invocation (RDBUFF) would be lost because the value in ARGTAB
was overwritten with the arguments from the invocation of RDCHAR.

 Solutions
o Write the macro processor in a programming language that allows recursive
calls, thus local variables will be retained.
o If you are writing in a language without recursion support, use a stack to take
care of pushing and popping local variables and returnaddresses.

5.4.2General-Purpose MacroProcessors

 Macro processors that do not dependent on any particular programming language,but


can be used with a variety of differentlanguages
 Pros
o Programmers do not need to learn many macrolanguages.
o Although its development costs are somewhat greater than those for a
language specific macro processor, this expense does not need to berepeated
for each language, thus save substantial overallcost.
 Cons
o Large number of details must be dealt with in a real programminglanguage
 Situations in which normal macro parameter substitution should not
occur, e.g.,comments.
 Facilities for grouping together terms, expressions, orstatements. Eg: some
languages use begin and end . Some use { and }
 Tokens, e.g., identifiers, constants, operators,keywords
 Syntax used for macro definition and macro invocation statement is
different.
5.4.3Macro Processing within LanguageTranslators

 The macro processors we discussed are called“Preprocessors”.


o Process macrodefinitions
o Expand macroinvocations
o Produce an expanded version of the source program, which is then used asinput
to an assembler orcompiler
 You may also combine the macro processing functions with the languagetranslator:
o Line-by-line macroprocessor
o Integrated macroprocessor

Line-by-Line Macro Processor


 Used as a sort of input routine for the assembler orcompiler
o Read sourceprogram
o Process macro definitions and expand macroinvocations
o Pass output lines to the assembler orcompiler
 Benefits
o Avoid making an extra pass over the sourceprogram.
o Data structures required by the macro processor and the language translatorcan be
combined (e.g., OPTAB andNAMTAB)
o Utility subroutines can be used by both macro processor and thelanguage
translator.
 Scanning inputlines
 Searchingtables
 Data formatconversion
o It is easier to give diagnostic messages related to the sourcestatements

Integrated Macro Processor


 An integrated macro processor can potentially make use of any information aboutthe source
program that is extracted by the languagetranslator.
o Ex (blanks are not significant inFORTRAN)
 DO 100 I =1,20
 a DOstatement
 DO 100 I =1
 An assignmentstatement
 DO100I: variable (blanks are not significant inFORTRAN)
 An integrated macro processor can support macro instructions that depend uponthe context
in which theyoccur.

 Disadvantages- They must be specially designed and written to work with a particular
implementation of an assembler or compiler.. Cost of development is high.
25

You might also like