0% found this document useful (0 votes)
51 views

Macros and macro processor

Uploaded by

drmadhavpvt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Macros and macro processor

Uploaded by

drmadhavpvt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Macros and Macro

Processors
Presented by: Foram Thakor
Overview
● Introduction
● Macro Definition and Call
● Macro Expansion
● Nested Macro Calls
● Advanced Macro Facilities
● Design of a Macro Preprocessor
● Design of a Macro Assembler
● Functions of a Macro Processor
● Basic Tasks of Macro Processor
● Design Issues of Macro Processors
Macros
● A macro is a sequence of instructions or statements that can be reused
multiple times in a program.
● Instead of writing the same code repeatedly, you define it as a macro and call
it when needed.
● Macros help in reducing code duplication and improving maintainability.
● A macro is a facility for extending a programming language .
● A macro either defines a new operation or a new method for declaring data .
● The language processor replaces a call on a macro by a sequence of
statements that implements the defined operation or the method of declaring
data.
● Many languages that support macro: C, C++
Macro Definition
● Macro definitions are typically located at the start of a program.
● A macro definition is enclosed between a macro header statement(MACRO)
and a macro end statement(MEND)
● Format of macro definition:

MACRO

macroname <formal parameters>

body

MEND
Macro Definition (Contd.)
● A macro definition consist of macro prototype statement and body of macro.
● A macro prototype statement declares the name of a macro and its
parameters. MACRO indicates the beginning of macro definition and
parameters indicates the list of formal parameters.
● Parameters is of the form &parameter1, &parameter2,…Each parameter
begins with ‘&’.
● Whenever we use the term macro prototype it simply means the macro name
along with its parameters.
● Body of macro consist of statements that will generated as the expansion of
macro.
Macro Definition (Contd.)
● Consider the following macro definition:
MACRO
SUM &X,&Y
LDA &X
MOV B
LDA &Y
ADD B
MEND
● Here, the macro named SUM is used to find the sum of two variables passed to it.
Macro Call (or Macro Invocation)
● A macro invocation statement (a macro call) gives the name of the macro
instruction being invoked and the arguments to be used in expanding the
macro.
● When a macro is invoked in the code, it is referred to as a macro call. The
macro processor replaces this call with the macro body.
● The format of macro invocation:

macroname p1, p2,...pn

● The above defined macro can be called as SUM P,Q


Macro Expansion
● 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.
● The arguments and parameters are associated with one another according to
their positions.
● The first argument in the macro invocation corresponds to the first parameter
in the macro prototype, etc.
● Comment lines within the macro body have been deleted, but comments on
individual statements have been retained.
● Macro invocation statement itself has been included as a comment line.
Maceo Expansion (Contd.)
Types of Statements
1. Macro Prototype
● A macro prototype defines the macro’s name, parameters, and the structure
of the macro. It serves as a template or blueprint for the macro, specifying
how it should be invoked and what arguments it requires.
1. Model Statement
● Model statements are the actual instructions within a macro that perform
operations. They are the core executable statements that define the actions to
be carried out when the macro is expanded.
1. Macro Preprocessor Statements
● Macro preprocessor statements are special directives used within macros to
control the behavior of the macro processor. These statements manage
macro expansion, conditional logic, iteration, and other preprocessing tasks.
Nested Macro Calls
● A nested macro call occurs when one macro calls or invokes another macro
within its definition.
● This creates a hierarchical or nested structure where macros are expanded
within other macros.
● This allows for complex functionality to be modularized and reused,
enhancing the flexibility and efficiency of macro programming.
● The macro that contains the nested call is called outer macro and the macro
that is called in the nested call is inner macro.
● The pre-processor performs expansion of nested macro calls LIFO order
Key Concepts of Nested Macro Calls
1. Macro Expansion:
● When a macro is called, the macro processor replaces the macro name with
its corresponding body (the set of instructions).
● In nested macro calls, during the expansion of one macro, another macro is
invoked and expanded.
● The expansion process continues until all macro calls are fully resolved.
1. Hierarchy of Macros:
● Outer Macro: The macro that contains another macro call within its body.
● Inner Macro: The macro that is called from within the outer macro.
Key Concepts of Nested Macro Calls
3. Parameters:
● Both the outer and inner macros can have parameters.
● These parameters are passed and expanded separately for each macro call.
● The arguments passed to each macro are processed independently, allowing
different values to be used in different levels of nested calls.
4. Macro Processor Behavior:
● The macro processor must handle each level of nesting separately.
● It expands the inner macro first, replacing it with its instructions, before
continuing to expand the outer macro.
● This requires the macro processor to keep track of multiple expansion
contexts
Example of Nested Macro calls
Macro Definition: Outer Macro Calling Nested Macro Expanded Macro call:
PUSH_ALL MACRO
PROCESS_DATA MACRO START:
PUSH AX
PUSH BX PUSH_ALL ; Call to nested macro PUSH AX
PUSH CX PUSH BX
; Perform some operations here
PUSH CX
PUSH DX
POP_ALL ; Call to nested macro PUSH DX
ENDM
; Perform some operations
POP_ALL MACRO ENDM here
POP DX POP DX
POP CX POP CX
Macro Calling: POP BX
POP BX
START: POP AX
POP AX
PROCESS_DATA ;
ENDM
Advanced Macro Facilities
● Advanced macro facilities enhance the capabilities of macros in programming,
allowing for more complex and efficient code generation.
● These facilities include features such as flow control alterations,
conditional expansions, expansion time variables, and loops during
macro expansion.
1. Alteration of Flow Controls
● This facility allows macros to control the flow of execution during their
expansion.
● It can include conditional statements and loops, enabling macros to behave
like control structures.
● This is useful when the behavior of the macro needs to change based on
specific conditions, reducing redundancy in code.
Advanced Macro Facilities
#SYNTAX

