Subject-System Programming
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
• The disadvantage of the macro is the size of the
program. The reason is, the pre-processor will
replace all the macros in the program by its real
definition prior to the compilation process of the
program.
Applications of Macro
• when assembly language programming was
commonly used to write programs for digital
computers
• the use of macro instructions was initiated for
two main purposes:
• to reduce the amount of program coding that
had to be written by generating several
assembly language statements from one
macro instruction
MACROS PROCEDURE
1
The corresponding machine code
is written every time a macro is
called in a program.
1 The Corresponding m/c code is
written only once in memory
2
Program takes up more memory
space.
2 Program takes up comparatively
less memory space.
3
No transfer of program counter. 3 Transferring of program counter is
required.
4
No overhead of using stack for
transferring control.
4 Overhead of using stack for
transferring control.
5
Execution is fast 5 Execution is comparatively slow.
6
Assembly time is more. 6 Assembly time is comparatively
less.
7
More advantageous to the
programs when repeated group of
instruction is too short.
7 More advantageous to the
programs when repeated group of
instructions is quite large.
Difference between MACRO and PROCEDURE
Difference between MACRO and Subroutine
Design of Macro pre-processor
Macro definition and call
• A macro is a unit of specification for program generation
through expansion.
• A macro consists of a name, a set of formal parameters and
a body of code.
• The use of macro name with a set of actual parameters is
replaced by code generated from its body.
• The formal structure of a macro includes the following
features:
– Macro prototype statement: Specifies the name of the macro
and type of formal parameters.
– Model statements: Specify the statements in the body of the
macro from which assembly language statements are to be
generated during expansion.
– Macro preprocessor statement: Specifies the statement used
for performing auxiliary function during macro expansion
Macro Statements
• A macro prototype statement
• where [<formal parameter spec> [,...]] defines
the parameter name and its kind, which are of
the following form:
• A macro can be called by writing the name of the
macro in the mnemonic field of the assembly
language called macro call
• macro call Statement:
Example of macro definition and call
• Macro Definition
• Macro Call
Name of macro
Start of definition
Sequence of instructions
End of definition
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
INCR &A, &B, AREG
MOVER REG &A
ADD REG & B
MOVEM REG & A
MEND
Macro definitionMacro call
 A and B are an actual
parameters
 &A and &B are formal
