SlideShare a Scribd company logo
1 of 26
Download to read offline
CS303 System Software Module 5
Page 1
MODULE 5
SYLLABUS
Macro Preprocessor: Macro Instruction Definition and Expansion, One pass
Macro processor Algorithm and data structures, Machine Independent Macro
Processor Features, Macro processor design options
5.1 MACRO PREPROCESSOR
Macro
 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
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.
CS303 System Software Module 5
Page 2
5.2 BASIC MACRO PROCESSOR FUNCTIONS
The fundamental functions common to all macro processors are: ( Code to remember - DIE )
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)
 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.
CS303 System Software Module 5
Page 3
 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.
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 individual
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
CS303 System Software Module 5
Page 4
LDA &Q
ADD B
MEND
Again the same macro is invoked with the macro call SUM M,N and the macro is expanded
as
LDA &M
MOV B
LDA &N
ADD B
MEND
Figure: Example for macro expansion
Difference between Macro and Subroutine
Macros differ from subroutines in two fundamental aspects:
1. A macro call leads to its expansion, whereas subroutine call leads to its execution. So
there is difference in size and execution efficiency.
2. Statements of the macro body are expanded each time the macro is invoked. But the
statements of the subroutine appear only once, regardless of how many times the
subroutine is called.
CS303 System Software Module 5
Page 5
5.3 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.
 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 type of macro are called nested macros.
SWAP MACRO &X,&Y
LDA &X
LDX &Y
STORE MACRO &X,&Y
STA &Y
STX &X
MEND
MEND
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
Inner macro
outer macro
CS303 System Software Module 5
Page 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
SUM MACRO &X,&Y
LDA &X
MOV B
LDA &Y
ADD B
MEND
START
LDA 4500
ADD B
SUM P,Q
LDA 3000
………….
END
CS303 System Software Module 5
Page 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 shows the different data structures used
CS303 System Software Module 5
Page 8
Algorithm for one pass macro processor
CS303 System Software Module 5
Page 9
Explanation of algorithm
 The algorithm uses 5 procedures
o MACROPROCESSOR (main function)
o DEFINE
o EXPAND
o PROCESSLINE
o GETLINE
MACROPROCESSOR (MAIN function)
 This function initialize the variable named EXPANDING to false.
 It then calls GETLINE procedure to get next line from the source program and
PROCESSLINE procedure to process that line.
 This process will continue until the END of program.
CS303 System Software Module 5
Page 10
PROCESSLINE
 This procedure checks
o If the opcode of current statement is present in NAMTAB. If so it is a macro
invocation statement and calls the procedure EXPAND
o Else if opcode =MACRO, then it indicates the beginning of a macro definition
and calls the procedure DEFINE
o Else it is identified as a normal statement(not a macro definition or macro
call) and write it to the output file.
DEFINE
 The control will reach in this procedure if and only if it is identified as a macro
definition statement.Then:
o Macro name is entered into NAMTAB
o Then the macro name along with its parameters are entered into DEFTAB.
o The statements in body of macro is also enterd into DEFTAB. References to
the macro instruction parameters are converted to a positional notation for
efficiency in substituting arguments.
o Comment lines from macro definition are not entered into DEFTAB because
they will not be a part of macro expansion.
o Store in NAMTAB the pointers to beginning and end of definition in
DEFTAB.
 To deal with Nested macro definitions DEFINE procedure maintains a counter named
LEVEL.
o When the assembler directive MACRO is read, the value of LEVEL is
incremented by 1
o When MEND directive is read, the value of LEVEL is decremented by 1
o That is, whenever a new macro definition is encountered within the current
definition, the value of LEVEL will be incremented and the while loop which
is used to process the macro definition will terminate only after the value of
LEVEL =0. With this we can ensure the nested macro definitions are properly
handled.
EXPAND
 The control will reach in this procedure if and only if it is identified as a macro call.
CS303 System Software Module 5
Page 11
 In this procedure, the variable EXPANDING is set to true. It actually indicates the
GETLINE procedure that it is going to expand the macro call. So that GETLINE
procedure will read the next line from DEFTAB instead of reading from input file.
 The arguments of macro call are entered into ARGTAB.
 The macro call is expanded with the lines from the DEFTAB. When the ?n notation is
recognized in a line from DEFTAB, the corresponding argument is taken from
ARGTAB.
GETLINE
 This procedure is used to get the next line.
 If EXPANDING = TRUE, the next line is fetched from DEFTAB. (It means we are
expanding the macro call)
 If EXPANDING = False, the next line is read from input file.
Flow Diagram of a one pass macroprocessor
CS303 System Software Module 5
Page 12
5.4 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”
TOTAL MACRO &ID
LDA X&ID1
ADD X&ID2
STA X&ID3
MEND
The macro call TOTAL A will be expanded as:
LDA XA1
ADD XA2
STA XA3
CS303 System Software Module 5
Page 13
 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
concatenation operator to specify the end of parameter.
 Thus LDA X&ID1 can be rewritten as . So that end of the parameter
&ID is clearly defined.
 Example:
 The example given above shows a macro definition that uses the concatenation
operator as previously described. The statement TOTAL A and TOTAL BETA shows
the invocation statements and the corresponding macro expansion.
 The macroprocessor deletes all occurrences of the concatenation operator
immediately 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.
 This results in duplicate labels problem if macro is invoked and expanded multiple
