Sp_Unit 2_ Macro Peocessor_MRD
Sp_Unit 2_ Macro Peocessor_MRD
❑ 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, ®
MOVER ®, &MEM_VAL
ADD ®, &INCR_VAL
MOVEM ®, &MEM_VAL
MEND
Macro call
❑ 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
❑ 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
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=, ®= (This line begins the definition of a macro named
`INCR_M`.)
`&MEM_VAL=`, `&INCR_VAL=`, and `®=` (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 ®, &MEM_VAL (The `MOVER` instruction moves the value of `&MEM_VAL` into the register
`®`.)
ADD ®, &INCR_VAL ( The `ADD` instruction adds the value of `&INCR_VAL` to the contents of
`®`.)
MOVEM ®, &MEM_VAL ( The `MOVEM` instruction moves the contents of `®` 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 `®`.
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
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
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 —