You are on page 1of 53

Macro Processor

JITHIN JACOB
LECTURER
Dept. Of CSE
MACE
ADAPTED FROM

1.SYSTEM SOFTWARE BY J.NITHYASHRI .


TATA MC GRAW 2ND EDITION
2.SYSTEM SOFTWARE BY LEYLAND L BECK
3RD EDITION ASIA PEARSON EDUCATION

COPYRIGHT@JITHIN JACOB , Dept. Of CSE, MACE , Kothamangalam


What is macro?
A macro is a single line abbreviation for a group of machine
instructions.
Basically a single instruction replacing a group of
instructions
A macro instruction (macro) is simply a notational
convenience for the programmer
Using macros allows a programmer to write a shorthand
version of a program.
Use of macro improves readability of program
What macro processor does?

The macro processor replaces each macro instruction


with the corresponding group of source statements.
This operation is called expanding the macro

Eg: ANSI C MACRO


ELENA MACRO PROCESSOR
Machine Independent
The functions of a macro processor essentially involve the
substitution of one group of lines for another. Normally,
the processor performs no analysis of the text it handles.
The meaning of these statements are of no concern during
macro expansion.
Therefore, the design of a macro processor generally is
machine independent.
Macros are mostly used un assembler language
programming. However, it can also be used in high-level
programming languages such as C or C++.
Basic Functions
1. MACRO DEFINITION

2. MACRO INVOCATION OR CALLING

3. MACRO EXPANSION
1. Macro definition
The two directive MACRO and MEND are used in macro
definition.
The first MACRO statement identifies the beginning of the
macron definition
MEND - end of macro definition
The name of the macro is followed by a list of parameters of
macro instruction
Each parameter begins with &
Between MACRO and MEND is the body of the macro. These
are the statements that will be generated as the expansion of the
macro definition.
syntax
MACRO
<name> [ parameter list]
-----------------------------
-----------------------------
----------------------------
<assembly language code>
MEND
Example:
MACRO // macro header statement ( indicate existence of macro)
SUM &X, &Y
LOAD &X
ADD &Y
STORE &X
MEND
2. MACRO INVOCATION
Used to call the MACRO
MACRO invocation statement gives the name of the macro
instruction being invoked and the actual parameters to be
used in expanding the macro

Often referred as MACRO call

Eg : SUM M,N ( SUM name of macro, M,N actual


parameters / * X , Y used in macro definition eg: is the
formal parameter)
3. MACRO EXPANSION
Assembly statement replace macro as a result of
macro call
Note:: actual parameter get substituted instead of
formal parameter and as a result the macro is said to
be expanded
Eg:
LOAD M
ADD N
STORE M
MACRO PROCESSOR ALGORITHM AND
DATA STUCTURES

Three main data structures


1.DEFTAB (definition table)
2.NAMTAB(Name table)
3.ARGTAB(Argument table)
DEFTAB:
The macro definitions are stored in DEFTAB.
It contains the statements that make up the macro body
The MEND statement is also stored in DEFTAB to indicate
end of macro definition
Store the definition statements of macros
Comment lines are omitted.
NAMTAB:
Used to store names of macro defined in the program
Each entry in a macro name table would have a macro
name and a pointer to the entry in deftab
It also specifies number of keyword parameters
ARGTAB:
Used during expansion of macro
When ever a macro call is recognized the arguments are
stored in ARGTAB according to their position in argument list
When the macro is expanded arguments from ARGTAB are
substituted for the corresponding parameters in the macro
body
For a clear under standing of this
shall we construct these tables???
DEF TAB : FORMAT

Entry # STATEMENT
26 SUM &X , &Y #1,#2
27 LOAD &x #1
28 ADD &Y #2
29 STORE &X #3
30 MEND
31
32
NAMTAB: FORMAT

NUMBER OF NUMBER OF NUMBER OF


POINTER TO POSITIONAL KEYWORD EXPANSION
MACRO DEFTAB PARAMETER PARAMETER TIME
NAME (#pp) VARIABLES
( EV)

SUM 26 2 3 0
ARGTAB : FORMAT

#1
M

#2 N
ADD M
#3
Data Structures Snapshot
DESIGN OF A ONE - PASS MACRO
PREPROCESSOR
PREPROCESSOR :Macro preprocessor is a software
which accepts an assembly language program that
consist of macro definition and macro calls and
converts it into an assembly language program with
out macro calls and macro expansion
Assembly lang. program macro preprocessor
ALP with out macros ASSEMBLER target
pgm
// macro processor inbuilt in assembler or closely
related to assembler
//one pass scan & expand in one step
Two pass scan first and expand when called
Design consist of 3 steps

STEP 1: scan all macro definitions one by one. For


each macro identified
1. enter its name in NAMTAB
2.store macro definition in DEFTAB
3.add information to NAMTAB where the
definition of the macro can be found in DEFTAB
STEP 2: Examine all statements in the assembly
lang. program to detect macro calls. For each
macro call
1. locate the macro in NAMTAB
2. obtain information from NAMTAB regarding
the position of macro definition in DEFTAB
3. process the macro call to establish one to one
correspondence between actual and formal
parameters
4. Expand the macro call by following the procedure
in step 3
STEP 3: process the statement in macro definition
until MEND is reached.
Algorithm
Procedure DEFINE
Called when the beginning of a macro definition is
recognized. Make appropriate entries in DEFTAB and
NAMTAB.
Procedure EXPAND
Called to set up the argument values in ARGTAB and
expand a macro invocation statement
Procedure GETLINE
Get the next line to be processed
Flowchart
START

READ NEXT STATEMENT


PROCESS
UPTO MEND
NO MACRO
HEADER
?
Y
CONSTRUCT ENTER
NAMTAB STATEMENTS
INTO DEF TAB

EXPAND = OFF
Algorithm Pseudo Code
TYPES OF MACRO

1. PARAMETERIZED MACRO ( EG: X ((X)-2))

2. NESTED MACRO ( MACRO WITH IN MACRO EG:))