times.(which is not allowed by the assembler)
CS303 System Software Module 5
Page 14
 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:
SAMPLE START 0
COPY MACRO &A,&B
LDA &A
.........
$LOOP ADD &B
..........
JLE $LOOP
STA &B
MEND
.............
COPY X,Y
LDA M
COPY P,Q
...........
END
After the macro expansion above code becomes:
SAMPLE START 0
.............
LDA &X
.........
$AALOOP ADD &Y Expansion of COPY X,Y
..........
JLE $AALOOP
STA &Y
CS303 System Software Module 5
Page 15
LDA M
LDA &P
.........
$ABLOOP ADD &P
.......... Expansion of COPY P,Q
JLE $ABLOOP
STA &Q
...........
END
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:
o Substitute the parameters in the macro body without changing the sequence of
statements expanded.(sequential macro expansion)
o Modify the sequence of statements for conditional macro expansion. This
capability increases power and flexibility of a macro language.
 Macro-Time Variables:
o Macro-time variables (often called as SET Symbol) can be used to store
working values during the macro expansion.
o It can also be used to store the evaluation result of Boolean expression
o Any symbol that begins with & and not a macro instruction parameter is
considered as macro-time variable. All such variables are initialized to zero.
o The value of macro-time variables can be modified by using the macro
processor directive SET
o 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.
Implementation of Macro-Time Conditional Structure IF-ELSE-ENDIF
 Structure of IF-ELSE_ENDIF:
CS303 System Software Module 5
Page 16
Macroname MACRO &COND
........
IF (&COND NE ’’)
.part I
ELSE
.part II
ENDIF
.........
MEND
 When an IF statement is encountered during the expansion of a macro, the specified
Boolean expression is evaluated.
 If the value of this expression TRUE,
o The macro processor continues to process lines from the DEFTAB until it
encounters the ELSE or ENDIF statement.
o If an ELSE is found, macro processor skips lines in DEFTAB until the next
ENDIF.
o Once it reaches ENDIF, it resumes expanding the macro in the usual way.
 If the value of the expression is FALSE,
o The macro processor skips ahead in DEFTAB until it encounters next ELSE
or ENDIF statement.
o The macro processor then resumes normal macro expansion.
 Example for conditional macro:
COPY START 0
EVAL MACRO &X,&Y,&Z
IF (&Y LE &X)
LDA &X
SUB &Z
ELSE
LDA &Y
ADD &Z
ENDIF
MEND
STA P
....................
CS303 System Software Module 5
Page 17
EVAL 2 ,3 ,4
STA Q
...............
END
After expansion the above code becomes:
COPY START 0
STA P
...................
LDA 3
ADD 4
STA Q
.........................
END
Implementation of Macro-time looping(or Expansion time looping) structure: WHILE-ENDW
 WHILE statement specifies that the following lines until the next ENDW statement,
are to be generated repeatedly as long as a particular condition is true. The testing
of this condition, and the looping are done during the macro under expansion.
 When a WHILE statement is encountered during the expansion of a macro, the
specified Boolean expression is evaluated.
 If expression is TRUE, the macro processor continues to process lines from
DEFTAB until it encounters the next ENDW statement.When ENDW is
encountered, the macro processor returns to the preceding WHILE, re-evaluates the
Boolean expression, and takes action based on the new value.
 If the expression is FALSE, the macro processor skips ahead in DEFTAB until it
finds the next ENDW statement and then resumes normal macro expansion.
 The example given below shows the usage of Macro-Time Looping
statement. In this example, the variable &LIMIT is a macro time variable.
CS303 System Software Module 5
Page 18
Above loop after expansion
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 recieves the value of P and &Y recieves 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,
CS303 System Software Module 5
Page 19
 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.
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
positional parameters.
 It is of the form
&formal parameter name = <ordinary string>
 Consider the example:
INCR MACRO &X=, &Y=, &Z=
MOV &Z, &X
ADD &Z, &Y
MOV &Z, &X
MEND
The following calls are now equivalent
1) INCR X=A, Y=B, Z=C
2) INCR Y=B, X= A, Z= C
Default specification of parameters
 Default specification of parameters can also be possible in macros.
 This specification is useful in situations where a parameter has the same value in most
calls.
 When the desired value is different from the default value, the desired value can be
specified explicitly in a macro call.
CS303 System Software Module 5
Page 20
 This specification overrides the default value of the parameter for the duration of that
macro call.
 Consider the example:
INCR MACRO &X=, &Y=, &Z=R
MOV &Z, &X
ADD &Z, &Y
MOV &Z, &X
MEND
 Then the macro call INCR X=A, Y=B will take the values A for parameter X, B for
parameter Y and R for the parameter Z.
 The macro call INCR X=A, Y=B, Z=C will take the values A for parameter X, B for
parameter Y and C for the parameter Z.
5.5 MACRO PROCESSOR DESIGN OPTIONS
5.5.1 Two Pass Macro Processor
 Same as on page 4
 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.
 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 type
of macro are called nested macros.
Here the default value R is
specified for the parameter Z
CS303 System Software Module 5
Page 21
SWAP MACRO &X,&Y
LDA &X
LDX &Y
STORE MACRO &X,&Y
STA &Y
STX &X
MEND
MEND
5.5.2 One Pass Macro Processor
 Same as on page 4 (here only brief description is needed)
 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
DEFTAB
NAMTAB
ARGTAB
 Whenever a macro definition is encountered, the macro prototype and body of macro
