Macro Processor

5,591 views 56 slides Jul 19, 2021
Slide 1
Slide 1 of 56
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56

About This Presentation

MAcro and MAcro Processor


Slide Content

Macro Language & Macro Processor Prepared by Mrs. Saranya V, M.Sc., M.Phil . Assistant Professor, BCA Dept., TICM

Content MACRO LANGUAGE AND THE MACRO PROCESSOR: Macroinstruction, Features of macro Facility, Macro instruction arguments, conditional macro Expansion, macro calls within macros, macro Instructions defining macros, Implementation, Statement of problem, implementation of a restricted facility, A two pass algorithm. A single pass algorithm, implementation of macro calls within macros. Implementation within an assembles. Mrs. Saranya V,AP/BCA,TICM

Macro Instructions Sometimes need to repeat some blocks of code several times in our program/task. In such cases to avoid this repetition the system software provides us with special component i.e ., MACRO Macro instructions is a notational convenience for the programmer that it allows the programmer to write a short hand version of a program Source code (with macro) Macro processor Expanded code Object code Compiler or Assembler Mrs. Saranya V,AP/BCA,TICM

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 . The design of macro processor is generally machine independent. Every programming language will be using the macro processor Mrs. Saranya V,AP/BCA,TICM

Macro Pros & Cons Advantage: The frequent use of macros can reduce programmer-induced errors. A macro allows to define instruction sequences that are used repetitively throughout the program. The scope of the symbol is limited to that macro only. Disadvantage: The size of the program is little higher. 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. Mrs. Saranya V,AP/BCA,TICM

Macro vs Sub-routine Used when maximum processor speed is required. Used when amount of memory consumed is less important. Each invocation includes the assembly code associated with the macro in-line Speeds up the execution of algorithm Should be used when certain procedures are frequently executed. Used when use of memory must be kept minimum. Each invocation requires only the code necessary to call subroutine Program size can be reduced Macro Sub-routine Mrs. Saranya V,AP/BCA,TICM

Macro-Processor Functions There are 3 main functions of macro processor. They are 1. Macro definition 2. Macro calls or invocation 3. Macro expansion Mrs. Saranya V,AP/BCA,TICM

Macro Definition Macro definition attaches a name to a sequence of instruction/operations Structure of macro-definition MACRO starting of macro definition [ ] macro_name ……………. ……………. sequence of instruction ……………. MEND ending of macro definition Mrs. Saranya V,AP/BCA,TICM

Macro Definition(Contd..) The macro definition starts with MACRO pseudo op code. It indicates beginning of the macro definition . The Macro-definition terminated with the MEND pseudo op code. A 1, data A 2, data A 3, data ….. ….. A 1, data A 2, data A 3, data ….. ….. A 1, data A 2, data A 3, data ….. ….. Data DC f „5 ‟ Mrs. Saranya V,AP/BCA,TICM

Macro Definition(Contd..) In the above example the sequence of A 1, data A 2, data A 3, data It repeats 3 times MACRO permits as to attach a name to this sequence and to use the name in its place then the macro definition will be MACRO Add A 1, data A 2, data A 3, data MEND MACRO=> is a pseudo op indicates beginning of definition Add => is the name of the macro MEND => is a pseudo op indicate end of the macro definition Between the name of the Macro Add and MEND we have the sequence of instruction. Mrs. Saranya V,AP/BCA,TICM

MACRO Calls or Invocation Once the macro has been defined, the use of the macro name as an operation mnemonic in an ALP is equivalent to corresponding instruction sequence Sequence of instruction are simply substituted at the point of call or macro name is referred as macro call In the above mentioned example sequence will be repeated thrice, then we need to replace sequence by macro name like: MACRO Add A 1, data A 2, data A 3, data MEND So, the main program looks like ……. Add …… Add …… Data DC f „5‟ Mrs. Saranya V,AP/BCA,TICM

