0% found this document useful (0 votes)
209 views64 pages

Unit - 4 Pushdown Automata

This document discusses macros and macro processors. It covers: 1) The definition of macros, how they allow code to be reused by defining blocks that can be called multiple times via substitution. 2) Macro expansion, the process by which the macro name is replaced by the defined block of code during assembly. 3) Types of formal parameters in macros, including positional parameters where order cannot change between definition and call.

Uploaded by

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

Unit - 4 Pushdown Automata

This document discusses macros and macro processors. It covers: 1) The definition of macros, how they allow code to be reused by defining blocks that can be called multiple times via substitution. 2) Macro expansion, the process by which the macro name is replaced by the defined block of code during assembly. 3) Types of formal parameters in macros, including positional parameters where order cannot change between definition and call.

Uploaded by

Prabhat kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

Unit – 4

Unit – 4
Pushdown
Macro & Macro processors
Automata
Prof. Dixita Kagathara
[email protected]

Unit
System
–– 44Programming
System
Unit :: Macro
Macro &
& Macro
Programming (2150708)
Macro processors
(2150708)
processors 11 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Topics to be covered
• Introduction
• Macro definition and call
• Macro expansion
• Advanced macro facilities
• Nested macro calls
• Design of a macro pre-processor
• Functions of a macro processor
• Design of a macro assembler
• Basic Tasks of a macro processor
• Design issues of macro processors
• Features of macro processors
• Macro processor design options

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Introduction
• A macro is a named block of assembly language statements.
• Macro allows a sequence of source language code to be defined.
• Once defined, it can be called one or more times.
• Example: Macro in ‘C’ Language.
#define PI 3.1415926535
• The advantages of macro are:
1. Simplify and reduce the amount of repetitive coding.
2. Reduce the possibility of errors caused by repetitive coding.
3. Make an assembly program more readable.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 33 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Macro definition and call
Macro

Macro name Set of formal parameters Body of codes  

• Features of macro are:


1. A Macro prototype statement: Specifies the name of the macro and
name and type of formal parameters.
2. A Model statements: Specifies the statements in the body of the
macro from which assembly language statements are to be generated
during expansion.
3. A Macro preprocessor statement: Specifies the statement used for
performing auxiliary function during macro expansion.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 44 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Macro definition and call
Write a macro to increment the value stored in memory word.
Macro definition
Prototype statement syntax:
<name_of_macro> [<formal parameter spec> [,...]]

&<name_of_parameter> [<parameter_type>]
Prototype
MACRO statement
Macro
header INCR &MEM_VAL, &INC_VAL, &REG
MOVER &REG &MEM_VAL
ADD &REG &INC_VAL body of the macro
Macro MOVEM &REG &MEM_VAL (model statements)
end
MEND
Macro call
A macro can be defined between
Macro call Syntax: a macro header and a macro end statement.
<name_of_macro> [<actual_parameter_spec> [,…]]

INCR A, B, AREG
Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 55 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Macro expansion
• During macro expansion, the macro name statement in the program is replaced
by the sequence of assembly statements.
START 100     START 100  
A DS 1   A DS 1
B DS 1   B DS 1
INCR A, B, AREG   + MOVER AREG A
PRINT A  
+ ADD AREG B
STOP    
END     + MOVEM AREG A
Assembly Program   PRINT A  
  STOP    
  END    
MACRO Extended Assembly Program
INCR &MEM_VAL, &INC_VAL, &REG
MOVER &REG &MEM_VAL
ADD &REG &INC_VAL
MOVEM &REG &MEM_VAL
Macro

MEND    

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 66 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Algorithm: Macro expansion
1. MEC: statement number of the first model statement following
the prototype statement in the definition of the called macro;
2. While the statement pointed to by MEC is not a MEND statement
a) If a model statement then
I. Expand the statement through lexical substitution.
II. MEC= MEC+1;
b) Else
MEC= Value specified in the preprocessor statement.
3. Exit from macro expansion.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 77 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Types of formal parameters
• Two types of formal parameters are:
1. Positional parameters: Order can not be changed in macro call.
Example:
Prototype statement: INCR &MEM_VAL, &INC_VAL, &REG
Macro call: INCR A, B, AREG
2. Keyword parameters: Order can be changed in macro call.
Example:
Prototype statement: INCR &MEM_VAL=, &INC_VAL=, &REG=
Macro call: INCR INCR_VAL=B, REG=AREG, MEM_VAL=A

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 88 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Positional parameter
• Syntax:
&<parameter name>
• The value of a positional formal parameter XYZ is determined by
the rule of positional association 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 that occupies the
same ordinal position in the list of actual parameters in the
macro call statement.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 99 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Positional parameter
MACRO
INCR &MEM_VAL, &INC_VAL, &REG Positional parameters
MOVER &REG &MEM_VAL
ADD &REG &INC_VAL
MOVEM &REG &MEM_VAL   START 100  
Macro