is entered into DEFTAB. References to the macro instruction parameters are
converted to a positional notation for efficiency in substituting arguments.
 The macro name along with the begin and end pointers are entered into NAMTAB.
 Whenever a macro invocation is encountered, the arguments are entered into
ARGTAB.
 The macro call is expanded with the lines from the DEFTAB. When the ?n notation is
recognized in a line from DEFTAB, the corresponding argument is taken from
ARGTAB.
5.5.3 Recursive Macro Expansion
 Invocation of one macro by another macro is called recursive macro.
 Example for recursive macro:
Inner macro
outer macro
CS303 System Software Module 5
Page 22
SUM MACRO &X,&Y
STA &X
ADD &Y
MEND
INPUT MACRO &A,&B
SUM &A,&B .i n v o k i n g the macro SUM
MEND
Here the macro named INPUT is calling another macro named SUM. This is called as
recursive macro.
 The macro processor design algorithm discussed previously cannot handle recursive
macro invocation and expansion.
 Reasons are:
o The procedure EXPAND would be called recursively, thus the invocation
arguments in the ARGTAB will be overwritten.
o The Boolean variable EXPANDING would be set to FALSE when the “inner”
macro expansion is finished, that is, the macro process would forget that it had
been in the middle of expanding an “outer” macro.
o A similar problem would occur with PROCESSLINE since this procedure too
would be called recursively.
 Solutions:
o Write the macro processor in a programming language that allows recursive calls,
thus local variables will be retained.
o If we are writing in a language without recursion support, use a stack to take care of
pushing and popping local variables and return addresses. So the recursive calls can
be handled.
5.5.4 General-Purpose Macro Processors
 The macro processor we discussed so far is related to assembly language programming.
Macro processor for high level languages have also been developed. Macro processors
which are designed for a specific language are called special purpose macro processors.
Example for special purpose macro processor is MASM Macro processor
 These special purpose macro processors are similar in general function and approach but
the implementation details differ from language to language.
CS303 System Software Module 5
Page 23
 The general purpose macro processor do not dependent on any particular programming
language, but can be used with a variety of different languages.
 Example for a general purpose macro processor is ELENA Macro processor
Advantages
 Programmers do not need to learn many macro languages, so much of the time and
expense involved in training are eliminated.
 Although its development costs are somewhat greater than those for a specific language
macro processor, this expense does not need to be repeated for each language, thus save
substantial overall cost.
Disadvantages
In spite of the advantages noted, there are still relatively few general purpose macro
processors. The reasons are:
 Large number of details must be dealt with in a real programming language.
 There are several situations in which normal macro parameter substitution or normal
macro expansion should not occur.
o For example, comments are usually ignored by the macro processor. But each
programming languages uses its own method for specifying comments. So a
general purpose macro processor should be designed with the capability for
identifying the comments in any programming languages.
 Another problem is with the facilities for grouping together terms, expressions, or
statements.
o Some languages use keywords such as begin and end for grouping statements
while some other languages uses special characters like { }. A general purpose
macro processor may need to take these groupings into account in scanning the
source statements.
 Another problem is with the identification of tokens of the programming languages. The
tokens means the identifiers, constants, operators and keywords in the programming
language. Different languages uses different rules for the formation of tokens. So the
design of a general purpose macro processor must take this into consideration.
 Another problem is with the syntax used for macro definitions and macro invocation
statements.
CS303 System Software Module 5
Page 24
5.5.5 Macro Processing within Language Translators
 Macros can be processed
1. Outside the language translators
2. Within the Language translators
1.Macro processing outside the language translators
 The macro processors that we had discussed so far belongs to this group. They are
called macro preprocessors.
 They reads the source program statements, process the macro definitions and expand
macro invocations, producing an expanded version of the source program.
 This expanded program is then used as input to an assembler or compiler.
 So this can be considered as macro processing outside the language translators.
2.Macro processing within the language translators
 In this section we discuss the methods for combining the macro processing functions
with the language translator itself.
 Two common methods are
a) Line –by- Line Macro Processors
b) Integrated Macro Processors
a) Line-by-line macro processor
 The simplest method for combining the macro processing functions with the
language translator is a line-by-line approach.
 Using this approach, the macro processor reads the source program statement, process
the macro definitions and expand macro invocations. But it does not produce an
expanded version of source program.
 The processed lines are passed to the language translator(compiler or assembler) as
they are generated, instead of being written to an expanded source file.
 Thus macro processor operates as a sort of input routine for the assembler or
compiler.
Benefits of line –by-line macro processor
 It avoids making an extra pass over the source program. So it can be more efficient
than using a macro preprocessor.
CS303 System Software Module 5
Page 25
 Some of the data structures required by the macro processor and the language
translator can be combined (e.g., OPTAB and NAMTAB)
 Many utility subroutines can be used by both macro processor and the language
translator. This involves scanning input lines, searching tables , converting numeric
values to internal representation etc.
 It is easier to give diagnostic messages related to the source statements.
b) Integrated Macro Processors
 Although a line-by-line macro processor may use some of the same utility routines as
language translator, the functions of macro processing and program translation are
still relatively independent.
 It is possible to have even closer cooperation between the macro processor and the
language translator. Such a scheme is called as language translator with an integrated
macro processor.
 An integrated macro processor can potentially make use of any information about
the source program that is extracted by the language translator.
 The macro processor may use the results of the translator operations such as scanning