IF <condition>

; code to include if condition is true

ELSE

; code to include if condition is false

ENDIF
Advanced Macro Facilities
2. Expansion Time Variables
● These are variables that can be defined within a macro and retain values
during the macro's expansion.
● They allow for dynamic behavior and state management within the macro.
● Useful for maintaining state or counters within a macro, such as keeping track
of how many times a macro has been called.
#SYNTAX
MACRO_NAME MACRO
SET variable = value
; use variable
ENDM
Advanced Macro Facilities
3. Conditional Expansions
● This feature allows certain parts of the macro to be included or excluded based on
conditions evaluated at the time of expansion.
● This is beneficial for generating different code paths based on compile-time
configurations or flags, allowing for more flexible and adaptable code.
#SYNTAX
MACRO_NAME MACRO
IF condition
; statements to include
ENDIF
ENDM
Advanced Macro Facilities
4. Local Variables in Macros

● Local variables (LCL) and global variables (GBL) allow defining variables that
are either local to the macro or accessible globally across multiple macros.
● Use local variables to store temporary values that are only needed during
macro expansion and should not interfere with variables outside the macro.

#SYNTAX

LCL &LOCAL_VAR ; Define a local variable

GBL &GLOBAL_VAR ; Define a global variable


Design of a Macro Preprocessor
● A macro preprocessor is a tool that processes macros before the actual
compilation or assembly of code.
● It replaces macros with their defined instructions or code segments and
performs various tasks like conditional compilation, file inclusion, and macro
expansion.
● The macro preprocessor accepts an assembly program containing macro
definitions and calls, and translates it into an equivalent assembly program
without any macro definitions or calls.
● The output program can then be processed by an assembler to generate the
target object program.
Design of a Macro Preprocessor
Key tasks performed by Macro Preprocessor
1. Identify Macro Calls:
● Recognize when a macro call is made in the program.
1. Determine Values of Formal Parameters:
● Capture the values passed to the macro.
1. Maintain Expansion Time Variables:
● Keep track of variables declared during macro expansion.
1. Organize Control Flow:
● Manage how the macro executes based on the parameters.
1. Determine Sequencing Symbols:
● Handle symbols that control the order of execution.
1. Perform Expansion of Model Statements:
● Replace macro calls with the corresponding code.
Data Structures used by Macro Preprocessors
1. Macro Name Table (MNT):
● Stores macro names, the number of positional and keyword parameters,
pointers to the Macro Definition Table (MDT), and other relevant metadata.
1. Parameter Name Table (PNT):
● Contains names of formal parameters.
1. Expansion Time Variable Table (EVT):
● Maintains information about expansion time variables.
1. Sequencing Symbol Table (SST):
● Contains sequencing symbols that control the flow of macro execution.
1. Keyword Parameter Default Table (KPDT):
● Holds keyword parameters and their default values.
Data Structures used by Macro Preprocessors
6. Macro Definition Table (MDT):

