MACRO DEFINITION: It Is Enclosed Between a Macro Header Statement & Macro End Statement

MACRO DEFINITION: It Is Enclosed Between a Macro Header Statement & Macro End Statement

MACRO: -

Def: Macro is a single line abbreviation for groups of instruction. For every occurrence of macro in assembly program, macro processor substitutes the entire block. Macro differ from a subroutine in the sense that macro name leads to expansion while routine name leads to its execution. Thus program size doesn’t change by using Macro while using function we can reduce the size.

MACRO DEFINITION: It is enclosed between a macro header statement & macro end statement. It consists of the Following:

A macro prototype statement.

One or more model statement.

Macro preprocessor statement.

Prototype statement declares the name of macro & its parameter.

Model statements are statements from which the assembly language is generated.

Preprocessor statements are used to perform auxiliary function

e.g.

MACRO

Name of macro {list of arguments} ------Prototype

______|

______|

______| {Sequence of instructions}

______|

______|

MEND

Where list of arguments is of the form

&Parameter1, &Parameter2, …………….

MACRO & MEND are pseudo operation codes of assembly language, and are macro header & macro end statements.

MACRO CALL: -

A macro is called by writing the macro name in the mnemonic field of an assembly statement. The syntax is

Macro_namelist of actual parameters.

Example. .

MACRO

INCR &a, &b, &c

MOV AX, &a

ADD AX, &b

MOV &C, AX

MEND

MACRO EXPANTION: -

A macro call leads to macro expansion. During macro expansion, macro call statement is replaced by a sequence of assembly statements. Two things affect macro expansion.

Flow of control during expansion. By default, it is sequential. A preprocessor statement can alter the flow of control in such a way that some model statements are either never visited or visited or repeatedly visited. The flow of control is implemented by using a counter called macro expansion counter (MEC).

Lexical substitution, which means replacement of character string by a corresponding character string. There are three types of string in model statements: -

  1. Ordinary string.
  2. The name of formal parameter preceded by &
  3. The name of preprocessor variable which is also preceded by &.

During lexical expansion, string of type 1 is retained without substitution. String of type 2 & 3 are replaced by the values of formal parameters or preprocessor variable. The rule of replacing formal parameters depends on the kind of parameter. There are two kind of parameters: -

  1. Positional.
  2. Keyword.

POSITIONAL PARAMETER: -In this case arguments are replaced on the basis of their position of the argument list i.e. first actual argument will replace first dummy argument, second will replace the second dummy argument & so on.

Example: -

MACRO

ABC &A, &B, &C

------

------

------

MEND

When we call ABC as follow

ABC X, Y, Z

Then X will replace &A, Y will replace &B and Z will replace &C

KEYWORD ARGUMENT: They allow references to the dummy argument by name as well as by the position. There is small modification in syntax as follow:

Macro_name &arg1 = , &arg2 =, &arg3 = , -----

And syntax of call is

Macro_name formal parameter name=actual parameter name, formal parameter name = actual parameter name, ……..

Example: -

MACRO

ABC &A=, &B=, &C=

------

------

------

MEND

Here in the Call

ABC &A=Z, &B=X, &C=Y

Z will replace &A, X will replace &B and Y will replace &C

Default values can be provided as follows

MACRO

ABC &A=, &B=, &C=Y

------

------

------

MEND

Then in call ABC &A=Z, &B=X the default value for &C is used.

And in call ABC &A=Z, &B=Y, &C=X the default value Y is overdriven by X.

Macro parameters can also appear in label field. For Example:

MACRO

ABC &A, &B, &C, &L

&L MOV Ax, &A

ADD Ax, &B

MOV &C, Ax

MEND

Then it can be called as

ABC X, Y, Z, LOOP1

NESTED MACRO

A model statement in one macro may constitute a call on another macro. Such calls are called nested macro calls. Expansion of nested macro calls follows a LIFO rule. Thus in a structure of nested macro calls, expansion of latest macro call is completed first. For Example:

MACRO

A1 &arg

MOV AX, 05

MOV &arg, AX

MEND

MACRO

B1 &arg1, &arg2, &arg3

A1 &arg1

A2 &arg2

A1 &arg 3

MEND

Then call

B1 X, Y, Z leads to

A1 X ------MOV AX, 05

MOV X, AX

A1 Y------MOV AX, 05

MOV Y, AX

A1 Z ----- MOV AX, 05

MOV Z, AX

CONDITIONAL MACRO EXPANTION: This feature of macro facility allows

To alter the flow of control.

Expansion time variable.

Attributes of parameter.

ALTERING THE FLOW OF CONTROL: This can be done by using

Sequence symbol

And expansion time statement AIF, AGO

SEQUENCE SYMBOL: It is a variable name preceded by a dot (.). It is also called macro label & do not appear in the output of the macro processor. It is used in AIF or AGO statements designate the destination. AIF is a condition branch statement. The AIF statement has syntax.