3. RECURSIVE MACRO
Machine Independent Features
Concatenation of Macro
Parameters
Most macro processors allow parameters to
be concatenated with other character stings.
E.g., to flexibly and easily generate the
variables XA1, XA2, XA3, , or XB1,
XB2, XB3, A or B can be input as an
argument. We just need to concatenate X,
the argument, and the 1 , 2, 3 ..
together.
Concatenation Example
Macro definition using concatenation operator
MACRO
TOTAL &ID
LDA X&ID1
ADD X&ID2
ADD X&ID3
STA X&IDs
MEND

Macro invocation TOTAL A

LDA XA1
ADD XA2
ADD XA3
STA XAS
Generation of Unique Labels
To generate unique labels for each macro
invocation, when writing macro definition, we
must begin a label with $.
During macro expansion, the $ will be replaced
with $xx, where xx is a two-character
alphanumeric counter of the number of macro
instructions expanded.
XX will start from AA, AB, AC,..
Unique Labels Macro Definition
Unique Labels Macro Expansion
Conditional Macro Expansion
So far, when a macro instruction is invoked, the
same sequence of statements are used to expand
the macro.
Here, we allow conditional assembly to be used.
Depending on the arguments supplied in the macro
invocation, the sequence of statements generated for a
macro expansion can be modified.
Conditional macro expansion can be very useful.
It can generate code that is suitable for a particular
application.
Conditional Macro Example
In the following example, the values of &EOR and &MAXLTH
parameters are used to determine which parts of a macro definition
need to be generated.
There are some macro-time control structures introduced for doing
conditional macro expansion:
IF- ELSE-ENDIF
WHILE-ENDW
Macro-time variables can also be used to store values that are used
by these macro-time control structures.
Used to store the boolean expression evaluation result
A variable that starts with & but not defined in the parameter list is treated
as a macro-time variable.
Macro time variable

Conditional macro control structure


Conditional macro expansion 1
Conditional macro expansion 2
Conditional macro expansion 3
Conditional Macro Implementation
The assembler maintains a symbol table that contains the
values of all macro-time variables used.
Entries in this table are made or modified when SET
statements are processed.
When an IF statement is encountered during the expansion
of a macro, the specified boolean expression is evaluated.
If the value of this expression is TRUE, the macro processor
continues to process until it encounters the next ELSE or ENDIF.
If ELSE is encountered, then skips to ENDIF
Otherwise, the assembler skips to ELSE and continues to process
until it reaches ENDIF.
Conditional Macro Example
Macro processor function
Conditional Macro Expansion v.s.
Conditional Jump Instructions
The testing of Boolean expression in IF statements
occurs at the time macros are expanded.
By the time the program is assembled, all such
decisions have been made.
There is only one sequence of source statements during
program execution.
In contrast, the COMPR instruction test data values
during program execution. The sequence of statements
that are executed during program execution may be
different in different program executions.
Keyword Macro Parameters
So far, all macro instructions use positional parameters.
If an argument is to be omitted, the macro invocation statement must
contain a null argument to maintain the correct argument positions.
E.g., GENER ,,DIRECT,,,,,,3.
If keyword parameters are used, each argument value is written
with a keyword that names the corresponding parameters.
Arguments thus can appear in any order.
Null arguments no longer need to be used.
E.g., GENER TYPE=DIRECT, CHANNEL=3
Keyword parameter method can make a program easier to read
than the positional method.
Keyword Macro Example

Can specify default values


Keyword parameters
Design Options
Recursive Macro Expansion
If we want to allow a macro to be invoked in a
macro definition, the already presented macro
processor implementation cannot be used.
This is because the EXPAND routine is
recursively called but the variable used by it (e.g.,
EXPANDING) is not saved across these calls.
It is easy to solve this problem if we use a
programming language that support recursive
functions. (e.g., C or C++).
Recursive Macro Example
Recursive Macro Example

For easy implementation, we require that RDCHAR macro


be defined before it is used in RDBUFF macro. This
requirement is very reasonable.

You might also like