of symbols, constants, etc without involved in processing it.
 For example in FORTRAN language, consider the statement:
DO 100 I = 1,20
where DO is a keyword, 100 is statement number, I is a variable name etc.
DO 100 I = 1
since in FORTRAN blanks are not significant, this statement is an assignment
that gives the value 1 to the variable DO100I. Thus the proper interpretation
of characters cannot be decided until the rest of the statement is examined.
Disadvantages of line-by-line and integrated macro processors
 They must be specially designed and written to work with a particular implementation
of an assembler or compiler. The cost of macro processor development is added to the
costs of the language translator, which results in a more expensive software. The
assembler or compiler will be considerably larger and more complex than using a
macro preprocessor. Size may be a problem if the translator is to run on a computer
with limited memory.
CS303 System Software Module 5
Page 26
5.6 TYPES OF MACRO
Different types of macro are 1. Parameterized Macro 2. Nested Macro 3. Recursive Macro
1. Parameterized Macro
 Macros which uses parameters are called parameterized macro.This type of macro has
the capability to insert the given parameters into its expansion.Macros we studied so
far belongs to this type.
 Example:
SUM MACRO &X,&Y
LDA &X
MOV B
LDA &Y
ADD B
MEND
2. Nested Macros
 Nested macros are macros in which definition of one macro contains definition of
other macros.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 type of macro are called nested macros.
SWAP MACRO &X,&Y
LDA &X
LDX &Y
STORE MACRO &X,&Y
STA &Y
STX &X
MEND
MEND
3. Recursive Macro
 Invocation of one macro by another macro is called recursive macro.Example for
recursive macro:
SUM MACRO &X,&Y
STA &X
ADD &Y
MEND
INPUT MACRO &A,&B
SUM &A,&B .i n v o k i n g the macro SUM
MEND
Here the macro named INPUT is calling another macro named SUM. This is called as
recursive macro.
Inner macro
outer macro

More Related Content

What's hot

09 sd capitulo vii circuitos logicos combinacionales
09 sd capitulo vii circuitos logicos combinacionales09 sd capitulo vii circuitos logicos combinacionales
09 sd capitulo vii circuitos logicos combinacionalesErnesto Villalpando
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort AlgorithmTobias Straub
 
03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdfKkSingh64
 
AUTOMATIC TEMPERATURE FAN CONTROLLER
AUTOMATIC TEMPERATURE FAN CONTROLLERAUTOMATIC TEMPERATURE FAN CONTROLLER
AUTOMATIC TEMPERATURE FAN CONTROLLERMuhammadFazilMemon
 
Coa module2
Coa module2Coa module2
Coa module2cs19club
 
Instruction cycle presentation
Instruction   cycle presentationInstruction   cycle presentation
Instruction cycle presentationMoniba Irfan
 
Unit 3 – assembly language programming
Unit 3 – assembly language programmingUnit 3 – assembly language programming
Unit 3 – assembly language programmingKartik Sharma
 
Boolean algebra.pptx
Boolean algebra.pptxBoolean algebra.pptx
Boolean algebra.pptxMhhh7
 
Modes Of Transfer in Input/Output Organization
Modes Of Transfer in Input/Output OrganizationModes Of Transfer in Input/Output Organization
Modes Of Transfer in Input/Output OrganizationMOHIT AGARWAL
 
Magnitude Comparator and types of MC
Magnitude Comparator and types of MCMagnitude Comparator and types of MC
Magnitude Comparator and types of MCEasy n Inspire L
 
Microinstruction sequencing new
Microinstruction sequencing newMicroinstruction sequencing new
Microinstruction sequencing newMahesh Kumar Attri
 
Branch instructions in 8051 micrcocontroller
Branch instructions in 8051 micrcocontrollerBranch instructions in 8051 micrcocontroller
Branch instructions in 8051 micrcocontrollerUshaRani289
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086Mahalakshmiv11
 
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operationNikhil Pandit
 
Asynchronous Data Transfer.pptx
Asynchronous Data Transfer.pptxAsynchronous Data Transfer.pptx
Asynchronous Data Transfer.pptxArunaDevi63
 

What's hot (20)

09 sd capitulo vii circuitos logicos combinacionales
09 sd capitulo vii circuitos logicos combinacionales09 sd capitulo vii circuitos logicos combinacionales
09 sd capitulo vii circuitos logicos combinacionales
 
Bubblesort Algorithm
Bubblesort AlgorithmBubblesort Algorithm
Bubblesort Algorithm
 
03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf
 
AUTOMATIC TEMPERATURE FAN CONTROLLER
AUTOMATIC TEMPERATURE FAN CONTROLLERAUTOMATIC TEMPERATURE FAN CONTROLLER
AUTOMATIC TEMPERATURE FAN CONTROLLER
 
Coa module2
Coa module2Coa module2
Coa module2
 
Instruction cycle presentation
Instruction   cycle presentationInstruction   cycle presentation
Instruction cycle presentation
 
Unit 3 – assembly language programming
Unit 3 – assembly language programmingUnit 3 – assembly language programming
Unit 3 – assembly language programming
 
Boolean algebra.pptx
Boolean algebra.pptxBoolean algebra.pptx
Boolean algebra.pptx
 
MACRO PROCESSOR
MACRO PROCESSORMACRO PROCESSOR
MACRO PROCESSOR
 
