Macro Processors 1 Introduction Concept » A macro instruction is a notational convenience for the programmer » It allows the programmer to write shorthand version of a program (module programming) » Macro is a Single line abbreviation for set of instructions . » The macro processor replaces each macro invocation with the corresponding sequence of statements i.e called as Macro expantion. 2 Functions of Macro Processor Recognize macro definitions Save the macro definition Recognize macro calls Expand macro calls Source Code (with macro) Macro Processor Expanded Code(without Compiler or Assembler obj macro) 3 Macro Definition copy code parameter substitution conditional macro expansion macro instruction defining macros 4 Structure of Simple Macro -- Example Source MACRO XYZ …..; macro Header .. .. } ;Macro Body …. MEND ; Macro ends MEND XYZ --. -----XYZ ;Macro Call 5 Structure of parameterized Macro -Example Source MACRO XYZ para1, para2…..; macro Header .. .. } ;Macro Body …. MEND ; Macro ends MEND XYZ --. -----XYZ 1,2,….. ;Macro Call 6 Example of macro expansion 7 Classification of macros Simple macro Nested Macro » Macro call within a macro definition » Macro definitions inside the Macro definitions of another Macro Conditional Macro Recursive Macro 8 Example 1: Simple Macro ( Find the Expanded code) Line no. Instructions Operands 1 Macro XYZ 2 Load A 3 Add B 4 Mend 5 Macro PQR 6 Load X 7 Add Y 8 Mend 9 Macro ABC 10 Load P 11 Add Q 12 Mend 9 Example 1: Simple Macro ( Find the Expanded code Line no. Instructions Operands 13 Mult C 14 Div D 15 XYZ macro call 16 Add C 17 XYZ ;macro call 18 ABC ;macro call 19 Store M 20 PQR ; macro call 21 End 10 MNT( Macro Name Table) Name of the Macro Number of Parameters Starting Row index in MDT Ending Row index in MDT XYZ 0 1 2 PQR 0 3 4 ABC 0 5 6 11 MDT( Macro Definition Table) Line No. Instructions( Macro Body) 1 Load A 2 Add B 3 Load X 4 Add Y 5 Load P 6 Add q 12 MDT( Modified Macro Definition Table) Line No. Instructions( Macro Body) 1 Load A 2 Add B 3 MEND 4 Load X 5 Add Y 6 MEND 7 Load P 8 Add q 9 MEND 13 MNT( Modified Macro Name Table) Name of the Macro Number of Parameters Starting Row index in MDT XYZ 0 1 PQR 0 4 ABC 0 7 14 Expanded Code( out put of Macro processor) Line No. Expanded Code 1 Mult c 2 Div d 3 Load a 4 Add b 5 Add c 6 Load a 7 Add b 8 Load p 9 Add q 10 Store m 11 Load x 12 Add y 15 Example 1: Parameterized Macro( Find the Expanded code) Line no. Instructions Operands 1 Macro XYZ 2 Load &a 3 Add &b 4 Mend 5 Macro ABC 6 Load P 7 Add q 8 Mend 9 Macro XYZ 10 Load X 11 Add &a 12 Sub &c &a,&b &a, &b ,&c 16 Example 1: Parameterized Macro( Find the Expanded code) Line no. Instructions Operands 13 Mult y 14 Store &b 15 Mend 16 Load 17 ABC 18 XYZ 3,4 19 XYZ 5,6,7 M End 17 MDT( Macro Definition Table) Line No. Instructions( Macro Body) 1 Load #1 2 Add #2 3 MEND 4 Load #1 5 Add #2 6 MEND 7 Load x 8 Add #1 9 Sub #3 10 Mult y 11 Store #2 12 MEND 18 MNT(Macro Name Table) Name of the Macro Number of Parameters Starting Row index in MDT XYZ 2 1 PQR 0 4 XYZ 3 7 19 Formal Vs Positional Parameter List for Macro XYZ (with 3 parameters) Formal Parameter Positional Parameter &a #1 &b #2 &c #3 20 Actual Vs Positional Parameter List for Macro XYZ (with 3 parameters) Formal Parameter Positional Parameter 5 #1 6 #2 7 #3 21 Expanded Code for parameterized Macro ( out put of Macro processor) Line No. Expanded Code 1 Load m 2 Load p 3 Add q 4 Load 3 5 Add 4 6 Load x 7 Add 5 8 Sub 6 9 Mult y 10 Store 7 11 End 22 Activities of single pass macro processors Read the source program statements from input file till EOF. If Source statement is a normal ALP statement then dump it as it is to output file.( Definition mode flag (d) and expansion mode flag(e) are set to 0), Reading is from input file and writing it into output file If a statements corresponds to macro definition , then, » Dump the macro body along with MEND ,starting from next available row index in MDT after replacement of positional parameters. » Reflect the corresponding changes into MNT. – Here d is set( d=1) and e is reset(e=0). Reading is from input file and writing it into MDT. Formal Vs Positional parameter list is used . d is reset( d=0) on MEND. 23 If a statements corresponds to macro call » Consult MNT to get index in MDT and read statements one by one from MDT till MEND , replace it with Actual Parameter and write it into output file. – Here e is set( e=1) and d is reset(d=0). Reading is from MDT and writing it into Output file. Actual Vs Positional parameter list is used . e is reset( e=0) when MEND is encountered in MDT . 24 Summary of Activities of single pass macro processors Mode d=0, e=0 d=1, e=0 d=0, e=1 d=1, e=1 activity Read from INPUT FILE INPUT FILE MDT ? Write into OUTPUT FILE MDT OUTPUT FILE ? 25 Data structures used by macroprocessor Pass 1: Input source file Intermediate file MNT MDT MDT pointer MNT pointer LC Formal Vs Positional para. List Pass 2: •Intermediate file •Output file •MNT •MDT •MDT pointer •MNT pointer •LC •Actual Vs Positional para. List 26 27 28 Find the output of the following program Line no. Label Instructions Operands 1 Read a 2 Read b 3 Macro 4 Load &x 5 Add &y 6 Store &z 7 Mend 8 Macro mult1 &l ,&x, &y, &z 9 Load =‘0’ 10 Store &z Load &z add &x 11 12 & l: Add2 &x,&y,&z 29 Find the output of the following program Line no. Instructions Operands 13 Store &z 14 Load &y 15 Sub =‘1’ 16 Store &y 17 Jpos &l 18 Mend 19 Add2 P,Q,R 20 Mult1 E,F,G,H 21 Endp 30 Expanded Code ( out put of Macro processor) Line No. Expanded Code Line No. Expanded Code 1 Read a 12 Sub =‘1’ 2 Read b 13 Store G 3 Load P 14 Jpos E 4 Add Q 5 Store R 15 Endp 6 Load =‘0’ 7 Store H 8 E : Load H 9 add F 10 Store H 11 Load G 31 Default value Parameters in macro Macro XYZ &a, &b=10, &c Laod &a add &b Store &c MEND .. .. XYZ 3, , 5 .. .. XYZ 6,7,8 .. Endp Here &b=10 is Default Parameter 32 keyword parameters in macro Macro TEST &a=, &b=, &op=ADD, &lab= &lab : Laod &a &op &b Store &a MEND .. .. TEST b=5, lab=BACK, a=4 …….. Endp OUTPUT : BACK : load 4 Add 5 store 4 Here &a= is a keyword parameter 33 Macro Overloading Macro FUNCT &arg1, &arg2,&arg3 load &arg1 &arg2 &arg3 store &arg1 MEND … … FUNCT a, add,b …. FUNCT a,sub, b … Endp 34 Output Expansion due to first macro call Load a add b Store a Expansion due to second macro call Load a sub b Store a 35 Concatenation of macro parameter Macro SUM &a, &b load X+&a Add X+&b Mend .. .. SUM A1,A2 … .. Endp Expanded code : Load XA1 Add XA2 36 Error Handling by Macroprocessor Error 1 : Call to a macro which is not defined . Error 2 : parameter mismatch in call and definition Error 3 : Multiple definition of same macro. Error 4 : Unexpected End of File. 37 Nested Macro Macro within a macro. » Macro call within a macro definition – Early expansion method. – Late Expansion method. » Macro definition within a macro definition 38 Example 1: Nested Macro( Find the Expanded code) Line no. Instructions Operands 1 Load A 2 Store B 3 Macro Add1 &arg 4 Load &arg 5 Add =‘1’ 6 Store &arg 7 Mend 8 Macro Add5 &a1,&a2,&a3 9 Load C 10 Store D 11 Add1 5 ; nested call 12 Add1 &a1 ; nested call 39 Example 1: Nested Macro( Find the Expanded code) Line no. Instructions Operands 13 Load &a2 14 Load &a3 15 Mend 16 Add5 17 endp Data1, data2,data3 40 MNT( Macro Name Table) Name of the Macro Number of Parameters Starting Row index in MDT Add1 1 1 Add5 3 5 Formal Vs Positional parameter list for macro Add1 &arg #1 41 Formal Vs Positional Parameter List for Macro Add5 (with 3 parameters) Formal Parameter Positional Parameter &a1 #1 &a2 #2 &a3 #3 42 MDT( Macro Definition Table) Early expansion method Line No. Instructions( Macro Body) 1 Load #1 2 Add =‘1’ 3 Store #1 4 MEND 5 Load c 6 Store d 7 Load 5 8 Add =‘1’ 9 Store 5 10 Load #1 11 Add =‘1’ 43 MDT( Macro Definition Table) Early expansion method Line No. Instructions( Macro Body) 12 Store #1 13 Load #2 14 Load #3 15 MEND 44 Actual Vs Positional Parameter List for Macro Add1(with 1 parameters) Actual Parameter Positional Parameter 5 #1 45 Actual Vs Positional Parameter List for Macro Add5(with 3 parameters) Actual Parameter Positional Parameter Data1 #1 Data2 #2 Data3 #3 46 Expanded Code ( out put of Nested Macro processor by Early expansion method ) Line No. Expanded Code Line No. Expanded Code 1 load A 12 Load Data3 2 Store B 13 Endp 3 Load C 4 Store D 5 Load 5 6 Load =‘1’ 7 Store 5 8 Load Data1 9 Add =‘1’ 10 Store Data1 11 Load Data2 47 MDT( Macro Definition Table) Late expansion method Line No. Instructions( Macro Body) 1 Load #1 2 Add =‘1’ 3 Store #1 4 MEND 5 Load c 6 Store d 7 Add1 5 8 Add1 #1 9 Load #2 10 Load #3 11 MEND 48 Expanded Code ( out put of Nested Macro processor by Late expansion method ) Line No. Expanded Code Line No. Expanded Code 1 load A 12 Load Data3 2 Store B 13 Endp 3 Load C 4 Store D 5 Load 5 6 Load =‘1’ 7 Store 5 8 Load Data1 9 Load =‘1’ 10 Store Data1 11 Load Data2 49 Example 2: Nested Macro( Find the Expanded code) Line no. Instructions Operands 1 Macro XYZ &a 2 Load &a 3 Mend 4 Macro PQR &a,&b 5 &a &b 6 Mend 7 PQR add,6 8 PQR XYZ,7 9 Endp 50 Nested macro definition Line no. Instructions Operands 1 Macro Outer &macname, &op 2 Macro &macname &x, &y, &z 3 Load &x 4 &op &y 5 Store &z 6 Mend 7 Mend …….. Outer Adder ,add 51 Nested macro definition Line no. Instructions Operands Outer Substractor, sub ……. …… Adder a,b,c Substractor m,n,o …. Endp 52 Expanded Code Line No. Expanded Code 1 Load a 2 Add b 3 Store c 1 Load m 2 Sub n 3 Store o 53 Ex 2: Nested macro definition Line no. Instructions Operands 1 Macro A &f1 2 Load &f1 3 Macro B &f2 4 Store &f2 5 Mend B 6 Mend A 7 A a1 8 B b1 9 Endp 54 MDT( Macro Definition Table) Line No. Instructions( Macro Body) 1 Load (#1,1) 2 Store (#2,1) 3 MEND 4 Mend 55 Expanded Code Line No. Expanded Code 1 Load a1 2 Store b1 3 Endp 56 Algorithm for Nested macro processor REFER TO DOC DOCUMENT. 57 Conditional Macro Expansion Expansion based on condition. 2 macro pseudo ops for conditional macro expansion are » AIF » AGO Syntax of flow of control statements: » AIF(< condition>) Conditional Label – Ex : AIF( &arg1 = &arg2). FINI » AGO Label – Ex: AGO .OVER 58 Ex : Conditional macro Expansion (Find expanded code) Line no. Instructions Operands 1 Macro VARY &arg0, &count, &arg1, &arg2 , &arg3 2 &arg0: add &arg1 3 AIF (&count = 1). FINI 4 add 5 AIF (&count = 2). FINI 6 add &arg3 7 .FINI AGO .Over 8 .Over Mend &arg2 ………….. 59 Ex : Conditional macro Expansion Line no. Instructions (Find expanded code) Operands …… 12 VARY Loop1,3,data1,data2,data3 ….. 15 VARY Loop2,2,data1,data2 ………………… 18 VARY Loop3,1,data1 ….. Endp 60 Expanded Code due to call at Line no 12 Line No. Expanded Code 1 Loop1 Add data1 2 Add data2 3 Add data3 61 Expanded Code due to call at Line no 15 Line No. Expanded Code 1 Loop2 Add data1 2 Add data2 62 Expanded Code due to call at Line no 18 Line No. Expanded Code 1 Loop3 Add data1 63 Ex 2 : Conditional macro Expansion (Find expanded code) Line no. Instructions Operands 1 Macro FIRST &arg1, &arg2 , &arg3 2 AIF (&arg1= &arg2). ONLY 3 Load &arg1 4 Add &arg2 5 .ONLY AGO .OVER 6 .Over Mul &arg3 7 Mend 8 Macro SECOND &a1, &a2,&a3,&a4,&a5 9 FIRST &a1, &a2,&a3 64 Ex 2 : Conditional macro Expansion Line no. Instructions Operands 10 Div &a4 11 Store &a5 12 Mend ……. ………………………………. ….. 15 SECOND A,B,C,D,E (Find expanded code) ………………………….. ………………………. 18 SECOND A,A,B,C,D Endp 65 Expanded Code due to call at Line no 15 Line No. Expanded Code 1 Load A 2 Add B 3 Mul C 4 Div D 5 Store E 66 Expanded Code due to call at Line no 18 Line No. Expanded Code 1 Mul B 2 Div C 3 Store D 67 Recursive Macro Recursive Macro is nothing but call to a macro say “XYZ” inside the definition of “XYZ” itself. 68 MACRO Vs FUNCTIONS Macro Functions (or Subroutines) Definition Macros are abbreviations single-line Functions are blocks of code that are to be executed of a small repeatedly by the program sequence of instructions. Lines of Code Macros are used when number of Functions can be used for any length of code that is to be repeating instructions is very executed repeatedly. small (3 to 5 instructions) Use of CALL mnemonic Only the macro name is used to CALL mnemonic is used along with the function name (as call the macro its operand), to call the function 69 Time of processing Macros are processed at compilation Functions are processed during execution time Processing the call Macro time calls are replaced by When a function is called, its corresponding sequence of instructions; corresponding instructions are actually these instructions are not actually executed. executed. Housekeeping operations No housekeeping operations (pushing The return address is pushed onto stack, and popping on the stack) are needed. when a function is called and is popped back when the function returns Need of a Preprocessor Macro calls are not recognized by the Functions (or Subroutines) are executed assembler. So, a separate Macro by the Assembler itself (as Procedures) Processor is needed to expand them Return value Macros cannot return a value Size of the program Macro calls are expanded inline. So, Functions do not increase the size of the they increase the size of the program Speed of execution Since there are no Functions can return a value program housekeeping Housekeeping operations make programs operations involved, programs using using functions execute at lesser speed. macros execute slightly faster than the ones using functions 70 Chapter Summary Macros are single-line abbreviations for a small sequence of commands. Macros appear functionally similar to functions (or subroutines). But there are many fundamental differences between them. Macros are expanded inline i.e. the macro call is replaced by corresponding sequence of instructions; these instructions are not actually executed. Functions are used to repeatedly execute a long enough sequence of instructions. Also, processing of macros does not require any housekeeping operations, whereas executing functions needs return address (and sometimes current state of the program) to be pushed onto stack. 71 Functions of Macro Processor are: » » » » Recognize the macro definition Store the macro definition Recognize the macro call Expand the macro call – Features of macro facility include: » Parameterized Macros – Passing arguments to a macro, either by position or by Keywords » Nested Macros – Using one macro from within another macro – Nested Macro Calls: Calling a macro from within another macro – Nested Macro Definition: Defining one macro inside definition of another macro. 72 » Conditionally Macro Expansion – Using AIF and AGO statements to conditionally decide which part of code to insert into the macro body. AIF is used as a conditional jump; AGO is used for unconditional jump. Recursive Macros – Calling a macro from within itself. But to limit the depth of recursion, proper conditions must be given to terminate the recursion. – A Simple Macro Processor takes two passes to completely process and expand the macros. In Pass 1, All macros used in the program get listed in MNT and their definitions get stored in MDT. ALA is used to convert formal parameters to positional parameters. In Pass 2, macro calls in the program get expanded by using MNT and MDT. ALA is used to convert positional parameters to actual parameters. 73 74