Macro Expansions Whenever the program needs the instruction in the place of macro name then we need macro expansion . The macro-processor replace each macro calls with the defined set of instructions. This process of replacement is called macro expansion. MACRO Add A 1, data A 2, data A 3, data MEND ……. Add …… Add …… Data DC f „5‟ A 1, data A 2, data A 3, data A 1, data A 2, data A 3, data Does not appear Expanded Program Source Program Mrs. Saranya V,AP/BCA,TICM

Input File Macro names Macro definitions Scan file for macros Extract macros with name and definition Scan for macro names Replace macro name with Corresponding definition Processed File Block Representation of Macro Expansion Mrs. Saranya V,AP/BCA,TICM

Features of macro facility: There are mainly 4 important feature of macro. They are 1. Macro instruction argument 2. Conditional macro expansion 3. Macro calls within macros 4. Macro instruction defining macros Mrs. Saranya V,AP/BCA,TICM

Macro instruction argument To overcome lack of flexibility problem, we use macro instructions arguments where these arguments are appears in macro call . The corresponding macro dummy arguments are appears in macro definition. Eg : A 1,data1 A 2,data2 A 3,data3 : A 1,data3 A 2,data2 A 3,data1 : data1 DC F‟5‟ data2 DC F‟6‟ Mrs. Saranya V,AP/BCA,TICM

Source MACRO INCR & arg (Macro name with A 1,&arg one argument) A 2,&arg A 3,&arg MEND : : INCR data1 (use of data1 as : operand) : INCR data 2 (use of data1 as : operand) data1 DC F’5’ data2 DC F’6’ : END F’6’ Expanded Source : : : : : A 1,data1 A 2,data1 A 3,data1 : A 1,data2 A 2,data2 A 3,data2 : : data1 DC F’5’ data2 DC Macro Instruction argument(Contd..) Mrs. Saranya V,AP/BCA,TICM

More than one argument in macro call Source MACRO INCR & arg 1, & arg 2, & arg 3, & lab 1 & lab1 A 1, &arg1 A 2, &arg2 A 3, &arg3 MEND : : : LOOP1 INCR data1, data2, data3 : : : LOOP2 INCR data3, data2, data1 : : data1 DC F ‘5’ data2 DC F ‘6’ data3 DC F ‘7’ Expanded source : : : : : : : : LOOP 1 A 1, data 1 A 2, data 2 A 3, data 3 : : LOOP 2 A 1, data3 A 2, data2 A 3, data1 : data1 DC F ‘5’ data2 DC F ‘6’ data3 DC F ‘7’ Mrs. Saranya V,AP/BCA,TICM

Two ways of specifying arguments to a macro call Positional argument Argument are matched with dummy arguments according to order in which they appear. Eg:INCR A,B,C “A ‟ replaces first dummy argument “B ‟ replaces second dummy argument “C ‟ replaces third dummy argument Mrs. Saranya V,AP/BCA,TICM

Two ways of specifying arguments to a macro call(Contd..) Keyword Argument This allows reference to dummy arguments by name as well as by position . e.g. INCR &arg1 = A,&arg3 = C, &arg2 =‟B‟ (Or) INC R & arg1= A, &arg2 = B, &arg2 =‟C‟ Mrs. Saranya V,AP/BCA,TICM

Conditional macro expansion This allows conditional selection of machine instructions that appear in expansion of macro call. The sequence of macro expansion can be reordered or change based on some conditions. There are 2 Important macro processor pseudo op. they are AIF AGO Mrs. Saranya V,AP/BCA,TICM

AIF It is a conditional branching pseudo op the format of AIF is AIF<expression >.<sequencing label> Where expression is a relational expression, it involves strings, numbers etc. If the expression evaluates to true them the control transferred to the statements containing the sequencing label Otherwise, the control transferred to the next statement followed by AIF AIF statement does not appears in the expanded source code Mrs. Saranya V,AP/BCA,TICM

AGO It is an unconditional branching pseudo op the format of AGO is AGO .<Sequencing label> It is conditional transfer control to the statement containing sequencing label Each and every label must be starting with a .(dot) operator. AGO statement does not appear in the expanded source code. Ex: Loop1 A 1,data1 A 2, data2 A 3, data3 Loop2 A 1,data3 A 2, data2 Loop3 A 1, data1 Mrs. Saranya V,AP/BCA,TICM