Modes Of Transfer in Input/Output Organization
Modes Of Transfer in Input/Output OrganizationModes Of Transfer in Input/Output Organization
Modes Of Transfer in Input/Output Organization
 
Magnitude Comparator and types of MC
Magnitude Comparator and types of MCMagnitude Comparator and types of MC
Magnitude Comparator and types of MC
 
Microinstruction sequencing new
Microinstruction sequencing newMicroinstruction sequencing new
Microinstruction sequencing new
 
Risc and cisc
Risc and ciscRisc and cisc
Risc and cisc
 
Branch instructions in 8051 micrcocontroller
Branch instructions in 8051 micrcocontrollerBranch instructions in 8051 micrcocontroller
Branch instructions in 8051 micrcocontroller
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
 
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operation
 
Arithmetic circuits
Arithmetic circuitsArithmetic circuits
Arithmetic circuits
 
ARM Fundamentals
ARM FundamentalsARM Fundamentals
ARM Fundamentals
 
Privilege levels 80386
Privilege levels 80386Privilege levels 80386
Privilege levels 80386
 
Asynchronous Data Transfer.pptx
Asynchronous Data Transfer.pptxAsynchronous Data Transfer.pptx
Asynchronous Data Transfer.pptx
 

Similar to Module 5.pdf

33443223 system-software-unit-iv
33443223 system-software-unit-iv33443223 system-software-unit-iv
33443223 system-software-unit-ivShaniya Fathimuthu
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSARASWATHI S
 
Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1venkatam
 
Unit ii-111206004636-phpapp01
Unit ii-111206004636-phpapp01Unit ii-111206004636-phpapp01
Unit ii-111206004636-phpapp01riddhi viradiya
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfacsmadurai
 
Writing command macro in stratus cobol
Writing command macro in stratus cobolWriting command macro in stratus cobol
Writing command macro in stratus cobolSrinimf-Slides
 
BAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureBAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureWake Tech BAS
 
SAS Macros part 3
SAS Macros part 3SAS Macros part 3
SAS Macros part 3venkatam
 
Power of call symput data
Power of call symput dataPower of call symput data
Power of call symput dataYash Sharma
 
SAS Macros part 1
SAS Macros part 1SAS Macros part 1
SAS Macros part 1venkatam
 

Similar to Module 5.pdf (20)

33443223 system-software-unit-iv
33443223 system-software-unit-iv33443223 system-software-unit-iv
33443223 system-software-unit-iv
 
Unit 2
Unit 2Unit 2
Unit 2
 
handout6.pdf
handout6.pdfhandout6.pdf
handout6.pdf
 
Ss4
Ss4Ss4
Ss4
 
System software - macro expansion,nested macro calls
System software - macro expansion,nested macro callsSystem software - macro expansion,nested macro calls
System software - macro expansion,nested macro calls
 
Handout#04
Handout#04Handout#04
Handout#04
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
Handout#05
Handout#05Handout#05
Handout#05
 
Sas macros part 4.1
Sas macros part 4.1Sas macros part 4.1
Sas macros part 4.1
 
Macro-processor
Macro-processorMacro-processor
Macro-processor
 
Unit ii-111206004636-phpapp01
Unit ii-111206004636-phpapp01Unit ii-111206004636-phpapp01
Unit ii-111206004636-phpapp01
 
Task Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdfTask Perform addition subtraction division and multiplic.pdf
Task Perform addition subtraction division and multiplic.pdf
 
Macro Processor
Macro ProcessorMacro Processor
Macro Processor
 
Writing command macro in stratus cobol
Writing command macro in stratus cobolWriting command macro in stratus cobol
Writing command macro in stratus cobol
 
BAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 LectureBAS 150 Lesson 8 Lecture
BAS 150 Lesson 8 Lecture
 
Macro assembler
 Macro assembler Macro assembler
Macro assembler
 
SAS Macros part 3
SAS Macros part 3SAS Macros part 3
SAS Macros part 3
 
Power of call symput data
Power of call symput dataPower of call symput data
Power of call symput data
 
SAS Macro
SAS MacroSAS Macro
SAS Macro
 
SAS Macros part 1
SAS Macros part 1SAS Macros part 1
SAS Macros part 1
 

Recently uploaded

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 

Recently uploaded (20)

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 

