• Skip to main content
  • Skip to primary sidebar
  • Computer Architecture
  • Computer Networks
  • DBMS
  • OS
  • Software Engineering
  • Security
  • OOT
binary-terms-logo

Binary Terms

The Computer Science & IT Guide

Macro in System Programming

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

  1. What is Macro?
    • Macro Definition
  2. What is Macro Processor?
    • Types of Macro Processor
    • Macro Processor Function
  3. Difference Between Macro and Function
  4. Macro Examples
  5. 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.

  1. Lexical Substitution
  2. Semantic Expansion

Lexical substitution

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.

Semantic Expansion

This process generates a sequence of statements. These statements are specifically tailored to meet the requirements of the corresponding macro call.

Macro Definition

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

macro_name Formal Parameters

.

.

body

.

.

MEND

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.

Macro in System programming

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:

  1. Identify the definition of macro.
  2. Macro invocation (macro calls).
  3. Expansion with substitution of the parameter.

Identify Macro Definition

First, we have to define a macro with a name and instructions. The

MACRO

macro_name Formal parameters

.

.

body

.

.

MEND

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.

macro_name Actual Parameters

Macro Expansion

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 ComparisonMacrosFunctions
Processed byMacros are processed by the macro processorFunctions are processed by the compilers
Type CheckingThere is no type checkingType checking is done
Code LengthMacro invocation increases the code lengthFunction invocation does not increase the code length
Execution SpeedUsing macro makes the execution fasterUsing function reduces the execution speed
Program CounterNo transfer of program counterThe program counter is transferred to the location of the function
Use of StackDo not require stack as there is no transfer controlRequire stack for transferring control
UsageUse macro when you have to repeat a small code in the programUse function when you have to reuse large code
Assembly TimeIt requires more assembly timeIt requires less assembly time
Error DetectionMacro processors do not check any compile time errorThe compiler checks the syntactic and semantic errors
InvocationMacro invocation never returns a valueFunction invocation may or may not return a value

Macro Examples

#Macro DefinitionMACRO

MACNAME &R1, &R2, &R3

LOAD &R1, &R2

ADD &R1, &R3

STORE &R1, &R2

MEND

#Macro Invocation

MACNAME A, B, C

#Macro Expansion

MACNAME A, B, C

LOAD A, B

ADD A, C

STORE A, B
Macro formal parameter

Advantages and Disadvantages of Macro

Advantages

  • Using macro reduces the size of code.
  • Programs with macros don’t reduce their execution speed.
  • Reduces programmer’s effort in writing repetitive code.

Disadvantages

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

Related Terms:

  1. Data Encryption Standard (DES)
  2. Process in Operating System
  3. Process Control Block (PCB)
  4. Scheduling in Operating System
  5. Difference Between fork() and exec() System Call

Reader Interactions

Comments

  1. Shashank Agasimani says

    26th December 2022 at 9:25 pm

    Great Article on Macros and Macro Processing, it helped me understand these concepts with ease!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Most Searched Terms

  • Directory Structure
  • Contiguous Memory Allocation
  • Addressing Mode and its Types
  • Pipelining
  • Vector Processing
  • Register Organization
  • Direct Memory Access (DMA)
  • Process Control Block (PCB)
  • Swapping in Operating System
  • Relational Data Model

Recent Additions

  • Types of Processors
  • Demand Paging in Operating System
  • Agents and Environment in Artificial Intelligence
  • Array Processor in Computer Architecture
  • Deadlock Avoidance in Operating System
  • Fragmentation in Operating System
  • Assembly Language in Computer
  • Control Signals in Computer Architecture
  • Memory Hierarchy in Computer Architecture
  • Ethernet in Computer Networks

Categories

  • Artificial Intelligence
  • Cloud Computing
  • Compiler Design
  • Computer Architecture
  • Computer Networks
  • Data Warehouse and Mining
  • DBMS
  • Object Oriented Technology
  • Operating System
  • Security
  • Software Engineering

Copyright © 2025 · Binary Terms · Contact Us · About Us · Privacy