MEND       A DS 1
  B DS 1
+ MOVER AREG A
START 100  
+ ADD AREG B
A DS 1
B DS 1 + MOVEM AREG A
INCR A, B, AREG     PRINT A  
PRINT A     STOP    
STOP       END    
END     Extended Assembly Program
Assembly Program

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 10
10 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Keyword parameter
• Syntax:
&<parameter name>=
• The <actual parameter specification> is written as <formal
parameter name> = <ordinary string>.
• The value of a formal parameter is determined by the rule of
keyword association as follows:
• Find the actual parameter specification which has the form
XYZ= <ordinary string>.
• If the <ordinary string> in the specification is some string ABC,
the value of formal parameter XYZ would be ABC.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 11
11 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Keyword parameters
MACRO
INCR_M &MEM_VAL=, &INCR_VAL=, &REG= Keyword
MOVER &REG &MEM_VAL
parameters
ADD &REG &INC_VAL
MOVEM &REG &MEM_VAL
MEND

During a macro call, a keyword parameter is specified by its name.


INCR_M MEM_VAL=A, INCR_VAL=B, REG=AREG

OR
The order
INCR_M INCR_VAL=B, REG=AREG, MEM_VAL=A can be
changed.
Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 12
12 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Macros with mixed parameter lists
• A macro definition may use both positional and keyword
parameters.
• In such a case, all positional parameters must precede all keyword
parameters in a macro call.
• Example:
SUMUP A, B, G=20, H=X
Positional Keyword
parameters parameters

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 13
13 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Notions used in macro expansion
• There are two notions used in implementing macro expansion:
1. Flow of control during expansion: it determines the order in
which model statements in a macro’s definition would be
visited for expansion of a macro call.
2. Lexical substitution: lexical substitution is used to generate an
assembly statement from a model statement.

Model statement uses following three types of strings:


1. An ordinary string remains in its original form during lexical
substitution.
2. Formal parameter preceded by ‘&’ is replaced by its value.
3. Preprocessor variable preceded by ‘&’ is replaced by its value.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 14
14 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Flow of control during expansion
• Default flow is sequential.
• A preprocessor statement can alter the flow of control during
expansion such as:
1. Conditional expansion: Some model statements are not
visited at all.
2. Expansion time loop: Some statements are repeatedly
executed.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 15
15 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Advanced macro facilities

Unit
System
–– 44Programming
System
Unit :: Macro
Macro &
& Macro
Programming (2150708)
Macro processors
(2150708)
processors 16
16 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Advanced macro facilities
Advanced macro facilities

Altering flow
Expansion
of control Attributes
time
during of parameters 
variables
expansion

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 17
17 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Altering flow of control during expansion
Expansion time statements (condition statements) are:
1. AIF
2. AGO
3. ANOP

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 18
18 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Expansion time statement: AIF
• Syntax:
AIF (<expression>) <sequencing symbol>
• Where <expression> is a relational expression consists of:
1. Ordinary strings.
2. Formal parameters with their attributes.
3. Expansion time variables.
• If the relational expression evaluates to true, expansion time
control is transferred to the statement containing <sequencing
symbol> in its label field.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 19
19 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: AIF
MACRO   Attribute of formal
  DCL_CONST &A parameter
  AIF (L’ &A EQ 1) .NEXT
  --  
.NEXT --   If length of A=1
  --   Syntax
  MEND   then move to
NEXT

<attribute name> ’ <formal parameter spec>

Attributes have the names


type (T), length (L), & size (S).

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 20
20 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Expansion time statement: AGO
• Syntax:
AGO <sequencing symbol>
• It unconditionally transfers expansion time control to the
statement containing <sequencing symbol> in its label field.
• Example:
  MACRO
  EVAL &X, &Y, &Z
