Macro
Macro
Macro
1
System Programming Mrs. Sunita M Dol, CSE Dept.
where <macro name> appears in the mnemonic field of assembly Statement and
<formal parameter specification> is of the form
&<parameter name> [<parameter kind>]
2. Model statements.
A model statement is a statement from which assembly language statements may be
generated during macro expansion.
Macro call
A macro is called by writing the macro name in the mnemonic field of an assembly
statement. The macro call has syntax
<macro name> [<actual parameter specification>[,….]]
where actual parameter resembles an operand specification in assembly statement.
Example 1: The definition of macro INCR is given below
MACRO
INCR &MEM_VAL, &INCR_VAL, ®
MOVER ®, &MEM_VAL
ADD ®, &INCR-VAL
MOVEM ®, &MEM_VAL
MEND
The prototype statement indicates that three parameters called MEM_VAL, INCR_VAL,
and REG are parameters. Since parameter kind isn’t specified for any of the
parameters, they are all of the default kind ‘positional parameters’. Statements with the
operation codes MOVER, ADD, MOVEM are model statements. No preprocessor
statements are used in this macro.
2
System Programming Mrs. Sunita M Dol, CSE Dept.
1. Lexical expansion.
Lexical expansion implies replacement of a character string by another character string
during program generation. It replaces the occurrences of formal parameter by
corresponding actual parameters.
Example 2: Consider the following macro definition
MACRO
INCR &MEM_VAL, &INCR_VAL, ®
MOVER ®, &MEM_VAL
ADD ®, &INCR-VAL
MOVEM ®, &MEM_VAL
MEND
3
System Programming Mrs. Sunita M Dol, CSE Dept.
4
System Programming Mrs. Sunita M Dol, CSE Dept.
5
System Programming Mrs. Sunita M Dol, CSE Dept.
Lexical Substitution
A model statement consists of 3 types of strings:
1. An ordinary string, which stands for itself.
2. The name of formal parameter which is preceded by the character ‘&’
3. The name of a preprocessor variable, which is also preceded by the character ‘&’.
During lexical expansion, strings of type 1 are retained without substitution. Strings of
type 2 and type 3 are replaced by the values of the formal parameters or preprocessor
variables. The rules for determining the value of a formal parameter depend on the kind
of parameter.
Types of parameters
1. Positional parameters.
2. Keyword parameters.
3. Default parameters.
4. Macros with mixed parameter list.
5. Other uses of parameters.
1. Positional parameters
A positional formal parameter is written as &<parameter name>, e.g. &SAMPLE
where SAMPLE is name of parameter. A <actual parameter specification> is simply an
<ordinary string>.
The value of positional parameter XYZ is determined by the rule of positional
association as follows:
6
System Programming Mrs. Sunita M Dol, CSE Dept.
(i) Find the ordinal position of XYZ in the list of formal parameters in the macro
prototype statement.
(ii) 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. Then the value of formal parameter XYZ is ABC.
7
System Programming Mrs. Sunita M Dol, CSE Dept.
2. Keyword parameters
A keyword formal parameter is written as ‘&<parameter
&<parameter name>=’.
name>= The <actual
parameter specification> is written as <formal parameter name>=<ordinary
ordinary string>
string
The value of a formal parameter XYZ is determined by the rule of keyword association
as follows:
(i) Find the actual parameter specification which has the form XYZ=<ordinary
string>.
(ii) ordinary string> in the specification be the string ABC. Then the value of
Let <ordinary
formal parameter XYZ is ABC.
The
he ordinal position of the specification XYZ=ABC in the list of actual parameters is
immaterial. This is very useful in situations where long lists of parameters have to be
used.
Example 5: Consider
onsider an example
MACRO
INCR &MEM_VAL=, &INCR_VAL=, ®=
MOVER ®, &MEM_VAL
ADD ®, &INCR-VAL
MOVEM ®, &MEM_VAL
MEND
8
System Programming Mrs. Sunita M Dol, CSE Dept.
+ MOVER AREG, A
+ ADD AREG, B
+ MOVEM AREG, A
9
System Programming Mrs. Sunita M Dol, CSE Dept.
The first call overrides to us a default specification for the parameter BREG.
10
System Programming Mrs. Sunita M Dol, CSE Dept.
+ MOVER AREG, A
+ ADD AREG, B
+ MOVEM AREG, A
11
System Programming Mrs. Sunita M Dol, CSE Dept.
+ MOVER BREG, A
+ ADD BREG, B
+ MOVEM BREG, A
12
System Programming Mrs. Sunita M Dol, CSE Dept.
MEND
13
System Programming Mrs. Sunita M Dol, CSE Dept.
call (I.e. the innermost macro call in the structure) is completed first.
Example 9: Consider an example
MACRO
COMPUTE &FRIST, &SECOND
MOVEM BREG, TMP
INCR &FIRST, &SECOND, AREG
MOVER BREG, TMP
MEND
14
System Programming Mrs. Sunita M Dol, CSE Dept.
15
System Programming Mrs. Sunita M Dol, CSE Dept.
Example 10:
MACRO
EVAL &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &X
SUB AREG, &Y
ADD AREG, &Z
AGO .OVER
.ONLY MOVER AREG, &Z
.OVER MEND
16
System Programming Mrs. Sunita M Dol, CSE Dept.
17
System Programming Mrs. Sunita M Dol, CSE Dept.
byte constant ‘1’. The second SET statement assigns the value ‘2’ to A and the second
DB statement declares a constant ‘2’.
18
System Programming Mrs. Sunita M Dol, CSE Dept.
Example 13:
MACRO
EVAL &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &X
SUB AREG, &Y
ADD AREG, &Z
AGO .OVER
.ONLY MOVER AREG, &Z
.OVER MEND
Since the value of a formal parameter is simply the corresponding actual parameter, the
AIF statement effectively compares names of the first two actual parameters. If the
names are same, expansion time control is transferred to the model statement MOVER
AREG, &Z. If not, the MOVE-SUB-ADD sequence is generated and expansion time
control is transferred to the statement .OVER MEND which terminates the expansion.
19
System Programming Mrs. Sunita M Dol, CSE Dept.
When called as CLEAR B, the statement puts the value 0 in AREG, while the three
MOVEM statements store this value in 3 consecutive bytes with the address B, B+1,
B+2.
The same can be achieved by writing an expansion time loop which visits the model
statement, or set of model statements, repeatedly during macro expansion. Expansion
time loops can be written using EV’s and expansion time control transfer statements AIF
and AGO.
Example 15:
MACRO
CLEAR &X, &N
LCL &M
&M SET 0
MOVER AREG, =’0’
.MORE MOVEM AREG, &X+&M
&M SET &M+1
AIF (&M NE N) .MORE
MEND
20
System Programming Mrs. Sunita M Dol, CSE Dept.
REPT statement
Syntax: REPT<expression>
<expression> should evaluate to a numerical value during macro expansion. The
statements between REPT and ENDM statement would be processed for expansion
<expression> number of times.
Example 16: Following example illustrates the use of this facility to declare 10
constants with the value 1, 2, …, 10
MACRO
CONST10
LCL &M
&M SET 1
REPT 10
DC ‘&M’
&M SETA &M+1
ENDM
MEND
IRP statement
Syntax: IRP <formal parameter>, <argument list>
The formal parameter mentioned in the statements takes successive values from the
argument list. For each value, the statements between the IRP and ENDM statements
are expanded once.
Example 17:
MACRO
CONSTS &M, &N, &Z
IRP &Z, &M, 7, &N
DC ‘&Z’
ENDM
MEND
21
System Programming Mrs. Sunita M Dol, CSE Dept.
A macro call CONSTS 4, 10 leads to declaration of 3 constants with the values 4, 7, 10.
This macro creates a constant ‘25’ with the name given by the 2nd parameter type of the
constant matches the type of the first parameter.
Example 19:
MACRO
CLEAR &X, &N
LCL &M
&M SET 0
MOVER AREG, =’0’
.MORE MOVEM AREG, &X+&M
&M SET &M+1
AIF (&M NE N) .MORE
MEND
22
System Programming Mrs. Sunita M Dol, CSE Dept.
Example 20:
MACRO
EVAL &X, &Y, &Z
AIF (&Y EQ &X) .ONLY
MOVER AREG, &X
SUB AREG, &Y
ADD AREG, &Z
AGO .OVER
.ONLY MOVER AREG, &Z
.OVER MEND
23
System Programming Mrs. Sunita M Dol, CSE Dept.
The following 4 step procedure is followed to arrive at a design specification for each
task
1. Identify the information necessary to perform a task.
2. Design a suitable data structure to record the information.
3. Determine the processing necessary to obtain and maintain the information.
24
System Programming Mrs. Sunita M Dol, CSE Dept.
If a macro call statement does not specify a value for some parameter then its default
value would be copied from PDT to APT.
25
System Programming Mrs. Sunita M Dol, CSE Dept.
So the information (<formal parameter name>,<value>) in APT has been split into two
tables:
PNTAB contains formal parameter names
APTAB contains formal parameter values
Similarly analysis leads to splitting of EVT into EVNTAB and EVTAN and SST into
SSNTAB and SSTAB. PDT is replaced by a keyword parameter default table
(KPDTAB).
• Macro name,
Macro Name Table
• Number of positional parameters (#PP),
(MNT)
• Number of keyword parameters (#KP),
26
System Programming Mrs. Sunita M Dol, CSE Dept.
27
System Programming Mrs. Sunita M Dol, CSE Dept.
Example 21:
MACRO
CLEAR_MEM &X, &N, ®=AREG
LCL &M
&M SET 0
MOVER ®, =’0’
.MORE MOVEM ®, &X+&M
&M SET &M+1
AIF (&M NE N) .MORE
MEND
Consider the macro call
CLEAR_MEM AREA, 10, AREG
PNTAB EVNTAB
X M
N
REG
SSNTAB
MORE
KPDTAB
Pointer(KPDTP) Formal parameter Default value
10 REG AREG
28
System Programming Mrs. Sunita M Dol, CSE Dept.
MDT
Pointer (MDTP) label opcode Operand
25 LCL (E, 1)
26 (E, 1) SET 0
MNT
Name #PP #KP #EV MDTP KPDTP SSTP
CLEAR_MEM 2 1 1 25 10 5
APTAB
AREA
10
AREG
EVTAB
29
System Programming Mrs. Sunita M Dol, CSE Dept.
c) KPDTP := KPDTAB_PTR;
e) MDTP := MDT_PTR
f) #EV := 0
g) SSTP := SSTAB_PTR
30
System Programming Mrs. Sunita M Dol, CSE Dept.
4. MEND statement
If SSNTAB+PTR=1 then
SSTP=0
Else
SSTAB_PTR = SSTAB_PTR + SSNTAB_PTR-1
If #KP: = 0 then KPCTP: = 0
31
System Programming Mrs. Sunita M Dol, CSE Dept.
32
System Programming Mrs. Sunita M Dol, CSE Dept.
33
System Programming Mrs. Sunita M Dol, CSE Dept.
34
System Programming Mrs. Sunita M Dol, CSE Dept.
35
System Programming Mrs. Sunita M Dol, CSE Dept.
36
System Programming Mrs. Sunita M Dol, CSE Dept.
37
System Programming Mrs. Sunita M Dol, CSE Dept.
38
System Programming Mrs. Sunita M Dol, CSE Dept.
39
System Programming Mrs. Sunita M Dol, CSE Dept.
b) If a SET statement with the specification (E, #m) in the label field then
i. Evaluate the expression in the operand and set an appropriate
value in EVTAB[m].
ii. MEC:= MEC+1
40
System Programming Mrs. Sunita M Dol, CSE Dept.
41
System Programming Mrs. Sunita M Dol, CSE Dept.
42
System Programming Mrs. Sunita M Dol, CSE Dept.
43
System Programming Mrs. Sunita M Dol, CSE Dept.
44
System Programming Mrs. Sunita M Dol, CSE Dept.
45
System Programming Mrs. Sunita M Dol, CSE Dept.
46
System Programming Mrs. Sunita M Dol, CSE Dept.
47
System Programming Mrs. Sunita M Dol, CSE Dept.
Pass I
1. Macro definition processing.
2. SYMTAB construction.
Pass II
1. Macro expansion.
2. Memory allocation and LC processing.
3. Processing of literals.
4. Intermediate code generation.
Pass III
1. Target code generation.
The pass structure can be simplified if attributes of actual parameters are not to be
supported. The macro preprocessor would then be a single pass program. Integrating
pass I of the assembler with preprocessor would give us the following two pass
structure
Pass I
1. Macro definition processing.
2. Macro expansion.
3. Memory allocation, LC processing and SYMTAB construction.
4. Processing of literals.
5. Intermediate code generation.
Pass II
1. Target code generation.
48