AIF (expression) (sequencing symbol).

Where expression can have values EQ for Equal, LT for less than, GT for greater than, and NE for not equal to. If expression evaluates to true then control is transferred at a statement containing sequencing symbol. E.g.

MACRO

ABC &count, &A, &B, &C

ADD &A, 5

AIF(&count EQ 1) .end

ADD &B, 7

AIF(&count EQ 2) .end

ADD &C, 8

.end MEND

Then the calls ABC 1, X, Y, Z

ABC 2, X, Y, Z

ABC 3, X, Y, Z

Will leads to different expansions.

AGO is used for unconditional branch. Its syntax is

AGO (Sequencing symbol)

EXPANTION TIME VARIBLE: (EV)

These are the variables that can only be used during the expansion of macro calls. EV can be local or global in scope. The general syntax for declaration is

LCL &(EV name)

GBL &(EV name)

Value of EV can be manipulated through preprocessor SET statement as follows

&(Ev name) SET <value>

Example: LCL &A

&A SET 1

EXPANSION TIME LOOP: -We can create a loop using AIF statement expansion variable as follows

MACRO

Clear &X, &N

LCL &A

&A SET 0

MOV AX, 0

.L MOV &X + &A, AX

&A SET &A+1

AIF (&A NE &N) .L

MEND

MACRO PROCESSOR DESIGN: -

Before designing a microprocessor, we must enlist the basic task that is to be performed by it. There are four basic tasks that every macro processor must do. Those are: -

  1. Recognize macro definition
  2. Save the definition
  3. Recognize calls
  4. Expand calls& substitute arguments

We break the process in 2 passes. In pass I, macro definition is searched and entries are made in appropriate tables. In pass2, macro calls are recognized & expansion is done. Before discussing further, let us understand the limitation of a macro processor.

A macro definition refers to nothing outside themselves.

Macro calls refer only to macro definition.

Macro calls substitutes the text and not values of parameter. Example: - if in assembly program two lines are situated as follows.

Y EQU 10

======

======

INCR Y (MACRO CALL)

Then macro processor substitutes Y & not 10 for the argument in macro definition. This is because macro never processes the EQU statement so it does not know the value of Y.

DATA BASE REQUIRED: -

In pass 1 following data bases are used: -

Macro definition table (MDT) used to store the body of macro definition.

Macro name table (MNT) used to store the name of definition and entry, which points to corresponding entry in MDT.

Macro definition table counter is used to indicate the next available entry (MDTC).

Macro name table counter (MNTC) to indicate the next available entry (MNTC).

Argument list array (ALA) used to substitute index markers for dummy arguments.

PASS 2: It will use the following data base: -

MDT

MNT

MDTP

ALA

DATA BASE FORMAT: -

MDT

-- / ------
22 / INCR &A, &B
23 / MOV AX, #1
24 / MOV AX, #2
25 / MOV #1, AX
26 / MEND
-- / ------

MNT

Index / Name / Line # in MDT
--- / --- / ----
8 / INCR / 22
-- / -- / ---

ALA: -

During pass 1, dummy arguments in the macro definition are replaced with positional indicators. The position is marked by # which is reserved symbol for processor. During pass 2, actual arguments are entered in ALA according to call. If the call is with positional arguments,

Example INCR X, Y the ALA will looks like

ALA index argument

1 / X
2 / Y

And if call is with keyword argument like

INCR & A=Y, &B=X then entries in ALA table are

1 / Y
2 / X

Pass one :

Pass 1 tests each I/P line. If it is macro pseudo op code, the entire macro definition is stored in the next available location in macro definition table and macro name is entered in MNT along with pointer to the first location of the MDT entry. When END pseudocode is encountered, control is transferred to pass 2.

FLOWCHART PASS1

PASS 2:

Single Pass Macroprocessor

Given a restriction that all macros must be defined before calling them, we can join 2 passes into a single pass. Here we need another indicator MDI which can have values ‘ON’ or ‘OFF’. MDI ON means lines are read from macro definition table and OFF means lines are read from regular input stream.

FLOWCHART:

Assembler &macro processor

MICROPROCESSOR can be added to an assembler in two ways:

  • As a preprocessor, which make complete pass over the text before pass 1 of assembler.
  • With in pass 1 of assembler

If we create macro processor within assembler some functions can be combined. Database can also be prepared jointly. e.g. MNT can be joined with assembler’s MOT or POT. Similarly the read function that expand macro calls and receive the source I/P will be same.

FLOWCHART

Advantages: -

Many functions do not have to implement twice.

Fewer overheads during processing.

More flexibility for programmer as he can use all the features of assembler as well as macros.

Disadvantages: -.

Program may be too large to sit into memory ofM/C.

Complexity of prog.