Fini FINI is a macro label. Labels starting with a period(.) such as .FINI, are macro labels and don't appear in the output of the macro processor. AIF (& COUNT EQ 1).FINI It directs the macro processor to skip to statement labeled FINI if the parameter corresponding to & COUNT is 1,otherwise the macroprocessor continues with the statement following the AIF pseudo-op Mrs. Saranya V,AP/BCA,TICM

Conditional macro expansion(Contd..) Source Program MACRO & arg0 INCR & count, & arg1, & arg 2, & arg3 & arg0 A 1, &arg1 AIF (& Count EQ.1). FINAL A 2,& arg2 AIF (&count EQ 2). FINAL A 3, &arg3 .FINAL MEND : LOOP1 INCR 3, data1, data2, data3 : : LOOP2 INCR 2, data3, data2 : LOOP3 INCR 1, data1 : data1 DC ‘5’ data2 DC ‘6’ data3 DC ‘7’ Expanded Program LOOP1 A 1, data1 A 2, data2 A 3, data3 LOOP2 A 1, data3 A 2, data2 : LOOP3 A 1, data1 : data1 DC ‘5’ data2 DC ‘6’ data3 DC ‘7’ : Mrs. Saranya V,AP/BCA,TICM

Macro calls within the macros Macro calls can be called only after being defined. Sometimes one macro will call another macro such a concept is called macro calls “Macro can be called within the definition of the another macro this is also referred as nested macros ” The definition of called macro should appear before the macro definition which contains the macro call . Macro ADD1 & arg L 1,&arg A 1,=F‟1‟ ST 1,& arg MEND Macro ADDS &arg1,&arg2,&arg3 ADD1 &arg1 ADD1 &arg2 ADD1 &arg3 MEND Mrs. Saranya V,AP/BCA,TICM

Macro calls within the macros(Contd..) Source code Expanded code(Level 1) Expanded code(Level 2) MACRO ADD 1, & arg L 1, & arg A 1, = F ‘10’ ST 1, & arg MEND MACRO ADDS &arg1, &arg2, arg3 ADD1 &arg1 ADD1 &arg2 ADD1 &arg3 MEND : : ADDS data1, data2, data3 : : data 1 DC F‘5’ data 2 DC F‘6’ data 3 DC F‘7’ END Expansion of ADDS : : ADD1 data 1 ADD1 data 2 ADD1 data 3 Expansion of ADD1 L 1, data 1 A 1, = F „10‟ ST 1, data 1 L 1, data 2 A 1, = F „10‟ ST 1, data 2 L 1, data 3 A 1, = F „10‟ ST 1, data 3 data1 DC F‘5’ data2 DC F‘6’ data3 DC F‘7’ END Mrs. Saranya V,AP/BCA,TICM

Macro definitions within Macro definition This feature helps to defining a group of similar macro‟s using a single macro instruction It is possible to have macro definition with in the body of macro. The inner macro definition is not defined until the outer macro has been called . MACRO DEFINE &SUB MACRO &SUB &Y CNOP 0,4 BAL 1,*+8 DC A(&Y) L 15,=V(&SUB) BALR 14,15 MEND MEND Definition of macro &SUB Definition of macro DEFINE Mrs. Saranya V,AP/BCA,TICM

Implementation of a macro processor The macro process taken as input an ALP which contains macro definition and macro calls. Then it transforms to expanded source without consisting macro definition and macro calls is through the translator it will be convert as an object code. Mrs. Saranya V,AP/BCA,TICM

Implementation(Contd..) There are four basic task that a macro-processor must perform 1. Recognize macro definition A macro instruction processor must recognize macro definition identified by MACRO & MEND pseudo – operations. 2. Save the Definition The processor must store the macro – definitions which will be needed at the time of expansion of calls. Mrs. Saranya V,AP/BCA,TICM

