Systems Programming Notes
Systems Programming Notes
Sub. Code-2150708
Unit- 4 (Macro and Macro pre-
processor)
By- Prof. Deepmala Sharma
MBICT CVM group, Anand
(V.V.Nagar)
Contents
• What is Macro?
• Advantages and disadvantages of Macro
• Applications of Macro
• Difference between Macro and Subroutines
• Macro Definition and Call
• Macro Expansion
• Handling Macro calls and Macro expansion
• Features of Macro facility
– Lexical expansion and parameter substitution
– Nested Macro Calls
– Advanced Macro Facilities
• Design Of a Macro Pre-processor
– Functions of a Macro Processor
– Basic Tasks of a Macro Processor
– Design Issues of Macro Processors,
– Features,
– Macro Processor Design Options,
– Two-Pass Macro Processors, and One-Pass Macro Processors
• Design of a Macro Assembler
Macro and Macro processor
• Macro:-
– Macro instructions are single line abbreviations for group
of instructions.
– Using a macro, programmer can define a single
“instruction” to represent block of code.
– Ex: #define in C and MACRO <Macro Name> in Assembly
language
• Macro processor-
– Macro instructions are an extension of the basic assembly
language that can simplify debugging and program
modification during translation.
– To process macro instructions , most assembler use pre-
processors known as Macro processors
Advantages and Disadvantages
• The advantages of using macro are as follows:
– Simplify and reduce the amount of repetitive coding.
– Reduce the possibility of errors caused by repetitive
coding
– Make an assembly program more readable
Sequence of instructions
End of definition
• Macro Call
Macro Expansion
• Replacement of macro call by corresponding sequence
of instructions is called as macro expansion
• To expand a macro, the name of the macro is placed in
the operation field, and no special directives are
necessary.
• The assembly code sequence INCR A, B, AREG
is an example of the macro call
• the assembler recognizes INCR as the name of the
macro, expands the macro, and places a copy of the
macro definition (along with the parameter
substitutions)
• ‘+’ sign in the preceding label field denote the
expanded code
Example
Macro call
MACRO
INCR &A, &B, AREG
MOVER REG &A
Expanded code ADD REG & B
MOVEM REG & A
MEND
• Macro call
– MNAME REG1, A, B
• Expanded code
– MOVER REG1, A
– ADD REG1, B
– MOVEM REG1, A
• B) keyword arguments or parameters
• This allows reference to dummy arguments by
name as well as by position.
• The parameter specification are useful when
macros use a long list of parameters
• Format: &<name_of_par>=<actual_par>
Example
• Macro definition
– MACRO
– MNAME &par1=, &par2=, &par3=
– MOVER &par1, &par2
– ADD &par1, &par3
– MOVEM &par1, &par2
– MEND
• Macro call
– 1. MNAME par1=REG1, par2=A, par3=B OR
– 2. MNAME par1=A, par2=REG1, par3=B
• Macro call
– 1. MNAME par2=A, par3=B OR
– 2. MNAME par1=REG2, par2=A, par3=B
• Macro call
– MCALC A, B, par4=loop
• 1. Expanded code
– +loop MOVER REG1, A
– MULT REG1, B
– MOVEM REG1, A
Flow of control during macro
expansion
• Flow of control during expansion.
– The default flow of control during macro expansion is
sequential. its start with statement following the
macro prototype statement and ending with the
statement preceding the MEND statement.
– A pre-processor statement can alter the flow of
control during expansion such that some model
statements are never visited during expansion is called
conditional expansion.
– Same statement are repeatedly visited during
expansion is called loops expansion.
Flow of control during macro
expansion
• The flow of control during macro expansion is
implemented using a macro expansion counter (MEC)
• Algorithm of macro expansion:
• 1. MEC:=statement number of first statement following
the prototype stmt.
• 2. While statement pointed by MEC is not a MEND
statement.
– a. If a model statement then
• i. Expand the statement
• ii. MEC:=MEC+1;
– b. Else (i.e. a pre-processor statement)
• i. MEC:= new value specified in the statement.
• 3. Exit from macro expansion.
Nested Macro Facility
• Nested Macro Definition:
• Nested macro definition are those cases where the definition of
one macro is contained within the definition of another
• Example :
• MACRO
• MAC_X &par1, &par2
• MOVER REG1, &par1
– MACRO
– MAC_Y &par2, REG=REG3
– ADD REG, &par2
– MOVEM REG, &par2
– MEND
• PRINT &par1
• MEND
Nested Macro Facility
• Nested Macro Expansion and Call:
• A model statement in a macro may constitute a
call on another macro.
• Such calls are known as nested macro calls
• Macro containing the nested call is the outer
macro and, Macro called is inner macro
• They follow LIFO rule.
• Thus, in structure of nested macro calls,
• expansion of latest macro call (i.e inner macro) is
• completed first.
Example
• MACRO
– MAC_Y &par2, REG=REG3
– ADD REG, &par2
– MOVEM REG, &par2
– MEND
• MACRO
– MAC_X &par1, &par2
– MOVER REG1, &par1
– MAC_Y &par2, REG=REG3
• PRINT &par1
• MEND
Nested Expansion
Macro call Expansion Nested expansion
MAC_X A, B + MOVER REG1, A + MOVER REG1, A
+MAC_Y B, REG2 ++ADD REG2, B
+PRINT A ++ MOVEM REG2, B
+PRINT A
Recursive macro call
• A recursive macro is a situation where the macro
expands itself.
• Note: Macro definition does not contain any statement
to stop any of the inner expansions and return to
complete the outer ones. This lead to infinite loop.
• The conditional statement is one mechanism to handle
recursive call
• MACRO
– MAC_Y &par=A
– MOVER REG1, &par
– MAC_Y &par=A
• MEND
ADVANCED MACRO FACILITIES
• Facilities for change of flow of control
• Expansion time variables
• Attributes of parameters
• Advance directives
– The Remove Directive
– The IRP Directive
– The REPEAT Directive
For change of flow of control:-
Two features are provided for this purpose
1) Expansion time Sequencing Symbol
2) Expansion time Statements ( AIF,AGO,ANOP)
Sequencing Symbol( SS)
SS is defined by putting it in the label field of a stmt in the macro
body.
Statements (syntax)
AIF-> AIF ( <expression>) <Sequencing Symbol (SS)>
Expression includes relational expression ( LT, NE etc.). If condition
is true then control is transferred to SS.
AGO-> AGO <SS> unconditional transfer
ANOP-> <SS> ANOP used to define SS
Example
MACRO
TEST &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &Y
AGO .LAST
.ONLY MOVER AREG, &Z
.LAST MEND
MACRO CALL
TEST S,10
Advance directives
• The Remove Directive: Remove Macro from MDT(Macro definition table).
• The IRP Directive: (Indefinite repeat): It is used by programmer to direct
the assembler to repeatedly duplicate and assemble the sequence a
number of times determined by a compound parameter.
• IRP example
– Macro
– MAC_X &P, &Q
– IRP &P
– ADD REG1, &P
– IRP
– MEND
• MACRO CALL
– MAX_X (A ,B, #3), H
• On execution
– ADD REG1, A
– ADD REG1, B
– ADD REG1, #3
The REPEAT (REPT)Directive
• REPEAT: ): This is another facility with assembler to duplicate and
assemble the sequence a number of times during macro expansion.
• Syntax: REPT <expression>
– MACRO
– DEF_R
– LCL &P
– &P SET 5
– REPT 5
– DC ‘&P’
– &P SET &P+1
– ENDM
– MEND
– On execution
– 5,6,7,8,9,10 will be output
Design issues of Macro preocessor
• Flexible data structures and databases
• Attributes of macro arguments/parameter
name
• Default arguments/ formal parameter value at
macro definition time
• Numeric values of arguments/ actual
parameter value at macro call
• Comments in macros
Features of macro processor
• Associating macro parameters with their
arguments
• Delimiting macro parameters
• Directives related to arguments
• Automatic label generation
• Machine independent features
Design of Macro Preprocessor
Functions of Macro preprocessor
• Macro processor will perform the following
tasks-
– Identifies macro definitions and calls in the
program.
– Determines formal parameters and their values.
– Keeps track of the values of expansion time
variables and sequencing symbols declared in a
macro.
– Handles expansion time control flow and performs
expansion of model statements.
Two pass Macro Processor
• General Design Steps
• Step 1: Specification of Problem:-
• Step 2 Specification of databases:-
• Step 3 Specification of database
formats
• Step 4 : Algorithm
Specify the problem
• In Pass-I the macro definitions are searched
and stored in the macro definition table and
the entry is made in macro name table
• In Pass-II the macro calls are identified and the
arguments are placed in the appropriate place
and the macro calls are replaced by macro
definitions.
Specification of databases:-
Pass 1:-
• The input macro source program.
• The output macro source program to be used by Pass2.
• Macro-Definition Table (MDT), to store the body of macro
defns.
• Macro-Definition Table Counter (MDTC), to mark next
available entry MDT.
• Macro- Name Table (MNT), used to store names of macros.
• Macro Name Table counter (MNTC), used to indicate the
next available entry in MNT.
• Argument List Array (ALA), used to substitute index markers
for dummy arguments before storing a macro-defns.
Specification of databases:-
• Pass 2:-
• The copy of the input from Pass1.
• The output expanded source to be given to assembler.
• MDT, created by Pass1.
• MNT, created by Pass1.
• Macro-Definition Table Pointer (MDTP), used to
indicate the next line of text to be used during macro-
expansion.
• Argument List Array (ALA), used to substitute macro-
call arguments for the index markers in the stored
macro-defns
Specification of Database format
Specification of Database format
EXAMPLE
MACRO
TEST &X, &N, R=AREG REPRESENTS PARAMETER NAME
LCL &M REPRESENTS EXPAN. VAR. NAME
&M SET 0 REPRESENTS SEQUENCING SYMBOL
.PQ MOVEM &R, &X+&M NAME
REPRESENTS ACTUAL PARAMETER NAME
&M SET &M + 1
AIF (&M NE N) .PQ
MEND
MACRO CALL
TEST S,10
X
R AREG PQ
N M
R
SSNTAB
EVNTAB KPDTAB
PNTAB
10 0
27
AREG SSTAB
APTAB EVTAB
Name #PP #KP #EV MDTP KPDTP SSTP
TEST 2 1 1 25 10 5
MNT
25 LCL (E,1)
26 (E,1) SET 0
27 MOVEM (P,3),(P,1)+(E,1)
MDT 28 (E,1) SET (E,1) +1
29 AIF (E,1) NE (P,2) (S,1)
30 MEND
Steps for MNT Entry
STRUCTURE OF MNT
Name #PP #KP #EV MDTP KPDTP SSTP
x N
#pp=1
#pp=1+1=2
PNTAB
MDTC MDTC+1
MDTC MDTC+1
Algorithm for Pass – 2
• This algorithm reads one line of i/p prog. at a time.
• for each Line it checks if op-code of that line matches any of the MNT
entry.
• When match is found (i.e. when call is pointer called MDTF to
corresponding macro defns stored in MDT.
• The initial value of MDTP is obtained from MDT index field of MNT
entry.
• The macro expander prepares the ALA consisting of a table of dummy
argument indices & corresponding arguments to the call.
• Reading proceeds from the MDT, as each successive line is read, The
values form the argument list one substituted for dummy arguments
indices in the macro defn.
• Reading MEND line in MDT terminates expansion of macro & scanning
continues from the input file.
• When END pseudo-op encountered , the expanded source program is
given to the assembler
MDTP MDTP + 1
Pass structure of Macro assembler
Macro assembler flow chart