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

Sp_Unit 2_ Macro Peocessor_MRD

This document covers the fundamentals of macro processors, including macro definition, expansion, and the design of macro processors. It explains the differences between macros and subroutines, types of macro parameters, and the process of macro expansion with examples. Additionally, it discusses nested macro calls and advanced macro facilities, emphasizing the importance of macros in program generation and code efficiency.

Uploaded by

patelsakib9317
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)
13 views

Sp_Unit 2_ Macro Peocessor_MRD

This document covers the fundamentals of macro processors, including macro definition, expansion, and the design of macro processors. It explains the differences between macros and subroutines, types of macro parameters, and the process of macro expansion with examples. Additionally, it discusses nested macro calls and advanced macro facilities, emphasizing the importance of macros in program generation and code efficiency.

Uploaded by

patelsakib9317
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
You are on page 1/ 36

System Programming :

Unit 2: Macro processor


Prof. Madhavi Dachawar
Comp Engg. Dept
Vishwakarma University
Points to be covered in this Unit

❑ Macro definition and call


❑ macro expansion
❑ Nested Macro calls
❑ Design of macro processor
❑ Design issues of macro processors. (Data structures for design of
MDT, MNT)
❑ Design of two-pass macro processors
❑ Advanced Macro Facilities.
Macro: Definition
1. Macros provide the program generation facilities
2. A macro instruction is a notational convenience for the programmer.
3. It allows the programmer to write a shorthand version of a program (module programming)
4. The macro processor replaces each macro invocation with the corresponding sequence of
statements (expanding)
1. A macro is a unit of specification for program generation through expansion.
2. Macro consists of a name, a set of formal parameters and a body of code.
3. “The use of the macro name with a set of actual parameters is replaced by some code
generated from its body, this is called macro expansion.”
4. Varies languages provide built-in facilities for writing macros
5. Ex: C, C++, PL/I
❑ Two kind of expansion
1. Lexical expansion:
➢ Lexical expansion implies the replacement of a character string by
another character string during program generation.
➢ Lexical expansion is typically employed to replace occurrences of
formal parameters by corresponding actual parameters.
2. Semantic Expansion:
➢ Semantic expansion implies the generation of instructions tailored
to the requirements of a specific usage
➢ Example: generation of type-specific instruction for manipulation
of byte and word operands.
Macro Processor

➢ Recognize macro definitions


➢ Save the macro definition
➢ Recognize macro calls
➢ Expand macro calls
Macro vs. Subroutine
❑ A macro is a fragment of code that is given a name. Whenever that name is used in the
code, it is replaced by the contents of the macro.
❑ This replacement happens during the preprocessing or assembly stage, before the actual
compilation or execution of the code.
❑ When a macro is invoked, the preprocessor or assembler simply replaces the macro call
with the macro's definition.
❑ This is called macro expansion.

❑ A subroutine is a block of code that performs a specific task. It is called or invoked from
different parts of a program, and control is passed to it. After the subroutine completes, the
control returns to the point from where it was called.
❑ When a subroutine is invoked, the program jumps to the code for the subroutine, executes
it, and then returns to the point where it was called.
Macro definition and call
Macro definition:
❑ A macro definition is enclosed between a macro header statement and a macro end
statement.
Macro definitions are typically located at the start of the program.
The macro definition consists of
❑ A macro prototype statement
❑ One or more model statement
❑ Macro preprocessor statement
A macro prototype statement
❑ The macro prototype statement declares the name of a macro and the names and kinds
of its parameters.
❑ <macro name> [<formal parameter spec>, …]
❑ Where the name appears in the mnemonic field of assembly statement and <formal
parameter spec> is of the form &<parameter name>[<parameter kind>]
Model statement
❑ A model statement is a statement from which an assembly language statement may be
generated during macro expansion.
Macro preprocessor statement
❑ A preprocessor statement is used to perform auxiliary functions during macro
expansion.
❑ Example: the following sequence of instruction is used to increment the value in a
memory word by a constant:

❑ Move the value from the memory word into a machine register.
❑ Increment the value in the machine register.
❑ Move the new value into the memory word.

❑ Using lexical expansion, the macro call INCR A, B, AREG can lead to the
generation of a MOVE-ADD-MOVE instruction sequence to increment A by the
value B using AREG.
 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.
❑ <macro name> [<actual parameter spec>,…]
❑ Where an actual parameter is typically an operand specification in an
assembly language statement.
❑ Example- INCR A, B, AREG
Macro Expansion

❑ A macro call leads to macro expansion, during macro expansion, the macro
call statement is replaced by a sequence of assembly statements.
❑ „+‟ is used to differentiate between the original statement of the program and
the macro statement.
❑ Example:
+ MOVER AREG , A
+ ADD AREG , B
+ MOVEM AREG, A
Flowof control duringexpansion