Module 5.pdf

  • 1. CS303 System Software Module 5 Page 1 MODULE 5 SYLLABUS Macro Preprocessor: Macro Instruction Definition and Expansion, One pass Macro processor Algorithm and data structures, Machine Independent Macro Processor Features, Macro processor design options 5.1 MACRO PREPROCESSOR Macro  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 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.
  • 2. CS303 System Software Module 5 Page 2 5.2 BASIC MACRO PROCESSOR FUNCTIONS The fundamental functions common to all macro processors are: ( Code to remember - DIE ) 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)  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.
  • 3. CS303 System Software Module 5 Page 3  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. 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 individual 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
  • 4. CS303 System Software Module 5 Page 4 LDA &Q ADD B MEND Again the same macro is invoked with the macro call SUM M,N and the macro is expanded as LDA &M MOV B LDA &N ADD B MEND Figure: Example for macro expansion Difference between Macro and Subroutine Macros differ from subroutines in two fundamental aspects: 1. A macro call leads to its expansion, whereas subroutine call leads to its execution. So there is difference in size and execution efficiency. 2. Statements of the macro body are expanded each time the macro is invoked. But the statements of the subroutine appear only once, regardless of how many times the subroutine is called.
  • 5. CS303 System Software Module 5 Page 5 5.3 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.  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 type of macro are called nested macros. SWAP MACRO &X,&Y LDA &X LDX &Y STORE MACRO &X,&Y STA &Y STX &X MEND MEND 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 Inner macro outer macro
  • 6. CS303 System Software Module 5 Page 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 SUM MACRO &X,&Y LDA &X MOV B LDA &Y ADD B MEND START LDA 4500 ADD B SUM P,Q LDA 3000 …………. END
  • 7. CS303 System Software Module 5 Page 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 shows the different data structures used
  • 8. CS303 System Software Module 5 Page 8 Algorithm for one pass macro processor
  • 9. CS303 System Software Module 5 Page 9 Explanation of algorithm  The algorithm uses 5 procedures o MACROPROCESSOR (main function) o DEFINE o EXPAND o PROCESSLINE o GETLINE MACROPROCESSOR (MAIN function)  This function initialize the variable named EXPANDING to false.  It then calls GETLINE procedure to get next line from the source program and PROCESSLINE procedure to process that line.  This process will continue until the END of program.
  • 10. CS303 System Software Module 5 Page 10 PROCESSLINE  This procedure checks o If the opcode of current statement is present in NAMTAB. If so it is a macro invocation statement and calls the procedure EXPAND o Else if opcode =MACRO, then it indicates the beginning of a macro definition and calls the procedure DEFINE o Else it is identified as a normal statement(not a macro definition or macro call) and write it to the output file. DEFINE  The control will reach in this procedure if and only if it is identified as a macro definition statement.Then: o Macro name is entered into NAMTAB o Then the macro name along with its parameters are entered into DEFTAB. o The statements in body of macro is also enterd into DEFTAB. References to the macro instruction parameters are converted to a positional notation for efficiency in substituting arguments. o Comment lines from macro definition are not entered into DEFTAB because they will not be a part of macro expansion. o Store in NAMTAB the pointers to beginning and end of definition in DEFTAB.  To deal with Nested macro definitions DEFINE procedure maintains a counter named LEVEL. o When the assembler directive MACRO is read, the value of LEVEL is incremented by 1 o When MEND directive is read, the value of LEVEL is decremented by 1 o That is, whenever a new macro definition is encountered within the current definition, the value of LEVEL will be incremented and the while loop which is used to process the macro definition will terminate only after the value of LEVEL =0. With this we can ensure the nested macro definitions are properly handled. EXPAND  The control will reach in this procedure if and only if it is identified as a macro call.
  • 11. CS303 System Software Module 5 Page 11  In this procedure, the variable EXPANDING is set to true. It actually indicates the GETLINE procedure that it is going to expand the macro call. So that GETLINE procedure will read the next line from DEFTAB instead of reading from input file.  The arguments of macro call are entered into ARGTAB.  The macro call is expanded with the lines from the DEFTAB. When the ?n notation is recognized in a line from DEFTAB, the corresponding argument is taken from ARGTAB. GETLINE  This procedure is used to get the next line.  If EXPANDING = TRUE, the next line is fetched from DEFTAB. (It means we are expanding the macro call)  If EXPANDING = False, the next line is read from input file. Flow Diagram of a one pass macroprocessor
  • 12. CS303 System Software Module 5 Page 12 5.4 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” TOTAL MACRO &ID LDA X&ID1 ADD X&ID2 STA X&ID3 MEND The macro call TOTAL A will be expanded as: LDA XA1 ADD XA2 STA XA3
  • 13. CS303 System Software Module 5 Page 13  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 concatenation operator to specify the end of parameter.  Thus LDA X&ID1 can be rewritten as . So that end of the parameter &ID is clearly defined.  Example:  The example given above shows a macro definition that uses the concatenation operator as previously described. The statement TOTAL A and TOTAL BETA shows the invocation statements and the corresponding macro expansion.  The macroprocessor deletes all occurrences of the concatenation operator immediately 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.  This results in duplicate labels problem if macro is invoked and expanded multiple times.(which is not allowed by the assembler)
  • 14. CS303 System Software Module 5 Page 14  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: SAMPLE START 0 COPY MACRO &A,&B LDA &A ......... $LOOP ADD &B .......... JLE $LOOP STA &B MEND ............. COPY X,Y LDA M COPY P,Q ........... END After the macro expansion above code becomes: SAMPLE START 0 ............. LDA &X ......... $AALOOP ADD &Y Expansion of COPY X,Y .......... JLE $AALOOP STA &Y
  • 15. CS303 System Software Module 5 Page 15 LDA M LDA &P ......... $ABLOOP ADD &P .......... Expansion of COPY P,Q JLE $ABLOOP STA &Q ........... END 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: o Substitute the parameters in the macro body without changing the sequence of statements expanded.(sequential macro expansion) o Modify the sequence of statements for conditional macro expansion. This capability increases power and flexibility of a macro language.  Macro-Time Variables: o Macro-time variables (often called as SET Symbol) can be used to store working values during the macro expansion. o It can also be used to store the evaluation result of Boolean expression o Any symbol that begins with & and not a macro instruction parameter is considered as macro-time variable. All such variables are initialized to zero. o The value of macro-time variables can be modified by using the macro processor directive SET o 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. Implementation of Macro-Time Conditional Structure IF-ELSE-ENDIF  Structure of IF-ELSE_ENDIF:
  • 16. CS303 System Software Module 5 Page 16 Macroname MACRO &COND ........ IF (&COND NE ’’) .part I ELSE .part II ENDIF ......... MEND  When an IF statement is encountered during the expansion of a macro, the specified Boolean expression is evaluated.  If the value of this expression TRUE, o The macro processor continues to process lines from the DEFTAB until it encounters the ELSE or ENDIF statement. o If an ELSE is found, macro processor skips lines in DEFTAB until the next ENDIF. o Once it reaches ENDIF, it resumes expanding the macro in the usual way.  If the value of the expression is FALSE, o The macro processor skips ahead in DEFTAB until it encounters next ELSE or ENDIF statement. o The macro processor then resumes normal macro expansion.  Example for conditional macro: COPY START 0 EVAL MACRO &X,&Y,&Z IF (&Y LE &X) LDA &X SUB &Z ELSE LDA &Y ADD &Z ENDIF MEND STA P ....................
  • 17. CS303 System Software Module 5 Page 17 EVAL 2 ,3 ,4 STA Q ............... END After expansion the above code becomes: COPY START 0 STA P ................... LDA 3 ADD 4 STA Q ......................... END Implementation of Macro-time looping(or Expansion time looping) structure: WHILE-ENDW  WHILE statement specifies that the following lines until the next ENDW statement, are to be generated repeatedly as long as a particular condition is true. The testing of this condition, and the looping are done during the macro under expansion.  When a WHILE statement is encountered during the expansion of a macro, the specified Boolean expression is evaluated.  If expression is TRUE, the macro processor continues to process lines from DEFTAB until it encounters the next ENDW statement.When ENDW is encountered, the macro processor returns to the preceding WHILE, re-evaluates the Boolean expression, and takes action based on the new value.  If the expression is FALSE, the macro processor skips ahead in DEFTAB until it finds the next ENDW statement and then resumes normal macro expansion.  The example given below shows the usage of Macro-Time Looping statement. In this example, the variable &LIMIT is a macro time variable.
  • 18. CS303 System Software Module 5 Page 18 Above loop after expansion 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 recieves the value of P and &Y recieves 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,
  • 19. CS303 System Software Module 5 Page 19  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. 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 positional parameters.  It is of the form &formal parameter name = <ordinary string>  Consider the example: INCR MACRO &X=, &Y=, &Z= MOV &Z, &X ADD &Z, &Y MOV &Z, &X MEND The following calls are now equivalent 1) INCR X=A, Y=B, Z=C 2) INCR Y=B, X= A, Z= C Default specification of parameters  Default specification of parameters can also be possible in macros.  This specification is useful in situations where a parameter has the same value in most calls.  When the desired value is different from the default value, the desired value can be specified explicitly in a macro call.
  • 20. CS303 System Software Module 5 Page 20  This specification overrides the default value of the parameter for the duration of that macro call.  Consider the example: INCR MACRO &X=, &Y=, &Z=R MOV &Z, &X ADD &Z, &Y MOV &Z, &X MEND  Then the macro call INCR X=A, Y=B will take the values A for parameter X, B for parameter Y and R for the parameter Z.  The macro call INCR X=A, Y=B, Z=C will take the values A for parameter X, B for parameter Y and C for the parameter Z. 5.5 MACRO PROCESSOR DESIGN OPTIONS 5.5.1 Two Pass Macro Processor  Same as on page 4  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.  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 type of macro are called nested macros. Here the default value R is specified for the parameter Z
  • 21. CS303 System Software Module 5 Page 21 SWAP MACRO &X,&Y LDA &X LDX &Y STORE MACRO &X,&Y STA &Y STX &X MEND MEND 5.5.2 One Pass Macro Processor  Same as on page 4 (here only brief description is needed)  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 DEFTAB NAMTAB ARGTAB  Whenever a macro definition is encountered, the macro prototype and body of macro is entered into DEFTAB. References to the macro instruction parameters are converted to a positional notation for efficiency in substituting arguments.  The macro name along with the begin and end pointers are entered into NAMTAB.  Whenever a macro invocation is encountered, the arguments are entered into ARGTAB.  The macro call is expanded with the lines from the DEFTAB. When the ?n notation is recognized in a line from DEFTAB, the corresponding argument is taken from ARGTAB. 5.5.3 Recursive Macro Expansion  Invocation of one macro by another macro is called recursive macro.  Example for recursive macro: Inner macro outer macro
  • 22. CS303 System Software Module 5 Page 22 SUM MACRO &X,&Y STA &X ADD &Y MEND INPUT MACRO &A,&B SUM &A,&B .i n v o k i n g the macro SUM MEND Here the macro named INPUT is calling another macro named SUM. This is called as recursive macro.  The macro processor design algorithm discussed previously cannot handle recursive macro invocation and expansion.  Reasons are: o The procedure EXPAND would be called recursively, thus the invocation arguments in the ARGTAB will be overwritten. o The Boolean variable EXPANDING would be set to FALSE when the “inner” macro expansion is finished, that is, the macro process would forget that it had been in the middle of expanding an “outer” macro. o A similar problem would occur with PROCESSLINE since this procedure too would be called recursively.  Solutions: o Write the macro processor in a programming language that allows recursive calls, thus local variables will be retained. o If we are writing in a language without recursion support, use a stack to take care of pushing and popping local variables and return addresses. So the recursive calls can be handled. 5.5.4 General-Purpose Macro Processors  The macro processor we discussed so far is related to assembly language programming. Macro processor for high level languages have also been developed. Macro processors which are designed for a specific language are called special purpose macro processors. Example for special purpose macro processor is MASM Macro processor  These special purpose macro processors are similar in general function and approach but the implementation details differ from language to language.
  • 23. CS303 System Software Module 5 Page 23  The general purpose macro processor do not dependent on any particular programming language, but can be used with a variety of different languages.  Example for a general purpose macro processor is ELENA Macro processor Advantages  Programmers do not need to learn many macro languages, so much of the time and expense involved in training are eliminated.  Although its development costs are somewhat greater than those for a specific language macro processor, this expense does not need to be repeated for each language, thus save substantial overall cost. Disadvantages In spite of the advantages noted, there are still relatively few general purpose macro processors. The reasons are:  Large number of details must be dealt with in a real programming language.  There are several situations in which normal macro parameter substitution or normal macro expansion should not occur. o For example, comments are usually ignored by the macro processor. But each programming languages uses its own method for specifying comments. So a general purpose macro processor should be designed with the capability for identifying the comments in any programming languages.  Another problem is with the facilities for grouping together terms, expressions, or statements. o Some languages use keywords such as begin and end for grouping statements while some other languages uses special characters like { }. A general purpose macro processor may need to take these groupings into account in scanning the source statements.  Another problem is with the identification of tokens of the programming languages. The tokens means the identifiers, constants, operators and keywords in the programming language. Different languages uses different rules for the formation of tokens. So the design of a general purpose macro processor must take this into consideration.  Another problem is with the syntax used for macro definitions and macro invocation statements.
  • 24. CS303 System Software Module 5 Page 24 5.5.5 Macro Processing within Language Translators  Macros can be processed 1. Outside the language translators 2. Within the Language translators 1.Macro processing outside the language translators  The macro processors that we had discussed so far belongs to this group. They are called macro preprocessors.  They reads the source program statements, process the macro definitions and expand macro invocations, producing an expanded version of the source program.  This expanded program is then used as input to an assembler or compiler.  So this can be considered as macro processing outside the language translators. 2.Macro processing within the language translators  In this section we discuss the methods for combining the macro processing functions with the language translator itself.  Two common methods are a) Line –by- Line Macro Processors b) Integrated Macro Processors a) Line-by-line macro processor  The simplest method for combining the macro processing functions with the language translator is a line-by-line approach.  Using this approach, the macro processor reads the source program statement, process the macro definitions and expand macro invocations. But it does not produce an expanded version of source program.  The processed lines are passed to the language translator(compiler or assembler) as they are generated, instead of being written to an expanded source file.  Thus macro processor operates as a sort of input routine for the assembler or compiler. Benefits of line –by-line macro processor  It avoids making an extra pass over the source program. So it can be more efficient than using a macro preprocessor.
  • 25. CS303 System Software Module 5 Page 25  Some of the data structures required by the macro processor and the language translator can be combined (e.g., OPTAB and NAMTAB)  Many utility subroutines can be used by both macro processor and the language translator. This involves scanning input lines, searching tables , converting numeric values to internal representation etc.  It is easier to give diagnostic messages related to the source statements. b) Integrated Macro Processors  Although a line-by-line macro processor may use some of the same utility routines as language translator, the functions of macro processing and program translation are still relatively independent.  It is possible to have even closer cooperation between the macro processor and the language translator. Such a scheme is called as language translator with an integrated macro processor.  An integrated macro processor can potentially make use of any information about the source program that is extracted by the language translator.  The macro processor may use the results of the translator operations such as scanning of symbols, constants, etc without involved in processing it.  For example in FORTRAN language, consider the statement: DO 100 I = 1,20 where DO is a keyword, 100 is statement number, I is a variable name etc. DO 100 I = 1 since in FORTRAN blanks are not significant, this statement is an assignment that gives the value 1 to the variable DO100I. Thus the proper interpretation of characters cannot be decided until the rest of the statement is examined. Disadvantages of line-by-line and integrated macro processors  They must be specially designed and written to work with a particular implementation of an assembler or compiler. The cost of macro processor development is added to the costs of the language translator, which results in a more expensive software. The assembler or compiler will be considerably larger and more complex than using a macro preprocessor. Size may be a problem if the translator is to run on a computer with limited memory.
  • 26. CS303 System Software Module 5 Page 26 5.6 TYPES OF MACRO Different types of macro are 1. Parameterized Macro 2. Nested Macro 3. Recursive Macro 1. Parameterized Macro  Macros which uses parameters are called parameterized macro.This type of macro has the capability to insert the given parameters into its expansion.Macros we studied so far belongs to this type.  Example: SUM MACRO &X,&Y LDA &X MOV B LDA &Y ADD B MEND 2. Nested Macros  Nested macros are macros in which definition of one macro contains definition of other macros.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 type of macro are called nested macros. SWAP MACRO &X,&Y LDA &X LDX &Y STORE MACRO &X,&Y STA &Y STX &X MEND MEND 3. Recursive Macro  Invocation of one macro by another macro is called recursive macro.Example for recursive macro: SUM MACRO &X,&Y STA &X ADD &Y MEND INPUT MACRO &A,&B SUM &A,&B .i n v o k i n g the macro SUM MEND Here the macro named INPUT is calling another macro named SUM. This is called as recursive macro. Inner macro outer macro