Presentation is loading. Please wait.

Presentation is loading. Please wait.

Macro Processors.

Similar presentations


Presentation on theme: "Macro Processors."— Presentation transcript:

1 Macro Processors

2 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.

3 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 macro) Compiler or Assembler obj

4 Macro Definition copy code parameter substitution
conditional macro expansion macro instruction defining macros

5 Structure of Simple Macro -- Example
Source MACRO XYZ …..; macro Header .. } ;Macro Body …. MEND ; Macro ends MEND XYZ --- XYZ ;Macro Call

6 Structure of parameterized Macro -- Example
Source MACRO XYZ para1, para2…..; macro Header .. } ;Macro Body …. MEND ; Macro ends MEND XYZ --- XYZ 1,2,….. ;Macro Call

7 Example of macro expansion

8 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

9 Example 1: Simple Macro ( Find the Expanded code)
Line no. Instructions Operands 1 Macro XYZ 2 Load A 3 Add B 4 Mend 5 PQR 6 X 7 Y 8 9 ABC 10 P 11 Q 12

10 Example 1: Simple Macro ( Find the Expanded code
Line no. Instructions Operands 13 Mult C 14 Div D 15 XYZ macro call 16 Add 17 ;macro call 18 ABC 19 Store M 20 PQR ; macro call 21 End

11 MNT( Macro Name Table) Name of the Macro Number of Parameters
Starting Row index in MDT Ending Row index in MDT XYZ 1 2 PQR 3 4 ABC 5 6

12 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

13 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 7 Load P 8 Add q 9

14 MNT( Modified Macro Name Table)
Name of the Macro Number of Parameters Starting Row index in MDT XYZ 1 PQR 4 ABC 7

15 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 7 8 Load p 9 Add q 10 Store m 11 Load x 12 Add y 13 End

16 Example 1: Parameterized Macro( Find the Expanded code)
Line no. Instructions Operands 1 Macro XYZ &a,&b 2 Load &a 3 Add &b 4 Mend 5 ABC 6 P 7 q 8 9 XYZ &a, &b ,&c 10 X 11 12 Sub &c

17 Example 1: Parameterized Macro( Find the Expanded code)
Line no. Instructions Operands 13 Mult y 14 Store &b 15 Mend 16 Load M 17 ABC 18 XYZ 3,4 19 5,6,7 End

18 MDT( Macro Definition Table)
Line No. Instructions( Macro Body) 1 Load #1 2 Add #2 3 MEND 4 5 6 7 Load x 8 Add #1 9 Sub #3 10 Mult y 11 Store #2 12

19 MNT(Macro Name Table) Name of the Macro Number of Parameters
Starting Row index in MDT XYZ 2 1 PQR 4 3 7

20 Formal Vs Positional Parameter List for Macro XYZ (with 3 parameters)
Formal Parameter Positional Parameter &a #1 &b #2 &c #3

21 Actual Vs Positional Parameter List for Macro XYZ (with 3 parameters)
Formal Parameter Positional Parameter 5 #1 6 #2 7 #3

22 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

23 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.

24 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 .

25 Summary of Activities of single pass macro processors
Mode activity d=0, e=0 d=1, e=0 d=0, e=1 d=1, e=1 Read from INPUT FILE MDT ? Write into OUTPUT FILE

26 Data structures used by macroprocessor
Pass 2: Intermediate file Output file MNT MDT MDT pointer MNT pointer LC Actual Vs Positional para. List Pass 1: Input source file Intermediate file MNT MDT MDT pointer MNT pointer LC Formal Vs Positional para. List

27

28

29 Find the output of the following program
Line no. Label Instructions Operands 1 Read a 2 b 3 Macro Add2 &x,&y,&z 4 Load &x 5 Add &y 6 Store &z 7 Mend 8 Macro mult1 &l ,&x, &y, &z 9 =‘0’ 10 11 & l: 12 add

30 Find the output of the following program
Line no. Instructions Operands 13 Store &z 14 Load &y 15 Sub =‘1’ 16 17 Jpos &l 18 Mend 19 Add2 P,Q,R 20 Mult1 E,F,G,H 21 Endp

31 Expanded Code ( out put of Macro processor)
Line No. Expanded Code 1 Read a 2 Read b 3 Load P 4 Add Q 5 Store R 6 Load =‘0’ 7 Store H 8 E : Load H 9 add F 10 Store H 11 Load G Line No. Expanded Code 12 Sub =‘1’ 13 Store G 14 Jpos E 15 Endp

32 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

33 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

34 Macro Overloading Macro FUNCT &arg1, &arg2,&arg3 load &arg1
store &arg1 MEND FUNCT a, add,b …. FUNCT a,sub, b Endp

35 Output Expansion due to first macro call Load a add b Store a
Expansion due to second macro call sub b

36 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

37 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.

38 Nested Macro Macro within a macro.
Macro call within a macro definition Early expansion method. Late Expansion method. Macro definition within a macro definition

39 Example 1: Nested Macro( Find the Expanded code)
Line no. Instructions Operands 1 Load A 2 Store B 3 Macro Add1 &arg 4 5 Add =‘1’ 6 7 Mend 8 Macro Add5 &a1,&a2,&a3 9 C 10 D 11 Add1 5 ; nested call 12 &a1 ; nested call

40 Example 1: Nested Macro( Find the Expanded code)
Line no. Instructions Operands 13 Load &a2 14 &a3 15 Mend 16 Add5 Data1, data2,data3 17 endp

41 MNT( Macro Name Table) Name of the Macro Number of Parameters
Starting Row index in MDT Add1 1 Add5 3 5 Formal Vs Positional parameter list for macro Add1 &arg #1

42 Formal Vs Positional Parameter List for Macro Add5 (with 3 parameters)
Formal Parameter Positional Parameter &a1 #1 &a2 #2 &a3 #3

43 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 9 Store 5 10 Load #1 11

44 MDT( Macro Definition Table) Early expansion method
Line No. Instructions( Macro Body) 12 Store #1 13 Load #2 14 Load #3 15 MEND

45 Actual Vs Positional Parameter List for Macro Add1(with 1 parameters)
Actual Parameter Positional Parameter 5 #1

46 Actual Vs Positional Parameter List for Macro Add5(with 3 parameters)
Actual Parameter Positional Parameter Data1 #1 Data2 #2 Data3 #3

47 Expanded Code ( out put of Nested Macro processor by Early expansion method )
Line No. Expanded Code 1 load A 2 Store B 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 Line No. Expanded Code 12 Load Data3 13 Endp

48 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

49 Expanded Code ( out put of Nested Macro processor by Late expansion method )
Line No. Expanded Code 1 load A 2 Store B 3 Load C 4 Store D 5 Load 5 6 Load =‘1’ 7 Store 5 8 Load Data1 9 10 Store Data1 11 Load Data2 Line No. Expanded Code 12 Load Data3 13 Endp

50 Example 2: Nested Macro( Find the Expanded code)
Line no. Instructions Operands 1 Macro XYZ &a 2 Load 3 Mend 4 Macro PQR &a,&b 5 &b 6 7 PQR add,6 8 XYZ,7 9 Endp

51 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 …….. Outer Adder ,add

52 Nested macro definition
Line no. Instructions Operands Outer Substractor, sub ……. …… Adder a,b,c Substractor m,n,o …. Endp

53 Expanded Code Line No. Expanded Code 1 Load a 2 Add b 3 Store c Load m
Sub n Store o

54 Ex 2: Nested macro definition
Line no. Instructions Operands 1 Macro A &f1 2 Load 3 Macro B &f2 4 Store 5 Mend B 6 Mend A 7 A a1 8 B b1 9 Endp

55 MDT( Macro Definition Table)
Line No. Instructions( Macro Body) 1 Load (#1,1) 2 Store (#2,1) 3 MEND 4 Mend

56 Expanded Code Line No. Expanded Code 1 Load a1 2 Store b1 3 Endp

57 Algorithm for Nested macro processor
REFER TO DOC DOCUMENT.

58 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

59 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 &arg2 5 AIF (&count = 2). FINI 6 &arg3 7 .FINI AGO .Over 8 .Over Mend …………..

60 Ex : Conditional macro Expansion (Find expanded code)
Line no. Instructions Operands …… 12 VARY Loop1,3,data1,data2,data3 ….. 15 Loop2,2,data1,data2 ………………… 18 Loop3,1,data1 Endp

61 Expanded Code due to call at Line no 12
Loop1 Add data1 2 Add data2 3 Add data3

62 Expanded Code due to call at Line no 15
Loop2 Add data1 2 Add data2

63 Expanded Code due to call at Line no 18
Loop3 Add data1

64 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

65 Ex 2 : Conditional macro Expansion (Find expanded code)
Line no. Instructions Operands 10 Div &a4 11 Store &a5 12 Mend ……. ………………………………. ….. 15 SECOND A,B,C,D,E ………………………….. ………………………. 18 A,A,B,C,D Endp

66 Expanded Code due to call at Line no 15
Load A 2 Add B 3 Mul C 4 Div D 5 Store E

67 Expanded Code due to call at Line no 18
Mul B 2 Div C 3 Store D

68 Recursive Macro Recursive Macro is nothing but call to a macro say “XYZ” inside the definition of “XYZ” itself.

69 MACRO Vs FUNCTIONS Macro Functions (or Subroutines) Definition
Macros are single-line abbreviations of a small sequence of instructions. Functions are blocks of code that are to be executed repeatedly by the program Lines of Code Macros are used when number of repeating instructions is very small (3 to 5 instructions) Functions can be used for any length of code that is to be executed repeatedly. Use of CALL mnemonic Only the macro name is used to call the macro CALL mnemonic is used along with the function name (as its operand), to call the function

70 Time of processing Macros are processed at compilation time Functions are processed during execution time Processing the call Macro calls are replaced by corresponding sequence of instructions; these instructions are not actually executed. When a function is called, its corresponding instructions are actually executed. Housekeeping operations No housekeeping operations (pushing and popping on the stack) are needed. The return address is pushed onto stack, when a function is called and is popped back when the function returns Need of a Preprocessor Macro calls are not recognized by the assembler. So, a separate Macro Processor is needed to expand them Functions (or Subroutines) are executed by the Assembler itself (as Procedures) Return value Macros cannot return a value Functions can return a value Size of the program Macro calls are expanded inline. So, they increase the size of the program Functions do not increase the size of the program Speed of execution Since there are no housekeeping operations involved, programs using macros execute slightly faster than the ones using functions Housekeeping operations make programs using functions execute at lesser speed.

71 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.

72 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.

73 AIF is used as a conditional jump; AGO is used for unconditional jump.
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.

74 Thank You!!!


Download ppt "Macro Processors."

Similar presentations


Ads by Google