The flow of control during expansion


❑ The default flow of control during macro expansion is sequential.
❑ It starts with the statement following the macro prototype statement and ends with
the statement preceding the MEND statement.
❑ A preprocessor statement can alter the flow of control during expansion such that
some model statements are never visited during expansion is called conditional
expansion.
❑ The same statement is repeatedly visited during expansion is called loop
expansion.
Algorithm –Macro Expansion

❑ Macro expansion is implemented using a macro expansion counter (MEC).


❑ Algorithm: (Outline of macro expansion)
MEC=statement number of first statement following the prototype statement;
While the statement pointed out by MEC is not a MEND statement
◼ (a) if a model statement then
◼ (i) Expand the statement
◼ (ii) MEC=MEC+1;
◼ (b) Else (i.e. a preprocessor statement)
◼ (i) MEC= new value specified in the statement;
Exit from macro expansion.
Lexical Substitution
The model statement consists of 3 types of strings
❑ An ordinary string, which stands for itself.
❑ The name of a formal parameter that is preceded by the character „&‟.
❑ The name of the preprocessor variable, which is also preceded by the character „&‟.
❑ During lexical expansion, strings of type 1 are retained without substitution.
❑ String types 2 and 3 are replaced by the value of a formal parameter or processor
variable, where the value of the formal parameter is the name of the corresponding
actual parameter
❑ The rules for determining the value of a formal parameter depend on the kind of
parameter.
❑ There are two kind of parameters positional and keyword parameters
Typesof Parameters

❑ Positional parameters
❑ Keyword parameters
❑ Default specification of parameter
❑ Macro with mixed parameter lists
❑ Other uses of parameters
Formal parameter
 A formal parameter of the positional kind is written as &<parameter name> ,
e.g. &SAMPLE
 where SAMPLE is the name of a parameter.
 other words, the syntax is simply an <ordinary string>.
 The value of a positional formal parameter XYZ is determined by the rule of
positional association, which is stated as follows —
1. Find the ordinal position of XYZ in the list of formal parameters in the macro
prototype statement,
2. Find the actual parameter specification occupying the same ordinal position
in the list of actual parameters in the macro call statement.
Let this be the ordinary string ABC. The value of the formal parameter XYZ is
then ABC.
INCR A, B, AREG
 on macro INCR Here A, B and AREG are ordinary strings representing 3 actual
parameters.
 Following the rule of positional association, values of the formal parameters are
formal parameter Values
MEM_VAL A
INCR_VAL B
REG AREG
Lexical expansion of the model statements now leads to the code
+ MOVER AREG A
+ ADD AREG, B
+ MOVEM AREG, A
Keywords parameter

 For keyword parameters,<parameter name> is an ordinary string


and<parameter kind> is the string ‘=’ in the syntax rule
 The <actual parameter specification>is written as<formal parameter name>
=<ordinary string> .
 The value of a formal parameter XYZ is determined by the rule of keyword
association as follows —
 two assembly-like code instructions involving the operation "INCR_M."

 In the first line:


 INCR_M: This appears to be the operation name.
 MEM_VAL=A: This suggests that the memory value is set to A.
 INCR_VAL=B: This implies that the increment value is set to B.
 REG=AREG: This indicates that the register is assigned to AREG.

 In the second line:


 The same operation INCR_M is used.
 INCR_VAL=B: The increment value remains as B.
 REG=AREG: The register is still assigned to AREG.
 MEM_VAL=A: The memory value is set to A.

 This could represent a specific pattern of operation where certain values are incremented or
manipulated within a loop or repeated sequence, with a slight variation in the order of
operations or assignments between the two instances.
definition of a macro named `INCR_M` in assembly language.
 Breakdown of the Macro Definition:
MACRO
 INCR_M &MEM_VAL=, &INCR_VAL=, &REG= (This line begins the definition of a macro named
`INCR_M`.)
 `&MEM_VAL=`, `&INCR_VAL=`, and `&REG=` (are the macro parameters. The `&` symbol indicates
that these are parameters that will be replaced by actual values when the macro is invoked.)
Macro Body:
 MOVER &REG, &MEM_VAL (The `MOVER` instruction moves the value of `&MEM_VAL` into the register
`&REG`.)
 ADD &REG, &INCR_VAL ( The `ADD` instruction adds the value of `&INCR_VAL` to the contents of
`&REG`.)
 MOVEM &REG, &MEM_VAL ( The `MOVEM` instruction moves the contents of `&REG` back into
`&MEM_VAL`.)
 MEND ( This marks the end of the macro definition.)
Explanation:
 When the `INCR_M` macro is invoked, the user will provide specific values for
`&MEM_VAL`, `&INCR_VAL`, and `&REG`.
 The macro performs three operations: it moves the value from memory to a
