MACROS AND
MACRO
PROCESSORS
Macros
● Macro instructions or Macros are single line abbreviations for groups of instructions.
● Single instruction is used to represent a block of code.
● For every occurrence of this one line macro instruction, the macro processing assembler will substitute the
entire block.
Macro Instructions
● Macro is an abbreviation for a series of operations.
● Example:
Start of definition
Macro name
A 1,DATA MACRO
A 2,DATA INCR INCR
A 3,DATA A 1,DATA
Repeated Sequence to be
A 2,DATA
code A 3,DATA
abbreviated
A 1,DATA INCR
A 2,DATA MEND
A 3,DATA
End of definition
Macros in C
#include <stdio.h> #include <stdio.h>
#define Square(x) ((x)*(x)) //Macro definition not included
int main(void) int main(void)
{ {
int a;
int a;
printf(“enter a no : ”);
printf(“enter a no : ”);
scanf(“%d”,&a);
scanf(“%d”,&a);
((a)*(a));
Square(a);
}
}
Features of Macro Facility
● Macro Instruction Arguments
● Conditional Macro Expansion
● Macro calls within Macros
● Macro Instruction defining Macros
Macro Instruction Arguments
● Macro calls replaces the call by a block of code.
● No flexibility to modify code that replaces the call.
● Extension for providing arguments or parameters in macro call.
● Macro instruction argument (dummy arguments) are used in definition.
● It is specified in the macro name line and distinguished by ‘&’
● Arguments that are not specified, are presumed blank by macro processor.
Macro Instruction Arguments
Original Source Original Source
Expanded Source Expanded Source
(single argument) (Multiple argument)
MACRO MACRO
INCR&ARG INCR &ARG1,&ARG2
A 1, &ARG A 1, &ARG1
A 2, &ARG A 1,DATA1 A 2, &ARG2
A 3, &ARG MEND A 1,DATA1
A 2,DATA1
MEND A 2,DATA2
A 3,DATA1
INCR
INCRDATA1 DATA1,DATA2 A 1,DATA2
A 1,DATA2 A 2,DATA1
A 2,DATA2 INCR
INCRDATA2 A 3,DATA2 DATA2,DATA1
Conditional Macro Expansion
● AIF and AGO permit conditional reordering of the sequence of macro expansion.
● AIF
○ Conditional branch
○ Performs arithmetic test and branches if condition is true
● AGO
○ Unconditional branch (similar to ‘go to’ statement)
● Machine instructions that appear in the expansion of a macro call can be selected based on condition.
● Labels inside a Macro start with a period (.) eg: .FINISH
Conditional Macro Expansion
Original Source Source with Macro
MACRO
&ARG0 VARY &COUNT, &ARG1, &ARG2,&ARG3
&ARG0 A 1, &ARG1
Loop1 A 1,DATA1 AIF (&COUNT EQ 1).FINISH
A 2,DATA2 A 2,&ARG2
A 3,DATA3 AIF (&COUNT EQ 2).FINISH Expanded Source
A 3,&ARG3
.FINISH MEND
Loop2 A 1,DATA3 Loop1 A 1,DATA1
A 2,DATA2 A 2,DATA2
Loop1 VARY 3,DATA1,DATA2,DATA3
A 3,DATA3
Loop3 A 1,DATA1 Loop2 VARY 2,DATA3,DATA2 Loop2 A 1,DATA3
A 2,DATA2
Loop3 VARY 1,DATA1
Loop3 A 1,DATA1
Macro calls within Macros
● Also known as nested macro calls.
● A macro can be called within another macro.
● A macro can call itself (using AIF or AGO) so long as it doesn’t go into an infinite loop.
● Macro calls within macros can have several levels
Macro calls within Macros
Source Expanded Source Expanded Source
MACRO (Level 1) (Level 2)
ADD1 &ARG
L 1,&ARG
A 1,=F’1’ Expansion of Expansion of
ST 1,&ARG ADDS ADD1
MEND
MACRO
ADDS &ARG1,&ARG2,&ARG3
L 1,DATA1
ADD1 &ARG1
ADD1 DATA1 A 1,=F’1’
ADD1 &ARG2
ST 1,DATA1
ADD1 &ARG3
L 1,DATA2
MEND
ADD1 DATA2 A 1,=F’1’
ST 1,DATA2
ADDS
L 1,DATA3
DATA1,DATA2,DATA3
ADD1 DATA3 A 1,=F’1’
ST 1,DATA3
Macro Instruction defining Macros
● Macros can be defined within a macro.
● Inner macro definition is not defined until after the outer macro has been called.
● Group of macros can be defined for subroutine calls with some standardized calling sequence.
● Individual macros have names of the associated subroutines (as given by the argument &SUB).
Macro Instruction defining Macros
MACRO
DEFINE &SUB Macro name: DEFINE
MACRO
&SUB &Y Dummy macro name
Definition CNOP 0,4 Align boundary
of macro Definition
BAL 1,*+8 Set reg 1 to parameter list pointer
DEFINE of macro
DC A(&Y) Parameter list pointer
&SUB
L 15,=V(&SUB) Address of subroutine
BALR1 4,15 Transfer control to subroutine
MEND
MEND
DEFINE COS
COS AR
BAL 1,*+8
DC A(AR) Address of AR
L 15,=V(COS) V denotes Address of external symbol
BALR14,15
2 pass Macro Processor
● Assumptions
○ Functionally different from assembler
○ No nested macro calls or macro within macro definitions
● Assembler scans and processes lines of text.
○ A line can refer to another line by its address or name
○ Address or name must be available to assembler
● Macro definitions do not refer to anything outside themselves.
○ Macro calls refer only to macro definitions
2 pass Macro Processor
● Pass 1: handles macro definitions
○ Input macro source deck
○ Output macro source deck copy (for pass 2)
○ Macro Definition Table (MDT) (to store definition body)
○ Macro Name Table (MNT) (to store names of defined macros)
○ Macro Definition Table Counter (MDTC) (next available entry in the MDT)
○ Macro Name Table Counter (MNTC) (next available entry in the MNT)
○ Argument List Array (ALA) (to substitute index markers for dummy arguments before storing macro definition)
2 pass Macro Processor
● Pass 2: handles macro calls and expansion
○ Copy of input macro source deck
○ Output expanded source deck (i/p for assembler)
○ MDT
○ MNT from Pass 1
○ ALA
○ Macro Definition Table Pointer (MDTP) (indicates the next line of text to be used during macro expansion)
2 pass Macro Processor
● Macro Definition Table
○ Table of text lines
○ 80 byte string entries
○ Each line of macro definition is stored in MDT
○ MACRO is omitted but MEND is kept to indicate the end.
○ Name line is kept to facilitate keyword replacement
Index Card
15 &LAB INCR &ARG1, &ARG2, &ARG3
16 #0 A 1,#1
17 A 2,#2
18 A 3,#3
19 MEND
MDT
2 pass Macro Processor
● Macro Name Table (MNT)
○ Each entry consists of a character string (macro name) and pointer to entry in MDT
● Argument List Array (ALA)
○ Dummy arguments in definition replaced by index markers (eg. #1) in pass 1
○ Index markers replaced by arguments in macro call
8 bytes 4 bytes 8 bytes
Index Name MDT Index Index Argument
0 “LOOP1bbb”
3 “INCRbbbb” 15
1 “DATA1bbb”
2 “DATA2bbb”
MNT 3 “DATA3bbb”
ALA
Macro Pass 1
PASS 1
MDTC <― 1
MNTC <― 1
No
Yes
Read next END GO TO PASS 2
source card psedo-op?
MACRO No Write copy of
psedo-op? source card
Yes Read next
source card
Read next
source card Substitute index
MDTC <― MDTC + 1 notation for
Enter Macro name arguments No
and current value of Enter macro MEND
MDTC in MNT entry name card into Enter line into psedo-op?
no MNTC MDT MDT
Yes
MNTC <― MNTC + 1 Prepare ALA MDTC <― MDTC + 1
Macro Pass 2
PASS 2
Read next
source card
(from pass 1) No
END Yes Supply expanded
Search MNT
pseudo-op? source file to
for match
assembler processing
with opcode
MACRO No Write into expanded
name found? source card file
Yes MEND Yes
Substitute arguments
from macro call pseudo-op?
MDTP <― MDT index
from MNT entry No
Get line from MDT
MDTP <― MDTP + 1
Example
MACRO
&LAB INCR &ARG1, &ARG2, &ARG3
&LAB A 1, &ARG1
A 2, &ARG2
A 3, &ARG3
MEND
.
.
LOOP1 INCR DATA1,DATA2,DATA3
.
.
LOOP2 INCR DATA1,DATA2,DATA3
.
.
Example Pass1
MNT ALA
Index Card MDT Index Index Argument
1 INCRbbbb 1 0 LOOP1bbb
1 DATA1bbb
2 DATA2bbb
MDT 3 DATA3bbb
Index Card
1 &LAB INCR&ARG1,&ARG2,&ARG3
2 #0 A 1,#1 MDTC 6
4
5
2
3
1
3 A 2,#2 MNTC 1
2
4 A 3,#3
5 MEND
Example Pass 2
MNT ALA
Index Card MDT Index Index Argument
1 INCRbbbb 1 0 LOOP1bbb
1 DATA1bbb
2 DATA2bbb
MDT 3 DATA3bbb
Index Card
2 #0 A
LOOP1 1,#11,DATA1
A
3 A 2,#2
2,DATA2 MDTP 5
4
2
1
3
4 A 3,#3
3,DATA3
5 MEND
THE END!
Have a nice day!