Macro in system programming is a feature of a programming language that allows programmers to define an abbreviation for a piece of code. The programmer uses this abbreviation wherever he/she has to repeat the corresponding piece of code in the program.
The macro processor scans this source program with macros. Wherever the processor comes across the macro abbreviations, it simply replaces the abbreviations with the appropriate piece of code. While scanning the source code, the macro processor does not identify any error in the code. It simply expands the macro abbreviations.
Macros are beneficial in reducing the length of the code. It also reduces the effort of programmers in writing repetitive code. Let us explore macros in detail in the section ahead.
Content: Macros in System Programming
- What is Macro?
- What is Macro Processor?
- Difference Between Macro and Function
- Macro Examples
- Advantages and Disadvantages
What is Macro in System Programming?
Macros allow a programmer to define a new operation or to declare data. We refer to this process as a macro definition. Now a macro call in a program is an invocation of that particular macro definition. We refer to this process as macro invocation. Macro invocation leads to a program-generating activity that substitutes the macro call with the appropriate macro definition. We refer to this process as macro expansion.
Now that we know there are three activities involved in the use of macros that are macro definition, macro invocation and macro expansion. Let us explore two more actions performed during macro expansion.
- Lexical Substitution
- Semantic Expansion
Like function definitions, we write macro definitions using formal parameters. Lexical substitution replaces the formal parameters with the actual parameters present in the macro call.
This process generates a sequence of statements. These statements are specifically tailored to meet the requirements of the corresponding macro call.
To define a macro you have to just choose an abbreviation that suits your macro definition. The macro definition starts with the MACRO keyword followed by the macro abbreviation. The MACRO abbreviation is followed by formal parameters.
The following definition shows you how you can define a macro.
macro_name Formal Parameters
The body of macro consists of the macro statements that either define operations or data. the body of a macro ends with the MEND keyword.
- MACRO – This keyword identifies the beginning of the macro definition.
- MEND – This keyword identifies the end of a macro definition.
- Parameters – It includes the parameters passed to the macro. Every parameter has to begin with ‘&’.
- Body – The body includes all the statements that the processor will substitute in response to the macro call in the program.
What is Macro Processor?
While creating a program a programmer may have implemented macros in it. So, before a compiler or an assembler converts it to a target code these macros must be processed. However, the assembler uses a preprocessor to process these macros in the source program. We refer to this preprocessor as the macro processor.
Types of Macro Processor
- A macro processor designed for use by assembly language programmers.
- A macro processor is used with a high-level programming language.
- A general-purpose macro processor that is not associated with a particular language.
What is the Macro Processor Function?
The main task of the macro processor is to expand the macro wherever it is used in the source program. Besides expanding the macros, the processor does not analyze the text it handles.
The processor is not concerned with the meaning of the involved statements during the expansion of macros. The basic function of a macro processor is as follows:
- Identify the definition of macro.
- Macro invocation (macro calls).
- Expansion with substitution of the parameter.
Identify Macro Definition
First, we have to define a macro with a name and instructions. The
macro_name Formal parameters
Macro Invocation (Macro Calls)
Macro invocation is a statement in a program that provides the name of the macro definition that has been invoked. Along with the macro name it also provides the parameters or arguments that we have to provide for expanding the macro.
Each time when the macro processor come across a macro_name/abbreviation, the processor substitutes the definition of that macro for the abbreviation. The abbreviation expands to the statement that forms the body of that corresponding macro.
Difference Between Macro and Function
|Basis for Comparison||Macros||Functions|
|Processed by||Macros are processed by the macro processor||Functions are processed by the compilers|
|Type Checking||There is no type checking||Type checking is done|
|Code Length||Macro invocation increases the code length||Function invocation does not increase the code length|
|Execution Speed||Using macro makes the execution faster||Using function reduces the execution speed|
|Program Counter||No transfer of program counter||The program counter is transferred to the location of the function|
|Use of Stack||Do not require stack as there is no transfer control||Require stack for transferring control|
|Usage||Use macro when you have to repeat a small code in the program||Use function when you have to reuse large code|
|Assembly Time||It requires more assembly time||It requires less assembly time|
|Error Detection||Macro processors do not check any compile time error||The compiler checks the syntactic and semantic errors|
|Invocation||Macro invocation never returns a value||Function invocation may or may not return a value|
MACNAME &R1, &R2, &R3
LOAD &R1, &R2
ADD &R1, &R3
STORE &R1, &R2
MACNAME A, B, C
MACNAME A, B, C
LOAD A, B
ADD A, C
STORE A, B
Advantages and Disadvantages of Macro
- Using macro reduces the size of code.
- Programs with macros don’t reduce their execution speed.
- Reduces programmer’s effort in writing repetitive code.
- When the macro processor replaces the abbreviation with the macro definition the length of the code increases prior to compilation.
So, this is all about the macro in system programming. It facilitates the programmer to assign an abbreviation to a piece of code in the program. We refer to this process as the macro definition. The abbreviation is then used wherever the corresponding macro statements are required. We refer to this process as macro invocation.
Macro invocation leads to macro expansion where the abbreviation is replaced by the macro definition itself. The macro expansion is performed by the macro processor.
Shashank Agasimani says
Great Article on Macros and Macro Processing, it helped me understand these concepts with ease!