● Stores the actual body of the macro definitions.

7. Actual Parameter Table (APT):

● Holds the actual parameters passed during macro calls.

8. Expansion Time Variable Name Table (EVNT):

● Contains names of expansion time variables.

9. Sequencing Symbol Name Table (SSNT):

● Contains name/s of all the available sequencing symbol in a macro definition.


Data Structures used by Macro Preprocessors
TABLE NAME ENTRY FIELDS

Macro Name Table (MNT) - name: Macro name


- pp: Number of positional parameters
- kp: Number of keyword parameters
- ev: Number of expansion time variables
- mdtPtr: Pointer to Macro Definition Table (MDT)
- kpdtPtr: Pointer to Keyword Parameter Default Table (KPDT)
- sstPtr: Pointer to Sequencing Symbol Table (SST)

Parameter Name Table (PNT) - paramName: Name of the formal parameter

Expansion Time Variable Table (EVT) - varName: Name of the expansion time variable
Data Structures used by Macro Preprocessors
TABLE NAME ENTRY FIELDS

Sequencing Symbol Table (SST) -symbolName: Name of the sequencing symbol

Keyword Parameter Default Table - paramName: Name of the keyword parameter


(KPDT) - defaultValue: Default value of the keyword parameter

Macro Definition Table (MDT) - label: Label of the statement


- opcode: Opcode of the statement
- operand: Operand of the statement

Actual Parameter Table (APT) - paramName: Name of the actual parameter


- value: Value of the actual parameter

Expansion Time Variable Name Table - varName: Name of the expansion time variable
(EVNT)
Algorithm for Macro Expansion
● Initialization: Set the Macro Expansion Counter (MEC) to the starting point of
the macro.
● Check for MEND: While the statement pointed to by MEC is not a MEND
statement:
○ If the statement is a model statement, expand it and increment MEC.
○ If it is a control statement, update MEC based on the control logic specified.
● Parameter Substitution: Replace formal parameters in the model statements
with actual parameters from the APT.
● Output Generation: Generate the output assembly code by substituting the
expanded statements in place of the macro calls.
● End Expansion: Once all macro calls are processed, the preprocessor
outputs the final assembly program without any macro definitions.
Example of Macro Preprocessor Working
Consider a macro definition and call

MACRO

CLEARMEM &X, &N, &REG=AREG

LCL &M

&M SET 0

MOVER &REG, = ‘0’

.MORE MOVEM &REG, &X+&M

&M SET &M+1

AIF (&M NE N).MORE

MEND

Macro call

CLEARMEM AREA,10
Design of Macro Assembler
● A macro assembler extends the functionality of a standard assembler by
supporting macro instructions.
● These macros are essentially placeholders for code snippets that can be
reused throughout the assembly code.
● The macro assembler handles macro definitions and expansions, providing a
more flexible and modular approach to assembly programming.
● The assembler performs this translation through a series of passes over the
source code, allowing it to resolve symbols and generate the corresponding
machine instructions.
Key Components of a Macro Assembler
1. Macro Definition Table (MDT)
● Stores the definitions of macros, including the macro body with placeholders
for parameters.
● Facilitates macro expansion by providing the code to substitute for each
macro invocation.
1. Macro Name Table (MNT)
● Maintains a list of macro names and their corresponding identifiers or
addresses in the MDT.
● Allows quick lookup of macro names to retrieve their definitions.
1. Argument List Array (ALA)
● Stores the arguments provided for each macro invocation.
● Supplies the macro processor with arguments to replace placeholders in the
macro body.
Different Types of Assemblers
1. Single-Pass Assembler
● Functionality: Scans the source code only once to generate machine code.
● Process:
○ Reads each line of the source code.
○ Translates instructions into machine code immediately.
○ Requires all symbols to be defined before they are used (no forward
references).
● Advantages: Faster, as it only requires one scan of the source code.
● Disadvantages: Limited flexibility due to the need for all symbols to be
defined beforehand.
Different Types of Assemblers
2. Multi-Pass Assembler

● Functionality: Scans the source code multiple times.


● Process:
○ First Pass: Builds a symbol table containing all symbols and their addresses.
○ Subsequent Passes: Uses the symbol table to translate the assembly code
into machine code.
● Advantages: More flexible, allowing symbols to be used before they are
defined.
● Disadvantages: Slower, as it requires multiple scans of the source code.
Functions of Macro Processors

● A macro processor is responsible for processing macros in assembly


language programs.
● It provides a means to automate the repetition of code sequences by allowing
the use of macros.
● The macro processor works independently of the assembler, providing macro
expansion before the actual assembly process.
● A macro processor is a component that processes macro definitions and
macro calls in assembly language programs.
Key Functions of Macro Processor
1. Macro Definition Handling:
● The macro processor identifies macro definitions in the source code and
stores them in the Macro Definition Table (MDT).
● It also records macro names and their addresses in the Macro Name Table
(MNT).
1. Macro Invocation Handling:
● When the macro processor encounters a macro invocation, it retrieves the
corresponding macro definition from the MDT and the arguments from the
Argument List Table (ALT).
● It substitutes actual arguments into the macro body.
Key Functions of Macro Processor (Contd.)
3. Macro Expansion

● The macro processor expands the macro invocations by substituting


arguments into the macro body and generates the expanded code.
● This process simplifies repetitive code.

4. Nested Macro Handling

● The macro processor can handle nested macro calls, where one macro
invokes another macro within its body.
● It manages the expansion in a recursive manner to handle multiple levels of
macro calls.
Key Functions of Macro Processor (Contd.)
5. Conditional Macro Expansion

● Some macro processors support conditional macro expansions, where certain


code is expanded based on conditions like argument values or external
parameters.

6. Error Detection

● The macro processor identifies errors related to incorrect macro invocations,


such as missing arguments or invalid syntax.
● It provides appropriate error messages for troubleshooting.
Basic Tasks of Macro Processor
1. Macro Definition Recognition:
● The macro processor scans the source code and identifies macro definitions
using specific keywords like MACRO and MEND.
1. Parameter Substitution:
● The macro processor replaces formal parameters (like &ARG1, &ARG2) in
the macro definition with actual arguments provided during invocation.
1. Expansion of Macro Calls:
● It replaces macro invocations with the actual code body (macro expansion) in
the source code.
Basic Tasks of Macro Processor (Contd.)
4. Maintaining Data Structures:

● The processor maintains key data structures:


● MDT (Macro Definition Table): Stores macro bodies.
● MNT (Macro Name Table): Holds macro names and their locations in the
MDT.
● ALT (Argument List Table): Maps actual arguments to formal parameters
during macro invocation.

5. Output of Expanded Code:

● The final output from the macro processor is the expanded source code,
which can then be passed to the assembler.
Difference B/W Macro Processor & Macro Assembler
FEATURES MACRO PROCESSORS MACRO ASSEMBLER

Purpose Processes macro definitions and Translates assembly language into


expansions machine code

Functionality Expands macros and handles Performs macro expansion and


parameter substitution assembles the resulting code

Output Produces an assembly program with Generates the final machine code from

expanded macros the assembly program

Independence Can operate independently or as part of


Typically integrated with the assembler
an assembler
Difference B/W Macro Processor & Macro Assembler
Execution Time Processes macros at assembly Executes during the assembly

time process

Error Handling Checks for macro definition and Handles errors related to

usage errors assembly language syntax

Data Structures Used Utilizes tables like MNT, MDT, Uses symbol tables, opcode

APT, etc. tables, etc.

Macro Expansion Focuses solely on expanding Expands macros and generates

macros machine code


Design Issues of Macro Processor
Design Issue Description Proposed Solutions

Macro Definition and Clear syntax for defining macros and Consistent format (e.g., MACRO,

Invocation identifying calls MEND), robust parsing


Management of formal and actual Use APT and PNT data
Parameter Handling
parameters structures
Expansion Time Control Organizing control flow during Implement sequencing symbol

Flow expansion management


Error Detection and Identifying errors in macro definitions Incorporate error-checking

Handling or calls mechanisms


Efficiently managing memory for Optimize memory allocation
Memory Management
storing definitions and parameters strategies
Design Issues of Macro Processor
Recursive Macro Supporting macros that invoke Maintain state information to

Definitions themselves prevent infinite loops

Managing increased code size due to Minimize unnecessary


Code Size Management
expansions expansions; provide inline options

Ensuring design is independent of


Machine Independence Focus on high-level abstractions
hardware architecture

Integration with Seamless integration between macro Define clear interfaces for

Assemblers processor and assembler output/input

You might also like