Implementation(Contd..) 3. Recognize the Macro call The processor must recognize macro calls that appear as operation mnemonics . 4. Expand calls & substitute arguments The macro call is replaced by the macro definition. The dummy arguments are replaced by the actual data . Mrs. Saranya V,AP/BCA,TICM

Implementation of a restricted facility: a two pass algorithm The following assumptions are made. Macro processor is the functionality independent of the assembler. Output from the macro processor will be fed into the assembler. Macro calls or definitions within macro definition are not allowed because of its complications. Mrs. Saranya V,AP/BCA,TICM

Two pass algorithm(Contd..) Macro processor performs expansion using 2 passes Pass I- Processing macro definitions It examines every operation code Save all macro definitions in a macro definition table and macro name table Save a copy of the input text , minus macro definition Pass II- Processing macro calls and expansion Identify macro calls Replace each macro name with the appropriate text from macro definition(expand macro call) Mrs. Saranya V,AP/BCA,TICM

Two pass algorithm- SPECIFICATION OF DATA BASE PASS I- Databases The input macro source deck Output macro source deck copy for use by pass2 Macro Definition Table(MDT) Macro Name Table(MNT) Macro Definition Table Counter(MDNTC) Macro Name Table Counter(MNTC) Argument List Array(ALA ) Mrs. Saranya V,AP/BCA,TICM

Two pass algorithm- SPECIFICATION OF DATA BASE(contd..) Pass 2- Databases The copy input macro source deck Output expanded source deck copy for use by assembler Macro Definition Table(MDT) created by pass1 Macro Name Table(MNT) created by pass1 Macro Definition Table Pointer(MDNTP) Argument List Array(ALA) Mrs. Saranya V,AP/BCA,TICM

SPECIFICATION OF DATA BASE FORMAT Arguments List array: It maintains the details about the parameters. It is used in both pass1 and pass2, but the functions are reverse in both the passes. ALA in pass1 In this when the macro definition are stored the arguments in definition are replaced by index markers . # is the index marker, which is preceded by the dummy argument. Mrs. Saranya V,AP/BCA,TICM

SPECIFICATION OF DATA BASE Format(Contd..) ALA in pass2 In this argument in the macro call are substituted for the index marker stored in macro definition In the above example the macro call is Loop Add data1, data2, data3 Mrs. Saranya V,AP/BCA,TICM

Macro Definition table It is used to store the body of macro definition The size of MDT is 80-bytes per entry It will be read every line in the definition except MACRO Mrs. Saranya V,AP/BCA,TICM

Macro Name Table It is used to store the names of the defined macros Total size of MNT is 12-bytes per entry Each MNT entry consists of A character string (the macro name) (8 bytes)& A pointer (index )(4 bytes) to the entry in MDT that corresponds to the beginning of the macro definition.( MDT index) Mrs. Saranya V,AP/BCA,TICM

Two pass macro processor algorithm pass 1: Macro definition Pass1 is used to store and processing macro definitions 1. Initialize or set MDTC as well as MNTC=1 2. Read the first line from the source code 3. A) If it is a macro pseudo op The entire macro definition except macro is stored in MDT Read next line from source Enter macro name and MDTC value stored in MNT Then increment MNTC value Prepare ALA Enter the macro name line in MDT then increment MDTC value Read next line from source i.e., sequence then activate ALA in pass1 Substitute index marker for the dummy argument Enter these values in MDT then increment MDTC value Repeat these procedure until we get MEND pseudo op code 3. B) If it is not a macro pseudo op code then copy the source code into pass2 4. If it is a MEND pseudo op code read the next line from source code otherwise the same procedure will be continue Mrs. Saranya V,AP/BCA,TICM

MACRO Add A 1, data A 2, data A 3, data MEND Mrs. Saranya V,AP/BCA,TICM