register, adds an increment value to the register, and then stores the updated
register value back into memory.
 This macro simplifies repetitive code sequences where a memory value needs
to be incremented, and the result stored back in memory. By defining this as
a macro, the user can easily repeat this sequence with different parameters
without writing the instructions manually each time.
 Find the actual parameter specification which “has the form XYZ=<ordinary
string>
 Let <ordinary string>in the specification be ABC. Then the value of parameter
XYZ is ABC.
 Note that the ordinal position of the specification XYZ=ABC in the list of
actual parameters is immaterial.
 This is the primary advantage of keyword parameters.
 It is useful in situations where positional parameters are difficult to use, e.g.
in long lists of parameters, due to the need to maintain correspondence
between the specifications of formal and actual parameters.
Default specification parameters

 A default is a standard assumption in the absence of an explicit specification


by the programmer.
 Default specification of parameters is useful in situations where a parameter
has the same value in most calls.
 When the desired value is different from the default value, the desired value
can be specified explicitly in a macro call.
 This specification overrides the default value of the parameter for the
duration of the call.
 Default specification of keyword parameters can be incorporated by
extending the syntax for formal parameter specification, as ; follows
 &<parameter name> [< parameter kind >[< default value >]
Default specification of parameters
 Example:
Call the macro
INCR_D MEM_VAL=A, INCR_VAL=B
INCR_D INCR_VAL=B, MEM_VAL=A
INCR_D INCR_VAL=B, MEM_VAL=A, REG=BREG
MARCO DIFINITION
MACRO
INCR_D &MEM_VAL=,&INCR_VAL=,&REG=AREG
MOVER ADD &REG, &MEM_VAL
MOVEM &REG, &INC_VAL
MEND &REG, &MEM_VAL
three invocations of a macro or a function called `INCR_D` with different arguments.
Breakdown:
First Invocation:
 `INCR_D MEM_VAL=A, INCR_VAL=B`
 Here, `MEM_VAL` is set to `A`, and `INCR_VAL` is set to `B`.
 The `REG` parameter is not explicitly set, so it might be using a default value if defined in the macro.
Second Invocation:
 `INCR_D INCR_VAL=B, MEM_VAL=A`
 The order of the parameters is swapped, but the values remain the same: `INCR_VAL` is `B`, and `MEM_VAL` is `A`.
 Again, `REG` is not set explicitly.
Third Invocation:
 `INCR_D INCR_VAL=B, MEM_VAL=A, REG=BREG`
 This time, along with `INCR_VAL` being `B` and `MEM_VAL` being `A`, the `REG` parameter is set to `BREG`.
Explanation:
These invocations are examples of calling the `INCR_D` macro (or function) with different sets of parameters.
The values `A`, `B`, and `BREG` are passed to the parameters `MEM_VAL`, `INCR_VAL`, and `REG`, respectively.
The macro likely performs a series of operations similar to those in the previous macros, where memory values are
incremented, stored in a register, and possibly moved back to memory.
The difference in the invocations shows flexibility in how the macro can be used, allowing for different configurations
depending on the needs of the program.
Macros with mixed parameter lists

 A macro may be defined to use both positional and keyword parameters.


 In such a case, all positional parameters must precede all keyword
parameters.
 For example, in the macro call SUMUP A, B, G=20, H=X A, B are positional
parameters while G, H are keyword parameters.
 Correspondence between actual and formal parameters is established by
applying the rules governing positional and keyword parameters separately.
Other uses of parameters

 The model statements have used macro parameters in operand fields.


 However, use of parameters need not be restricted to these fields.
 The parameters can also appear in the label and opcode fields of model
statements.
 The command or macro `calc &x &y &op=mult , &Lab=` can be interpreted as:

 Perform a multiplication operation (`mult`) between the values stored in `&x` and `&y`.**
 The `&Lab` parameter is empty, suggesting that no specific label or additional instruction is
needed for this operation.**
NESTED MACRO CALLS

 A model statement in one macro may constitute a call on another macro.


 Such calls are known as nested macro calls.
 We refer to the macro containing the nested call as the outer macro and the
called macro as the inner macro.
 Expansion of nested macro calls follows the last-in-first-out (LIFO) rule.
 Thus, in a structure of nested macro _calls, expansion of the latest macro
call (i.e. the innermost macro call in the structure) is completed first.
NESTED MACRO CALLS

 Macro COMPU contains a nested call on macro INCR_D While expanding the
call
 COMPU x y
 recognized to be a call on macro INCR_D.
 Expansion of this macro is now performed.
 This leads to generation of statements marked[2] [3] and [4]
 The third model statement of COMPU is + now expanded. Thus the expanded
code for the call on COMPU is —

You might also like