parameters
Macro call
Expanded code
First Macro Example:-
#include <stdio.h>
#define SquareOf(x) x*x // (Macro definition)
void main() {
int xin=3;
printf("nxin=%i",xin);
printf("nSquareOf(xin)=%i",SquareOf(xin)); // (macro call)
printf("nSquareOf(xin+4)=%i",SquareOf(xin+4));
printf("nSquareOf(xin+xin)=%i",SquareOf(xin+xin));
}
Lexical Expansion and Parameter
Substitution (Lexical Substitution)
• Two key points concerning macro expansion
are:
– Expansion time control flow: This determines the
order in which model statements (body) are
visited during macro expansion.
– Lexical Substitution: It is used to generate an
assembly statement from a model
statement(body).
Four ways of specifying arguments to
a macro call (Parameter Substitution)
• A) Positional argument or Parameter
Argument are matched with dummy arguments
according to order in which they appear or
compute the ordinal position of the formal
parameters.
• INCR A,B,C
• ‘A’ replaces first dummy argument
• ‘B’ replaces second dummy argument
• ‘C’ replaces third dummy argument
Example
• Macro definition
– MACRO
– MNAME &par1, &par2, &par3
– MOVER &par1, &par2
– ADD &par1, &par3
– MOVEM &par1, &par2
– MEND
• Macro call
– MNAME REG1, A, B
• Expanded code
– MOVER REG1, A
– ADD REG1, B
– MOVEM REG1, A
Formal Par Actual par
&par1 REG1
&par2 A
&par3 B
• 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
• 1. Expanded code
– MOVER REG1, A
– ADD REG1, B OR
– MOVEM REG1, A
2. Expanded code
MOVER A, REG1
ADD A, B
MOVEM A, REG1
• C) keyword Default Parameters
• If a parameter has the same value in most calls
on a macro, this value can be specified as its
default value in the macro definition itself.
• If a macro call does not explicitly specify the value
of the parameter, the pre-processor uses its
default value; otherwise, it uses the value
specified in the macro call.
• Par. spec. required when the parameter takes the
same value in most calls.
• Format :
Example
• Macro definition
– MACRO
– MNAME &par1= REG1, &par2=, &par3=
– MOVER &par1, &par2
– ADD &par1, &par3
– MOVEM &par1, &par2
– MEND
• Macro call
– 1. MNAME par2=A, par3=B OR
– 2. MNAME par1=REG2, par2=A, par3=B
• 1. Expanded code
– MOVER REG1, A
– ADD REG1, B OR
– MOVEM REG1, A
2. Expanded code
MOVER REG2, A
ADD REG2, B
MOVEM REG2, A
• d) Mixed Parameters
• A macro definition may use both positional and
keyword parameters. In such a case, all
• positional parameters must precede all keyword
parameters in a macro call
• This makes the search and association of the
actual parameters to the corresponding formal
parameter easier.
• The parameters can be used not only in the
operation field, but also in all the field of the
instruction
Example
• Macro definition
– MACRO
– MCALC &par1=, &par2=, &par3=MULT, &par4=
– &par4
– MOVER REG1, &par1
– ADD REG1, &par2
– MOVEM REG1, &par1
– MEND
• 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
REPRESENTING SS
If condition is true ( Y= X) then control will be transferred
to .ONLY
Expansion time variables
EV’s are variables which can be only used during the expansion
of macro calls.
It may be of two types:-
1) Local EV (LCL) can be used only in one particular macro.
2) Global EV (GBL) works like global variables.
Syntax:-
LCL <EV specification>, ………
GBL <EV specification>, ……..
Values of EV can be changed using SET (preprocessor stmt).
<EV specification> SET <SET expression>
Example of EV
MACRO
TESTEV
LCL &A
&A SET 1
LCL &A declares local variable A
Attributes of parameters
An attribute is written using the syntax:-
<attribute name> ’ <formal parameter specification>
T– type
L – length
S – size
Example
MACRO
ME &A
AIF (L’&A EQ 1) .NEXT
------
-----
EXAMPLE
MACRO
TEST &X, &N, R=AREG
LCL &M
&M SET 0
.PQ MOVEM &R, &X+&M
&M SET &M + 1
AIF (&M NE N) .PQ
MEND
MACRO CALL
TEST S,10
REPRESENTS PARAMETER NAME
REPRESENTS EXPAN. VAR. NAME
REPRESENTS SEQUENCING SYMBOL
NAME
REPRESENTS ACTUAL PARAMETER NAME
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
LCL &M
&M SET 0
.PQ MOVEM &R, &X+&M
&M SET &M + 1
AIF (&M NE N) .PQ
MEND
MACRO CALL
TEST S,10
REPRESENTS PARAMETER NAME
REPRESENTS EXPAN. VAR. NAME
REPRESENTS SEQUENCING SYMBOL
NAME
REPRESENTS ACTUAL PARAMETER NAME
X
N
R
PNTAB
M
EVNTAB
R AREG
KPDTAB
PQ
SSNTAB
S
10
AREG
APTAB
0
EVTAB
27
SSTAB
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)
28 (E,1) SET (E,1) +1
29 AIF (E,1) NE (P,2) (S,1)
30 MEND
MDT
Steps for MNT Entry
Name #PP #KP #EV MDTP KPDTP SSTP
TEST &X, &N, R=AREG
STRUCTURE OF MNT
For each positional parameter
(i) Enter Parameter name in PNTAB [PNTAB_ptr]
(ii)PNTAB_ptr := PNTAB_Ptr + 1;
(iii)# PP := # PP + 1 :
PNTAB
x
#pp=1
N
#pp=1+1=2
.PQ MOVEM &R, &X+&M
SSNTAB
a) If SS is present in SSNTAB then get index No.
Else
1 Enter SS in SSNTAB, then get index No. and
2. Enter MDT_Ptr in SSTAB
b) For a parameter , generate the specification ( p,#n).
c) For an expansion variable,generate the
specification ( E, # m).
.PQ
SSTAB
5
27
.PQ MOVEM &R, &X+&M
MOVEM (P,3) , (P,1)+(E,1) Enter this line into MDT
Steps for Model Stmt
Argument List Array (ALA):
• ALA is used during both Pass1 & Pas2 but for some what
reverse functions.
• During Pass1, in order to simplify later argument
replacement during macro expansion, dummy arguments
are replaced with positional indicators when defn is stored.
Ex. # 1, # 2, # 3 etc.
• The ith dummy argument on the macro-name is
represented in the body by #i.
• These symbols are used in conjunction with ALA prepared
before expansion of a macro-call.
• Symbolic dummy argument are retained on macro-name to
enable the macro processor to handle argument
replacement byname rather by position.
During pass-I
Algorithm
• Pass1 of macro processor makes a line-by-line scan
over its input.
• Set MDTC = 1 as well as MNTC = 1.
• Read next line from input program.
• If it is a MACRO pseudo-op, the entire macro definition
except this (MACRO) line is stored in MDT.
• The name is entered into Macro Name Table along with
a pointer to the first location of MDT entry of the
definition.
• When the END pseudo-op is encountered all the
macro-defns have been processed, so control is
transferred to pass2
MDTC 1
MNTC 1
MDTC MDTC+1
MDTC MDTC+1
Enter Macro Name and Current
value of MDTC in MNT
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

Unit 4 sp macro

  • 1.
    Subject-System Programming Sub. Code-2150708 Unit-4 (Macro and Macro pre- processor) By- Prof. Deepmala Sharma MBICT CVM group, Anand (V.V.Nagar)
  • 2.
    Contents • What isMacro? • 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
  • 3.
    Macro and Macroprocessor • 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
  • 4.
    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 • The disadvantage of the macro is the size of the program. The reason is, the pre-processor will replace all the macros in the program by its real definition prior to the compilation process of the program.
  • 5.
    Applications of Macro •when assembly language programming was commonly used to write programs for digital computers • the use of macro instructions was initiated for two main purposes: • to reduce the amount of program coding that had to be written by generating several assembly language statements from one macro instruction
  • 6.
    MACROS PROCEDURE 1 The correspondingmachine code is written every time a macro is called in a program. 1 The Corresponding m/c code is written only once in memory 2 Program takes up more memory space. 2 Program takes up comparatively less memory space. 3 No transfer of program counter. 3 Transferring of program counter is required. 4 No overhead of using stack for transferring control. 4 Overhead of using stack for transferring control. 5 Execution is fast 5 Execution is comparatively slow. 6 Assembly time is more. 6 Assembly time is comparatively less. 7 More advantageous to the programs when repeated group of instruction is too short. 7 More advantageous to the programs when repeated group of instructions is quite large. Difference between MACRO and PROCEDURE
  • 7.
  • 8.
    Design of Macropre-processor
  • 9.
    Macro definition andcall • A macro is a unit of specification for program generation through expansion. • A macro consists of a name, a set of formal parameters and a body of code. • The use of macro name with a set of actual parameters is replaced by code generated from its body. • The formal structure of a macro includes the following features: – Macro prototype statement: Specifies the name of the macro and type of formal parameters. – Model statements: Specify the statements in the body of the macro from which assembly language statements are to be generated during expansion. – Macro preprocessor statement: Specifies the statement used for performing auxiliary function during macro expansion
  • 10.
    Macro Statements • Amacro prototype statement • where [<formal parameter spec> [,...]] defines the parameter name and its kind, which are of the following form: • A macro can be called by writing the name of the macro in the mnemonic field of the assembly language called macro call • macro call Statement:
  • 11.
    Example of macrodefinition and call • Macro Definition • Macro Call Name of macro Start of definition Sequence of instructions End of definition
  • 12.
    Macro Expansion • Replacementof 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
  • 13.
    Example MACRO INCR &A, &B,AREG MOVER REG &A ADD REG & B MOVEM REG & A MEND Macro definitionMacro call  A and B are an actual parameters  &A and &B are formal parameters Macro call Expanded code
  • 14.
    First Macro Example:- #include<stdio.h> #define SquareOf(x) x*x // (Macro definition) void main() { int xin=3; printf("nxin=%i",xin); printf("nSquareOf(xin)=%i",SquareOf(xin)); // (macro call) printf("nSquareOf(xin+4)=%i",SquareOf(xin+4)); printf("nSquareOf(xin+xin)=%i",SquareOf(xin+xin)); }
  • 15.
    Lexical Expansion andParameter Substitution (Lexical Substitution) • Two key points concerning macro expansion are: – Expansion time control flow: This determines the order in which model statements (body) are visited during macro expansion. – Lexical Substitution: It is used to generate an assembly statement from a model statement(body).
  • 16.
    Four ways ofspecifying arguments to a macro call (Parameter Substitution) • A) Positional argument or Parameter Argument are matched with dummy arguments according to order in which they appear or compute the ordinal position of the formal parameters. • INCR A,B,C • ‘A’ replaces first dummy argument • ‘B’ replaces second dummy argument • ‘C’ replaces third dummy argument
  • 17.
    Example • Macro definition –MACRO – MNAME &par1, &par2, &par3 – MOVER &par1, &par2 – ADD &par1, &par3 – MOVEM &par1, &par2 – MEND • Macro call – MNAME REG1, A, B • Expanded code – MOVER REG1, A – ADD REG1, B – MOVEM REG1, A Formal Par Actual par &par1 REG1 &par2 A &par3 B
  • 18.
    • B) keywordarguments 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>
  • 19.
    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 • 1. Expanded code – MOVER REG1, A – ADD REG1, B OR – MOVEM REG1, A 2. Expanded code MOVER A, REG1 ADD A, B MOVEM A, REG1
  • 20.
    • C) keywordDefault Parameters • If a parameter has the same value in most calls on a macro, this value can be specified as its default value in the macro definition itself. • If a macro call does not explicitly specify the value of the parameter, the pre-processor uses its default value; otherwise, it uses the value specified in the macro call. • Par. spec. required when the parameter takes the same value in most calls. • Format :
  • 21.
    Example • Macro definition –MACRO – MNAME &par1= REG1, &par2=, &par3= – MOVER &par1, &par2 – ADD &par1, &par3 – MOVEM &par1, &par2 – MEND • Macro call – 1. MNAME par2=A, par3=B OR – 2. MNAME par1=REG2, par2=A, par3=B • 1. Expanded code – MOVER REG1, A – ADD REG1, B OR – MOVEM REG1, A 2. Expanded code MOVER REG2, A ADD REG2, B MOVEM REG2, A
  • 22.
    • d) MixedParameters • A macro definition may use both positional and keyword parameters. In such a case, all • positional parameters must precede all keyword parameters in a macro call • This makes the search and association of the actual parameters to the corresponding formal parameter easier. • The parameters can be used not only in the operation field, but also in all the field of the instruction
  • 23.
    Example • Macro definition –MACRO – MCALC &par1=, &par2=, &par3=MULT, &par4= – &par4 – MOVER REG1, &par1 – ADD REG1, &par2 – MOVEM REG1, &par1 – MEND • Macro call – MCALC A, B, par4=loop • 1. Expanded code – +loop MOVER REG1, A – MULT REG1, B – MOVEM REG1, A
  • 24.
    Flow of controlduring 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.
  • 25.
    Flow of controlduring 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.
  • 26.
    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
  • 27.
    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.
  • 28.
    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
  • 29.
    Nested Expansion Macro callExpansion 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
  • 30.
    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
  • 31.
    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
  • 32.
    For change offlow 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
  • 33.
    Example MACRO TEST &X, &Y,&Z AIF (&Y EQ &X) .ONLY MOVER AREG, &Y AGO .LAST .ONLY MOVER AREG, &Z .LAST MEND REPRESENTING SS If condition is true ( Y= X) then control will be transferred to .ONLY
  • 34.
    Expansion time variables EV’sare variables which can be only used during the expansion of macro calls. It may be of two types:- 1) Local EV (LCL) can be used only in one particular macro. 2) Global EV (GBL) works like global variables. Syntax:- LCL <EV specification>, ……… GBL <EV specification>, …….. Values of EV can be changed using SET (preprocessor stmt). <EV specification> SET <SET expression>
  • 35.
    Example of EV MACRO TESTEV LCL&A &A SET 1 LCL &A declares local variable A
  • 36.
    Attributes of parameters Anattribute is written using the syntax:- <attribute name> ’ <formal parameter specification> T– type L – length S – size Example MACRO ME &A AIF (L’&A EQ 1) .NEXT ------ -----
  • 37.
    EXAMPLE MACRO TEST &X, &N,R=AREG LCL &M &M SET 0 .PQ MOVEM &R, &X+&M &M SET &M + 1 AIF (&M NE N) .PQ MEND MACRO CALL TEST S,10 REPRESENTS PARAMETER NAME REPRESENTS EXPAN. VAR. NAME REPRESENTS SEQUENCING SYMBOL NAME REPRESENTS ACTUAL PARAMETER NAME
  • 38.
    Advance directives • TheRemove 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
  • 39.
    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
  • 40.
    Design issues ofMacro 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
  • 41.
    Features of macroprocessor • Associating macro parameters with their arguments • Delimiting macro parameters • Directives related to arguments • Automatic label generation • Machine independent features
  • 42.
    Design of MacroPreprocessor
  • 43.
    Functions of Macropreprocessor • 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.
  • 44.
    Two pass MacroProcessor • General Design Steps • Step 1: Specification of Problem:- • Step 2 Specification of databases:- • Step 3 Specification of database formats • Step 4 : Algorithm
  • 45.
    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.
  • 46.
    Specification of databases:- Pass1:- • 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.
  • 47.
    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
  • 48.
  • 49.
  • 50.
    EXAMPLE MACRO TEST &X, &N,R=AREG LCL &M &M SET 0 .PQ MOVEM &R, &X+&M &M SET &M + 1 AIF (&M NE N) .PQ MEND MACRO CALL TEST S,10 REPRESENTS PARAMETER NAME REPRESENTS EXPAN. VAR. NAME REPRESENTS SEQUENCING SYMBOL NAME REPRESENTS ACTUAL PARAMETER NAME
  • 51.
  • 52.
    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) 28 (E,1) SET (E,1) +1 29 AIF (E,1) NE (P,2) (S,1) 30 MEND MDT
  • 53.
    Steps for MNTEntry Name #PP #KP #EV MDTP KPDTP SSTP TEST &X, &N, R=AREG STRUCTURE OF MNT For each positional parameter (i) Enter Parameter name in PNTAB [PNTAB_ptr] (ii)PNTAB_ptr := PNTAB_Ptr + 1; (iii)# PP := # PP + 1 : PNTAB x #pp=1 N #pp=1+1=2
  • 54.
    .PQ MOVEM &R,&X+&M SSNTAB a) If SS is present in SSNTAB then get index No. Else 1 Enter SS in SSNTAB, then get index No. and 2. Enter MDT_Ptr in SSTAB b) For a parameter , generate the specification ( p,#n). c) For an expansion variable,generate the specification ( E, # m). .PQ SSTAB 5 27 .PQ MOVEM &R, &X+&M MOVEM (P,3) , (P,1)+(E,1) Enter this line into MDT Steps for Model Stmt
  • 55.
    Argument List Array(ALA): • ALA is used during both Pass1 & Pas2 but for some what reverse functions. • During Pass1, in order to simplify later argument replacement during macro expansion, dummy arguments are replaced with positional indicators when defn is stored. Ex. # 1, # 2, # 3 etc. • The ith dummy argument on the macro-name is represented in the body by #i. • These symbols are used in conjunction with ALA prepared before expansion of a macro-call. • Symbolic dummy argument are retained on macro-name to enable the macro processor to handle argument replacement byname rather by position.
  • 56.
  • 57.
    Algorithm • Pass1 ofmacro processor makes a line-by-line scan over its input. • Set MDTC = 1 as well as MNTC = 1. • Read next line from input program. • If it is a MACRO pseudo-op, the entire macro definition except this (MACRO) line is stored in MDT. • The name is entered into Macro Name Table along with a pointer to the first location of MDT entry of the definition. • When the END pseudo-op is encountered all the macro-defns have been processed, so control is transferred to pass2
  • 58.
    MDTC 1 MNTC 1 MDTCMDTC+1 MDTC MDTC+1 Enter Macro Name and Current value of MDTC in MNT
  • 59.
    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
  • 60.
  • 61.
    Pass structure ofMacro assembler
  • 62.