Conditional   AIF (&Y EQ &X) .ONLY
expansion MOVER AREG, &X
  SUB AREG, &Y
ADD AREG, &Z
Unconditional
  AGO .OVER
expansion
 .ONLY MOVER AREG, &Z
.OVER MEND

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 21
21 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Expansion time statement: ANOP
• Syntax:
<sequencing symbol> ANOP
• It has the simply effect of defining the sequencing symbol.
• No operation is carried out by an ANOP instruction. Instead, if a branch
is taken to the ANOP instruction, the assembler processes the next
sequential instruction.
  MACRO
  CREATE_CONST &X, &Y
  AIF (T’ &X EQ B) .BYTE
&Y DW 25
  AGO .OVER
.BYTE ANOP
&Y DB 25
 .OVER MEND

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 22
22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Advanced macro facilities
Advanced macro facilities

Altering flow
Expansion
of control Attributes
time
during of parameters 
variables
expansion

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 23
23 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Expansion time variables
• Expansion time variables (EV's) are variables which can only be
used during the expansion of macro calls.
• Types of expansion time variables:
1. Local expansion time variables
Example: LCL &A
local variable A is created.
2. Global expansion time variables
Example: GBL &A
Global variable A is created.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 24
24 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Expansion time variable
• Local expansion time variables can be used only within one macro
and it does not retain its value across the macro call.
• Syantax: LCL <EV specification> [,<EV specification> .. ]
• Global expansion time variables can be used in every macro
definition that has a declaration for it and it retains its value across
the macro call.
• Syntax: GBL <EV specification> [,<EV specification> .. ]
• <EV specification> has the syntax &<EV name>, where <EV name> is
an ordinary string.
• Values of EV's can be manipulated through the preprocessor
statement SET.
• Syntax:< EV specification > SET <SET-expression>

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 25
25 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Expansion time variable

 MACRO
  CONSTANTS Local variable
Assigns value   LCL &A created
‘1’ to A &A SET 1
  DB &A
&A SET &A+1
  DB &A
Assigns value   MEND  
‘2’ to A

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 26
26 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Expansion time loops
• It is often necessary to generate many similar statements during
the expansion of a macro.
• This can be achieved by writing similar model statements in the
macro.
• Expansion time loops can be written using expansion time
variables (EV’s) and expansion time control transfer statements
AIF and AGO.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 27
27 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Expansion time loops
 MACRO Local variable
Set value of M   CLEAR &X, &N M created
to ‘0’
  LCL &M
MOVEM AREG,
MOVEM B
AREG, B+2
B+1
&M SET 0 (&X=B,&M=0)
(&X=B,&M=1)
(&X=B,&M=2)
Set value of   MOVER AREG, =’0’ content of
Move content
Move of AREG
AREG to
to B+2
B
B+1
AREG to ‘0’
.MORE MOVEM AREG, &X+&M
Set value of M &M SET &M + 1
‘2’
‘1’
to ‘3’   AIF (&M NE &N) .MORE
  MEND  
 Compare M and N
M N (3
(2
(1 3)?
Call statement: CLEAR B, 3
&X=B, &N=3

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 28
28 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Expansion time loops (REPT statement)
• Syntax:
REPT <expression>
• Example: Declare 10 constants with the values 1, 2, …., 10 using
REPT statement.   MACRO
Set value of M   CONST10
to ‘1’   LCL &M
&M SET 1
REPT 10 Repeat Block
10 times
DC ‘&M’
&M SET &M+1
  ENDM
  MEND  

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 29
29 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Expansion time loops (IRP statement)
• Syntax:
IRP <formal parameter> , <argument list>
• The formal parameter mentioned CONSTS 4, 10
in the statement takes successive M=4, N=10
values from the argument list.
MACRO
• For each value the statements
CONSTS &M, &N, &Z
between the IRP and ENDM
statements are expanded once. IRP &Z, &M, 7, &N

• A macro call CONSTS 4, 10 leads to DC ‘&Z’


declaration of 3 constants with the ENDM
values 4, 7, and 10. MEND

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 30
30 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Nested macro calls
• A model statement in a macro may constitute a call on another
macro. Such a call is known as a nested macro call.
• The macro that contains the nested call is known as outer macro
and the macro that is called in the nested call is known inner
macro.
• The macro preprocessor performs expansion of nested macro calls
using the last-in first-out (LIFO) rule.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 31
31 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Nested macro calls
MACRO MACRO
COMPUTE &FIRST, &SECOND INCR_D &MEM_VAL=, &INCR_VAL=,
MOVEM BREG, TMP &REG=BREG
INCR_D &FIRST, & SECOND, &REG=BREG MOVER &REG, &MEM_VAL
MOVER BREG, TMP ADD &REG, &INC_VAL
MEND  
MOVEM &REG, &MEM_VAL
MEND

+ MOVEM BREG TMP [1] + MOVER BREG, X [2]


COMPUTE X , Y + INCR_D X, Y + ADD BREG, Y [3]
+ MOVER BREG,TMP [5] + MOVEM BREG, X [4]

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 33
33 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design of a macro preprocessor

Unit
System
–– 44Programming
System
Unit :: Macro
Macro &
& Macro
Programming (2150708)
Macro processors
(2150708)
processors 34
34 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design of a macro preprocessor

…… …..
Macro
…… Assembler …..
preprocessor …..
……

Program with Program without


Target
macro definitions macro definitions
program
& calls & calls

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 35
35 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design of a macro preprocessor
• Language translators such as assemblers and compilers cannot
directly generate the target code from the programs containing
definitions and calls for macros.
• Therefore, most language processing activities by assemblers and
compilers preprocess these programs through macro pre-
processors.
• A macro preprocessor essentially accepts an assembly program
with macro definitions and calls as its input.
• It converts into an equivalent expanded assembly program with
no macro definitions and calls.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 36
36 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Tasks involved in macro expansion
1. Recognizing macro calls
2. Determining the values of formal parameters
3. Maintaining the values of expansion time variables
4. Organizing expansion time control flow
5. Determine the values of sequencing symbols
6. Perform expansion of a model statement

• Macro Name Table (MNT) is used to store names of all macros defined in a
program.
• During processing program statements, a match is done to compare strings in
the mnemonic field with entries in the MNT.
• A successful match in the MNT indicates that the statement is a macro call.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 37
37 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Tasks involved in macro expansion
1. Recognizing macro calls (Table: MNT)
2. Determining the values of formal parameters
3. Maintaining the values of expansion time variables
4. Organizing expansion time control flow
5. Determine the values of sequencing symbols
6. Perform expansion of a model statement

• Preprocessor needs to know the names of formal parameters and default values of
keyword parameters.
• It obtains this information from macro definition and builds a table called Parameter
Default Table (PDT) to store the pairs of the form (<formal parameter name>,
<default value>) for each macro defined in the program.
• Actual Parameter Table (APT) is used to holds the values of formal parameters during
the expansion of a macro call in the form (<formal parameter name>, <value>).

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 38
38 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Tasks involved in macro expansion
1. Recognizing macro calls (Table: MNT)
2. Determining the values of formal parameters (Table: PDT, APT)
3. Maintaining the values of expansion time variables
4. Organizing expansion time control flow
5. Determine the values of sequencing symbols
6. Perform expansion of a model statement

• Expansion time Variable Table (EVT) is used to maintains information about


expansion variables in the form (<EV name>, <value>).
• The expansion time variable is needed when the preprocessor statement or
the model statement under expansion refers to an Expansion time variable.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 39
39 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Tasks involved in macro expansion
1. Recognizing macro calls (Table: MNT)
2. Determining the values of formal parameters (Table: PDT, APT)
3. Maintaining the values of expansion time variables (Table: EVT)
4. Organizing expansion time control flow
5. Determine the values of sequencing symbols
6. Perform expansion of a model statement

• Macro Definition Table (MDT) is used to store the body of a macro.


• The flow of control determines when a model statement from the MDT is to
be visited for expansion during macro expansion.
• MEC (Macro Expansion Counter) is defined and initialized to the first
statement of the macro body in the MDT.
• MDT is updated after an expansion of a model statement by a macro
preprocessor.
Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 40
40 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Tasks involved in macro expansion
1. Recognizing macro calls (Table: MNT)
2. Determining the values of formal parameters (Table: PDT, APT)
3. Maintaining the values of expansion time variables (Table: EVT)
4. Organizing expansion time control flow (Table: MDT)
5. Determine the values of sequencing symbols
6. Perform expansion of a model statement

• Sequencing Symbols Table (SST) is used to maintains information about


sequencing symbols in pairs of the form (<sequencing symbol name>, <MDT
entry #>).
• Where <MDT entry #> denotes the index of the MDT entry containing the
model statement with the sequencing symbol.
• Entries are made on encountering a statement with the sequencing symbol in
their label field or on reading a reference prior to its definition.
Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 41
41 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Tasks involved in macro expansion
1. Recognizing macro calls (Table: MNT)
2. Determining the values of formal parameters (Table: PDT, APT)
3. Maintaining the values of expansion time variables (Table: EVT)
4. Organizing expansion time control flow (Table: MDT)
5. Determine the values of sequencing symbols (Table: SST)
6. Perform expansion of a model statement

• The expansion task has the following steps:


1. MEC (Macro Expansion Counter) points to the MDT (Macro Definition
Table) entry that contains the model statement.
2. APT(Actual Parameter Table) and EVT (Expansion Time variable Table)
provides the values of the formal parameters and EVs, respectively.
3. SST (Sequencing Symbol Table) enables identifying the model statement
and defining sequencing symbol.
Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 42
42 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Data structures of the macro preprocessor
Table Fields in each entry
Macro name table (MNT) Macro name, Number of positional
parameters (#PP), Number of keyword
parameters (#KP), "Number of expansion
time variables (#EV), MDT pointer (MDTP),
KPDTAB pointer (KPDTP), SSTAB pointer
(SSTP)

Parameter Name Table (PNTAB) Parameter name


EV Name Table (EVNTAB) Expansion time variable name
SS Name Table (SSNTAB) Sequencing Symbol name
Keyword Parameter Default Table (KPDTAB) Parameter name, default value
Macro Definition Table (MDT) Label, Opcode, Operands
Expansion time Variable Table (EVTAB) Value
Actual Parameter Table (APTAB) Value
SS Table (SSTAB) MDT entry #

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 43
43 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Data structures (Macro preprocessor)
Macro call: CLEARMEM AREA, 10 Positional
parameters X
MACRO Keyword N
parameter
CLEARMEM &X, &N, &REG=AREG REG
LCL &M PNTAB
&M SET 0
MOVER &REG, =‘0’ REG AREG
.MORE MOVEM &REG, &X+&M KPDTAB
&M SET &M+1
AREA
AIF (&M NE N) .MORE
10
MEND
AREG
APTAB
M 0 MORE
EVNTAB EVTAB SSNTAB

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 44
44 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Data structures (Macro preprocessor)

MACRO
CLEARMEM &X, &N, &REG=AREG
LCL &M
&M SET 0 25 (E, 1) SET 0
MOVER &REG, =‘0’
.MORE MOVEM &REG, &X+&M 26 MOVER (P,3), =‘0’
27 MOVEM (P,3), (P,1)+(E,1)
&M SET &M+1
28 (E, 1) SET (E,1) + 1
AIF (&M NE N) .MORE 29 AIF ((E,1) NE (P,2)) (S,1)
MEND 30 MEND
MDT

Name #PP #KP #EV MDTP KPDTP SSTP 5 28


CLEARMEM 2 1 1 25 10 5 SSTAB
MNT
Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 45
45 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Basic functions of macro processors
• Tow basic functions of macro processors (two pass macro
processor) are:
1. Handling macro definition
2. Handling macro expansion

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 46
46 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Handling macro definition
• On encountering the MACRO directive in the source assembly
program, the assembler is being changed from the regular mode to
a special macro definition mode, wherein it does the following
activities:
1. Analyzes the available space in the MDT.
2. Reads continually the statements and writes them to the MDT
until the MEND directive is found.
3. When a MEND directive is found in the source file, the
assembler reverts to the normal mode.
4. If the MEND directive is missing, the assembler will stay in the
macro definition mode and continue to save program
statements in the MDT until an obvious error occurs.
Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 47
47 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Algorithm: Processing Macro Definition 1. SSNTAB_ptr := 1; PNTAB_ptr := 1;
2. Process the macro prototype statement and form the MNT entry
(a) name :=macro name; #PP = 0; #KP = 0; PointerEnter positional
initialization.
(b) For each positional parameter parameter
Enter in PNTAB
parameter name
i. Enter parameter name in PNTAB[PNTAB_ptr]. and increment
and default value in
pointer.
KPDTAB.
ii. PNTAB_ptr := PNTAB_ptr + 1;
Enter parameter name
iii. #PP := #PP + 1;
in PNTAB and
(c) KPDTP := KPDTAB_ptr; increment pointers.
Set KPDTAB.
(d) For each keyword parameter
i. Entry parameter name and default value (if any), in KPDTAB [KPDTAB_ptr].
ii. Enter parameter name in PNTAB [PNTAB_ptr].
iii. KPDTAB_ptr := KPDTAB_ptr +1;
iv. PNTAB_ptr := PNTAB_ptr + 1;
v. #KP := #KP + 1;
(e) MDTP := MDT_ptr: Set MDTP, EV, SSTP.
(f) #EV := 0;
(g) SSTP := SSTAB_ptr;

System
System Programming
Programming (2150708)
(2150708) 48
48 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
3. While not a MEND statement Enter local variable to
Algorithm: Processing Macro Definition (a) If an LCL statement then
EVNTAB and increment the
pointer.
i. Enter expansion time variable name in EVNTAB.
ii. #EV := #EV + 1; If label field having
sequencing symbol
(b) If a model statement then
then process it and
i. If label field contains a sequencing symbol then set pointers
If symbol is present in SSNTAB then accordingly.
Generate
q := entry number in SSNTAB; specification for
else parameters and
expansion variables.
Enter symbol in SSNTAB[SSNTAB_ptr]. Then generate IC
q := SSNTAB_ptr; and store in MDT.
SSNTAB_ptr := SSNTAB_ptr + 1;
SSTAB[SSTP + q – 1] := MDT_ptr;
ii. For a parameter, expansion variable, and sequencing symbol
generate the specification (P, #n), (E, #n) and (S, #n) respectively.
iii. Record the IC in MDT [MDT_ptr];
iv. MDT_ptr := MDT_ptr + 1;
System
System Programming
Programming (2150708)
(2150708) 49 Darshan
49 Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
c) If a preprocessor statement then
Algorithm: Processing Macro Definition
i. If a SET statement
Generate specification for
expansion time variable.
Search each expansion time variable name used in the statement in
EVNTAB and generate the specification (E, #n).
ii. If an AIF Or AGO statement then
If sequencing symbol used in the statement is present in SSNTAB
then
q := entry number in SSNTAB;
else
Enter symbol in SSNTAB [SSNTAB_ptr].
q := SSNTAB_ptr;
SSNTAB_ptr := SSNTAB_ptr + 1;
Replace the symbol by (S, SSTP + q -1). Process sequencing
iii. Record the IC in MDT [MDT_ptr]. symbol, set pointers
iv. MDT_ptr := MDT_ptr + 1; accordingly and record IC
in MDT.
4. (MEND statement)
a) Record the intermediate code of the statement in MDT[MDT_ptr];
b) MDT_ptr = MDT_ptr + 1
Set pointers.
c) If SSNTAB_ptr = 1 (i.e. SSNTAB is empty) then SSTP := 0;
d) If #KP = 0 then KPDTP = 0
System
System Programming
Programming (2150708)
(2150708) 50 Darshan
50 Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Handling macro expansion
• The tasks performed in the macro expansion mode are as follows:
1. Reading a source statement from the MDT entry.
2. Writing the statement on the new source listing.
3. Repeating step 1 and 2 until the end of the macro is located in
the MDT.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 51
51 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Algorithm: Macro expansion
1. Perform initialization for the expansion of a macro
(a) MEC := MDTP field of the MNT entry; Set pointers and
(b) Create EVTAB with #EV entries and set EVTAB_ptr. process parameters.
(c) Create APTAB with #PP+#KP entries and set APTAB_ptr.
(d) Copy keyword parameter defaults from the entries KPDTAB [KPDTP] …
KPDTAB[KPDTP+#KP -1] into APTAB[#PP+1]… APTAB[#PP+#KP].
(e) Process Positional parameters in the actual parameter list copy them into
APTAB [1]… APTAB[#PP].
(f) For keyword parameters in the actual parameter list Search the keyword name
in parameter name field of KPDTAB [KPDTP]… KPDTAB [KPDTP+#KP-1].
2. While statement pointed by MEC is not MEND statement
a. If a model statement then
i. Replace operands of the form (P,#n) and (E, #m) by values in APTAB [n]
and EVTAB [m] respectively. Replace parameter and
ii. Output the generated statement. expansion time variable from
iii. MEC :=MEC + 1; table and increment MEC.
Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 52
52 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Algorithm: Macro expansion
b. If a SET statement with the specification (E, #m) in the label field then
I. Evaluate the expression in the operand field and set an appropriate
Value in EVTAB [m]. Set appropriate value in EVTAB
and increment MEC.
II. MEC := MEC + 1;
c. If an AGO statement with (S,#s) in operand field then Set value of
MEC := SSTAB[SSTP+s -1]; MEC.

d. If an AIF statement with (S,#s) in operand field then Set value of


If condition in the AIF statement Is true then MEC.
MEC := SSTAB [SSTP+s - 1];
3. Exit From macro expansion.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 53
53 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Macro assembler

Unit
System
–– 44Programming
System
Unit :: Macro
Macro &
& Macro
Programming (2150708)
Macro processors
(2150708)
processors 54
54 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design of macro assembler
• A macro processor is functionally independent of the assembler.
• The output of the macro processor will be a part of the input into
the assembler.
• A macro processor scans and processes the statements.
• Often, the use of a separate macro processor for handling macro
instructions leads to less efficient program translation because many
functions are duplicated by the assembler and macro processor.
• To overcome efficiency issues and avoid duplicate work by the
assembler, the macro processor is generally implemented within
pass 1 of the assembler.
• The integration of macro processor and assembler is often referred
to as macro assembler.

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 55
55 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Advantages & Disadvantages of macro assembler
Advantages
• It ensures that many functions need not be implemented twice. (Example:
Analysis of a source statement to detect macro calls requires us to process the
mnemonics field. A similar function is required in the first pass of the
assembler.
• Results in fewer overheads because many functions are combined and do not
need to create intermediate (temporary) files.
• It offers more flexibility in programming and allows the use of all assembler
features in combination with macros.
Disadvantages
• The resulting pass by combining macro processing and pass 1 of the assembler
may be too large and sometimes suffer from core memory problems.
• Sometimes increase the complexity of program translation, which may not be
desired.
Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 56
56 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Pass structure of macro
assembler

Unit
System
–– 44Programming
System
Unit :: Macro
Macro &
& Macro
Programming (2150708)
Macro processors
(2150708)
processors 57
57 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Pass structure of macro assembler
Pass I Pass I
• Macro definition processing • Macro definition processing
• Entering of names and types of • Macro expansion
symbols in the SYMTAB • Memory allocation, LC
Pass II processing and SYMTAB
Can be merged
• Macro expansion construction
• Memory allocation and LC • Processing of literals
processing • Intermediate code generation
• Processing of literals Pass II
• Intermediate code generation • Target code generation
Pass III
• Target code generation

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 58
58 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design issues of macro
processor

Unit
System
–– 44Programming
System
Unit :: Macro
Macro &
& Macro
Programming (2150708)
Macro processors
(2150708)
processors 59
59 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design issues of macro processor
• Flexible data structures and databases
• Attributes of macro arguments
• Default arguments
• Numeric values of arguments
• Comments in macros

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 60
60 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design features of macro
processor

Unit
System
–– 44Programming
System
Unit :: Macro
Macro &
& Macro
Programming (2150708)
Macro processors
(2150708)
processors 61
61 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design features of macro processor
• Associating macro parameters with their arguments
• Delimiting macro parameters
• Directives related to arguments
• Automatic label generation
• Machine-independent features

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 62
62 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Macro processor design options

Unit
System
–– 44Programming
System
Unit :: Macro
Macro &
& Macro
Programming (2150708)
Macro processors
(2150708)
processors 63
63 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Macro processor design options
• Recursive macro Expansion
• General-purpose macro processors
• Macro processing within language translators
• Line-by-Line macro processor

Unit –– 44Programming
System
Unit
System :: Macro
Macro &
& Macro
Programming processors
(2150708)
Macro processors
(2150708) 64
64 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
End of Unit - 4

Unit
System
–– 44Programming
System
Unit :: Macro
Macro &
& Macro
Programming (2150708)
Macro processors
(2150708)
processors 65
65 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology

You might also like