Open In App

Macro Processor

Last Updated : 02 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Macro instruction is a notational convenience for the programmer. For every occurrence of macro, the whole macro body or macroblock of statements gets expanded in the main source code. Thus Macro instructions make writing code more convenient. 

macro definition may be written using formal parameters . A macro call on such a macro specifies actual parameters. 

Salient features of Macro Processor: 

  • Macro represents a group of commonly used statements in the source programming language.
  • Macro Processor replaces each macro instruction with the corresponding group of source language statements. This is known as the expansion of macros.
  • Using Macro instructions programmer can leave the mechanical details to be handled by the macro processor.
  • Macro Processor designs are not directly related to the computer architecture on which it runs.
  • Macro Processor involves definition, invocation, and expansion.

 Macro Expansion can be performed  by using two kinds of language processors and when we use of macro name with a set of actual parameters is replaced by same code generated from its body .This is called macro expansion.

macro expansion can be performed two ways:

  1. macro assembler 
  2. macro pre-processor
    Macro Assembler :  It performs expansion of each macro call in a program into a sequence of assembly language statements and also assembles the resultant assembly language program.
    Macro pre-processor:  It only processes the macro call . Other statements are processes with the help of assembler a macro pre-processor merely performs expansion of macro in program.   It produces an assembly program in which a macro call has been replaced by statements that resulted from its expansion but statements that were not macro calls have been retained in their original from . This program can be assembled by using assembler.  

The macro preprocessor operates as follows: If  the first statement in the program input to its macro header statement , it knows that one or more macro definitions exist in  the program. It processes and stores all macro definitions in its own data program as follow.     

Macro Definition and Expansion: 

Line                 Label                 Opcode                 Operand
  
5                    COPY                  START                  0
10                   RDBUFF                MACRO                  &INDEV, &BUFADR
15                   
.
.
90
95                                         MEND

Line 10: RDBUFF (Read Buffer) in the Label part is the name of the Macro or the definition of the Macro. &INDEV and &BUFADR are the parameters present in the Operand part. Each parameter begins with the character &. 

Line 15 – Line 90: From Line 15 to Line 90 Macro Body is present. Macro directives are the statements that make up the body of the macro definition. 

Line 95: MEND is the assembler directive that means the end of the macro definition. 

Macro Invocation: 

Line                 Label                 Opcode                 Operand
  
180                  FIRST                 STL                    RETADR
190                  CLOOP                 RDBUFF                 F1, BUFFER
200                   
.
.
255                                         END                    FIRST

Line 190: RDBUFF is the Macro invocation or Macro Call that gives the name of the macro instruction being invoked and F1, BUFFER are the arguments to be used in expanding the macro. The statement that forms the expansion of a macro are generated each time the macro is invoked. 

 

Features of Macro Facility:

Macro call leads to macro expansion.

  • Macro call statement is replaced by a sequence of assembly statement in macro expansion.
  • Two key notions are used in macro expansion

 a) Expansion time control flow

b) Lexical substitution.

âš« Algorithm for macro expansion

1. Initialise the Macro Expansion Counter (MEC) 2. Check the statement which is pointed by MEC is not a MEND statement.

a) If the statement is model statement

then

expand the statement and
 increment the MEC by 1
 else

MEC = new value specified in the statement;

3. Exit from the macro expansion.

Tasks involved in macro expansion:

1. Identify the macro calls in the program.

 2. The values of formal parameters are identified.

3. Maintain the values of expansion time variables declared in a macro. 

4. Expansion time control flow is organized.

5. Determining the values of sequencing symbols.

6. Expansion of a model statement is performed.

• Each macro invocation statement will be expanded into the statements that form the body of the macro. Arguments from the macro invocation are substituted for the parameters in the macro prototype (according to their positions).

1. In the definition of macro: Parameter.

2. In the macro invocation: Argument.

  • Comment lines within the macro body will be deleted. Macro invocation statement itself has been included as a comment line. The label on the macro invocation statement has been retained as a label on the first statement generated in the macro expansion. We can use a macro instruction in exactly the same way as an assembler language mnemonic.
  • Each parameter begins with the character and which facilities the substitution of parameters during macro expansion.
  • The macro name and parameters define a pattern or prototype for the macro instruction used by the programmer. Body of the macro definition is defined by the MACRO directive statements. Macro expansion generate these statements. End of the macro definition is defined by the MEND assembler directive.
  • Macro invocation statement that gives the name of the macro instruction being invoked and the arguments to be used in expanding the macro.

 Each macro invocation statement has been expanded into the statements that form the body of the macro, with the arguments from the macro invocation substituted for the parameters in the macro prototype. Parameters and arguments, both are associated with one another according to their positions. After macro processing, the expanded file can be used as input to the assembler.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads