0% found this document useful (0 votes)
2 views53 pages

Understanding Macros and Processors

Chapter 4 discusses macros and macro processors, defining macros as single-line abbreviations for groups of instructions that simplify coding and reduce errors. It outlines the advantages and disadvantages of using macros, explains macro definition and call, and describes the macro expansion process, including various argument specifications and flow control. Additionally, it covers advanced macro facilities, such as expansion time variables and control flow changes.

Uploaded by

hiwot
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views53 pages

Understanding Macros and Processors

Chapter 4 discusses macros and macro processors, defining macros as single-line abbreviations for groups of instructions that simplify coding and reduce errors. It outlines the advantages and disadvantages of using macros, explains macro definition and call, and describes the macro expansion process, including various argument specifications and flow control. Additionally, it covers advanced macro facilities, such as expansion time variables and control flow changes.

Uploaded by

hiwot
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter- 4

Macros and Macro Processors


Introduction
• Macro:- single line abbreviations for group of instructions.(templates)
• Using macro, programmer can define a single “instruction” to
represent block of code. AKA metaprogramming.
• E.g. MACRO <macro name> in assembly, #define in c.
Introduction
• Use of macros provide a program generation through macro
expansion.
• Many languages provide built-in facilities for writing macros, e.g PL/I,
C, Ada and C++. Assembly languages of most computer systems also
provide such facilities.
• But when a language does not support, Generalized preprocessors
and software tools like Awk of Unix provide built-in macro facilities
for the programmer.
Introduction …
• Macros Provide two kinds of facilities:
1. Lexical expansion: it involves replacement of a character string by a
corresponding character string during a program generation.
2. Semantic expansion: Semantic expansion helps the macro
processor generate instructions that match the specific
requirements of a macro [Link] example, the macro can choose
byte instructions when given byte operands, and word instructions
when given word operands.
Advantage and Disadvantages of Macro

Advantages Disadvantages
• Simplify and reduce the amount • Size of the program, because the
of repetitive coding. preprocessor will replace all the
• Reduce the possibility of errors macros in the program by it’s
caused by repetitive coding. real definition prior to the
compilation process of the
• Make the program more program.
readable.
Example.
• We want to do a very common operation like: “increase the value stored in
memory by some amount”
• Using normal assembly: MOVE A, AREG
ADD B, AREG
MOVE AREG,A
• Instead of writing this every time, we can write macro like this: INCR A,B
• And the assembler expands this into the full 3-line sequence during
assembly.
• Macros differ from subroutines in a way that macros lead to expansion
whereas subroutines leads to execution.
Macro Definition and Call
Macro definition: consists of
1. A macro prototype statement,
2. One or more model statements, and
3. Macro preprocessor statement.
• It is enclosed between a macro header and a macro end statement.
• They are typically located at the start of a program.
Cont…
• The macro prototype statement declares the name of a macro and the names and kinds
of it’s parameters.
• A model statement is a statement from which an assembly language statement maybe
generated during macro expansion.
• A preprocessor statement is used to perform auxiliary functions during macro
expansion.
• Consider the following macro.
MACRO
INCR &MEM_VAL, &INCR_VAL, &REG
MOVER &REG, &MEM_VAL
ADD &REG, &INCR_VAL
MOVEM &REG, &MEM_VAL
MEND
Macro call
• A macro is called by writing the macro name in the mnemonic field of
an assembly statement. The macro call has the syntax
• <macro name> [<actual parameter spec>[,..]]
Macro Expansion
• A macro call leads to macro expansion.
• During macro expansion the macro call statement is replaced by a
sequence of assembly statements.
Two key notions in macro expansion are :-
1. Flow of control during expansion
2. lexical substitution.
Four ways to specify arguments to a macro call
(Parameter Substitution)
A) Positional Argument or parameter
B) Keyword arguments or parameters
C) Keyword Default parameters
D) Mixed parameters
A) Positional Argument or parameter
B) Keyword arguments or parameters
C) Keyword Default parameters
D) Mixed parameters
Flow of control during expansion.
• The default flow of control during macro expansion is sequential.
• Thus in the absence of preprocessor statements, the model statements of a
macro are visited sequentially from the statement following the macro
prototype statement to the statement preceding the MEND statement.
• A preprocessor statement can alter the flow of control during expansion
such that some model statements are either
1. never visited during expansion (conditional expansion), or
2. repeatedly visited during expansion (expansion time loops).
The flow of control during macro expansion is implemented using a macro
expansion counter(MEC).
Macro expansion Algorithm
1. Initialize the MEC(Macro Expansion Counter).
2. Check the statement which is pointed by MEC is not MEND.

If(statement = model statement) then


Expand the statement
Increment MEC
Else
MEC = new value specified in statement
Exit from ME
Macro Expansion
• Tasks Involved,
• Identify Macro Call
• Identify the value of Formal Parameter.
• Maintain Expansion time variables.
• Organize Expansion time control flow.
• Determining the values of sequencing symbols.
• Expansion of model statement.
Nested Macro Calls
• Nested Macro -> Macro may call another macro.
• Macro containing the nested call, is called Outer macro
• Called Macro, is called Inner macro.
• It follows LIFO principle.
Recursive macro call
• Recursive macro -> Macro expands itself.
• Note:- Macro definition does not contain any statement to stop any of
the inner expansion and return to complete the outer ones.
• Problem? Infinite loop.
• Solution:- Conditional assembly.
• E.g. MACRO
• MAC_Y &par=A
• MOVER REG1, &par
• MAC_Y &par=A
MEND
Advanced Macro Facilities
1. Facilities for change the flow of control
2. Expansion time variables
3. Attributes of parameters
4. Advanced directives
The Removal Directive
The IRP Directive
The REPEAT Directive
1. Facilities for change the flow of control
Two features are provided for this purpose
1) Expansion time Sequencing symbol(SS):- Is defined by putting it in
the label field of a stmt in the macro body.
2) Expansion time statements or syntax( AIF, AGO, ANOP)
• AIF-> AIF(<expression>) <Sequencing Symbol (SS)>
• Expression includes relational expression (LT,NE etc.). If condition is
true then control is transferred to SS.
• AGO-> AGO <SS> -> Unconditional transfer
• ANOP -> <SS> ANOP -> used to define SS
Cont…
2. Expansion time variables
EV(Expansion Variables):- are variables which can only be used during
the expansion of macro call. It may be of two types:
1. local EV(LCL) used only in one particular macro.
• Syntax: LCL <EV specification>,….
2. Global EV(GBL) works like global variables.
• Syntax: GBL <EV specification>,…..
Values of EV can be changed using SET(preprocessor stmt).
• Syntax: <EV specification> SET <SET expression>
3. Attributes of parameters
• An attribute is written using the syntax:
<attribute name> <formal parameter specification>
T-type, L-length, S- size e.g.
Design of Macro Preprocessor

You might also like