Two pass macro processor algorithm pass 2: Macro calls & Expansion Pass2 is used for specifying macro calls and expansion 1. Read next line from the source code copied by pass1 2. Search macro name table for match with that code a ) Check whether you have encountered macro name then the MDT index of that macro name entered into MDTP Then activate argument list array Increment MDTP value by one Read next line from MDT and substitute arguments for macro call Substitute arguments from macro calls Check whether you have encountered MEND pseudo-op i )If it is a MEND pseudo op then, read the next statement from the source otherwise write the expanded source code ii) If it is not a macro name directly write into expanded source code b)If you have not encountered a macro call,write into expanded source card file. c) Check whether you have encountered END pseudo-op i)If End,transfer expanded source file to the assembler for further processing. ii) If not END, Goto step1. Mrs. Saranya V,AP/BCA,TICM

Mrs. Saranya V,AP/BCA,TICM

A single pass algorithms for macro definitions within macros There are 2 additional variables are used in simple one pass macro processor. They are: MDI [Macro Definition Indicator] It is used to keep track of macro calls It has 2 states ON and OFF While expanding a macro calls, then MDI=ON otherwise MDI=OFF MDLC [Macro Definition Level Counter] It is used to keep track of macro definition It is a counter for MACRO and MEND pseudo op When macro pseudo op is encountered then MDLC is incremented by 1 and It is decremented by 1 when MEND pseudo op is encountered Mrs. Saranya V,AP/BCA,TICM

A single pass algorithms for macro definitions within macros Mrs. Saranya V,AP/BCA,TICM

Mrs. Saranya V,AP/BCA,TICM

Implementation of macro calls within macros If a macro call is encountered during the expansion of a macro, the macro processor will have to expand the included macro call and then finish expanding the closed macro. This implementation involves recursion. Hence something is done store the status of macro while they are expanded as the outer macro needs to be expanded form a position from where it was left while calling the inner macro. We make use of stack to store the state of macro when a macro call is made with in another macro. MACRO ADD 1, & arg L 1, & arg A 1, = F ‘10’ ST 1, & arg MEND MACRO ADDS &arg1, &arg2, arg3 ADD1 &arg1 ADD1 &arg2 ADD1 &arg3 MEND Mrs. Saranya V,AP/BCA,TICM

Organization of stack frame Mrs. Saranya V,AP/BCA,TICM

One pass macro processor capable of handling macro calls within macro definitions Mrs. Saranya V,AP/BCA,TICM

Read function for macro expansion Mrs. Saranya V,AP/BCA,TICM

One pass macro processor Mrs. Saranya V,AP/BCA,TICM

chronology of processing example Mrs. Saranya V,AP/BCA,TICM

MACRO PROCESSOR COMBINED WITH ASSEMBLER PASS 1 There are two ways of implementing macro-processor with assembler It can be added as pre-processor to an assembler, making a complete pass over the input text before pass1 of the assembler Or It can be implemented within pass1 of the assembler Mrs. Saranya V,AP/BCA,TICM

MACRO PROCESSOR COMBINED WITH ASSEMBLER PASS 1 - Pros & Cons Advantages: 1. Many function need not be implanted twice i.e. read a card, test a statement etc. 2. There is less overhead, since there are no intermediate files needed over here. 3. More flexible since the user can make use of more features. Disadvantages: 1. The integration may be too complex and lengthy. 2. Problem with integration as two groups of persons for pass 1 of assembler and macro processor. Mrs. Saranya V,AP/BCA,TICM

MACRO PROCESSOR COMBINED WITH ASSEMBLER PASS 1 Mrs. Saranya V,AP/BCA,TICM

MACRO PROCESSOR COMBINED WITH ASSEMBLER PASS 1 - Algorithm 1. READ (similar to read of macro processor with stack) 2. Search POT 3. If pseudo code found a. Then find type i. MACRO 1. Then use macro defintion with stack. 2. GOTO step 1. ii. Other 1. Then similar task as that of pass 1 assembler. 2. GOTO step 1. iii. END GOTO pass 2 of assembler. 4. Else search the MNT if found a. Set up macro call process b. GOTO step 1. 5. Else search MOT 6. Process machine instructions. 7. GOTO step 2. Mrs. Saranya V,AP/BCA,TICM

Completion of Unit 3 Mrs. Saranya V,AP/BCA,TICM