0% found this document useful (0 votes)
44 views61 pages

Macroprocessor New

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)
44 views61 pages

Macroprocessor New

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/ 61

CHAPTER 3

MACROS AND
MACRO PROCESSORS
Overview

Ch 3 – Macros and Macro Processors 3/20/2024


Macros
 Macro instructions or Macros are single line
abbreviations for groups of instructions.

 Single instruction is used to represent a block of


code.

 Expanding a Macro: For every occurrence of this


one line macro instruction, the macro processing
assembler will substitute the entire block.
Ch 3 – Macros and Macro Processors 3/20/2024
Macro Instructions
 Macro is an abbreviation for a series of
operations.

Start of definition
 Example:
Macro name

A 1,DATA  MACRO
A 2,DATA INCR INCR
A 3,DATA A 1,DATA
Repeated  Sequence to be
 A 2,DATA
code A 3,DATA
abbreviated
A 1,DATA INCR
A 2,DATA  MEND
A 3,DATA

End of definition
Ch 3 – Macros and Macro Processors 3/20/2024
Macros in C
#include <stdio.h> #include <stdio.h>
#define Square(x) ((x)*(x)) //Macro definition not included

int main(void) int main(void)


{ {
int a; int a;
printf(“enter a no : ”); printf(“enter a no : ”);
scanf(“%d”,&a); scanf(“%d”,&a);
Square(a); ((a)*(a));
} }
Ch 3 – Macros and Macro Processors 3/20/2024
Macros in Assembly code (8086)
macro print msg start:
mov dx, offset msg
mov ah, 09h .
int 21h .
endm
mov dx, offset msg
data segment
Msg1 db “hello world$” mov ah, 09h
data ends int 21h
code segment .
.
.
.
print msg code ends
. end start
.
end start
Ch 3 – Macros and Macro Processors 3/20/2024
A macro-processor
 Essentially involve the substitution of group of
characters or lines for another
 The design of macro-processor is machine
independent.

Ch 3 – Macros and Macro Processors 3/20/2024


Basic Macro Processor Functions
 Macro definition
 Macro Invocation
 Macro Expansion

Ch 3 – Macros and Macro Processors 3/20/2024


MACRO DEFINITION
 Two new assembler directives are used in macro definition:
 MACRO: identifies beginning of MACRO definition
 MEND: identifies end of MACRO definition
 label op operands
Name MACRO
<macro name> [<formal parameters specification>]

<model statements>

MEND
 Name: Name given for the macro appeareas in mnemonic field of aasembly statement
 Formal Parameters: The entries in the operands field identify the parameters of the macro
definition.
 Model Statements: the statements that will generate as the expansion of macro
 Prototype: The macro name and parameters define a pattern or prototype

Ch 3 – Macros and Macro Processors 3/20/2024


Macro Invocation
 A macro invocation (macro call) gives the name of
the macro instruction being invoked and the
argument in expanding macro.
 Arguments from the macro invocation are
substituted for parameters in the macro prototype.

Ch 3 – Macros and Macro Processors 3/20/2024


Parameter specification
 Parameters can be of two types:
 Positional parameters
 Formal paramaeter : &<parameter name>
 Actual parameter is usually an ordinary string

INCR A,B, AREG

Ch 3 – Macros and Macro Processors 3/20/2024


Parameter specification
 Parameters can be of two types:
 Keyword parameters
 Formal paramaeter : &<parameter name>[<parameter kind> [<default value>]]
 Actual parameter : <formal parameter> = <ordinarystring>

Ch 3 – Macros and Macro Processors 3/20/2024


Keyword parameter

Ch 3 – Macros and Macro Processors 3/20/2024


Attributes of formal parametes
 Attribute of formal parater represents information
about the value of formal parameter i.e. about
corresponding actual parameters
 Syntax : <attribute name>’ <formal para specification>
T : type
 L : length

 S: size

 Eg. L’&A  Length of parameter A

Ch 3 – Macros and Macro Processors 3/20/2024


Macro Invocation
 A macro invocation (macro call) gives the name of the
macro instruction being invoked and the argument in
expanding macro.
 Arguments from the macro invocation are substituted
for parameters in the macro prototype.
 Macro Invocation Vs Subroutine call
 Statement of macro are expanded each time the macro is
invoked.
 Statements of subroutine appear only once, regardless of
how many times the subroutine is called.
 Macro invocation is more efficient than subroutine call,
however the code size is larger.

Ch 3 – Macros and Macro Processors 3/20/2024


Features of Macro Facility
 Macro Instruction Arguments

 Conditional Macro Expansion

 Macro calls within Macros

 Macro Instruction defining Macros

Ch 3 – Macros and Macro Processors 3/20/2024


Macro Instruction Arguments
 Macro calls replaces the call by a block of code.

 No flexibility to modify code that replaces the call.

 Extension for providing arguments or parameters in macro call.

 Macro instruction argument (dummy arguments) are used in definition.

 It is specified in the macro name line and distinguished by ‘&’

 Arguments that are not specified, are presumed blank by macro


processor.

Ch 3 – Macros and Macro Processors 3/20/2024


Macro Instruction Arguments
Original Source Original Source
Expanded Source Expanded Source
(single argument) (Multiple argument)
MACRO MACRO
INCR &ARG INCR &ARG1,&ARG2
A 1, &ARG A 1, &ARG1
A 2, &ARG A 1,DATA1 A 2, &ARG2
A 3, &ARG MEND A 1,DATA1
A 2,DATA1
MEND A 2,DATA2
. A 3,DATA1
.
. INCR DATA1,DATA2
INCR DATA1 A 1,DATA2
. A 1,DATA2 A 2,DATA1
.
.
A 2,DATA2 INCR DATA2,DATA1
INCR DATA2 A 3,DATA2

Ch 3 – Macros and Macro Processors 3/20/2024


Conditional Macro Expansion
 AIF (arithmetic IF) and AGO(Arithmetic go) permit
conditional reordering of the sequence of macro
expansion.
 AIF
 Conditional branch
 Performs arithmetic test and branches if condition is true
 AGO
 Unconditional branch (similar to ‘go to’ statement)
 Machine instructions that appear in the expansion of a
macro call can be selected based on condition.
 Labels inside a Macro start with a period (.) eg: .FINISH

Ch 3 – Macros and Macro Processors 3/20/2024


Conditional Macro Expansion
 AIF
 Conditional branch
 Performs arithmetic test and branches if condition is true
 AIF (<expression>) <sequencing symbol>
 expression: The condition under which the branch should
occur. It can be any arithmetic condition like 'EQUAL',
'NOT EQUAL', 'GREATER THAN', etc.

Ch 3 – Macros and Macro Processors 3/20/2024


Conditional Macro Expansion
 AGO
 Unconditional branch (similar to ‘go to’ statement)
 Machine instructions that appear in the expansion
of a macro call can be selected based on condition.
 Labels inside a Macro start with a period (.) eg:
.FINISH

Ch 3 – Macros and Macro Processors 3/20/2024


Conditional expansion using AIF and AGO

Ch 3 – Macros and Macro Processors 3/20/2024


Conditional Macro Expansion
Original Source Source with Macro
MACRO
&ARG0 VARY &COUNT, &ARG1, &ARG2,&ARG3
&ARG0 A 1, &ARG1
Loop1 A 1,DATA1 AIF (&COUNT EQ 1).FINISH
A 2,DATA2 A 2,&ARG2
A 3,DATA3 AIF (&COUNT EQ 2).FINISH Expanded Source
. A 3,&ARG3
.
. .FINISH MEND
Loop2 A 1,DATA3 .
.
Loop1 A 1,DATA1
.
A 2,DATA2 A 2,DATA2
Loop1 VARY 3,DATA1,DATA2,DATA3
.
. . A. 3,DATA3
. . .
. .

Loop3 A 1,DATA1 Loop2 VARY 2,DATA3,DATA2 Loop2 A 1,DATA3


.
.
.
A. 2,DATA2
.
Loop3 VARY 1,DATA1 .
Loop3 A 1,DATA1

Ch 3 – Macros and Macro Processors 3/20/2024


Macro calls within Macros
 Also known as nested macro calls.

 A macro can be called within another macro.

 A macro can call itself (using AIF or AGO) so long


as it doesn’t go into an infinite loop.

 Macro calls within macros can have several levels

Ch 3 – Macros and Macro Processors 3/20/2024


Macro calls within Macros
Source Expanded Source Expanded Source
MACRO (Level 1) (Level 2)
ADD1 &ARG
L 1,&ARG
A 1,=F’1’ Expansion of Expansion of
ST 1,&ARG ADDS ADD1
MEND
MACRO
ADDS &ARG1,&ARG2,&ARG3
L 1,DATA1
ADD1 &ARG1
ADD1 DATA1 A 1,=F’1’
ADD1 &ARG2
ST 1,DATA1
ADD1 &ARG3
L 1,DATA2
MEND
ADD1 DATA2 A 1,=F’1’
ST 1,DATA2
ADDS DATA1,DATA2,DATA3
L 1,DATA3
ADD1 DATA3 A 1,=F’1’
ST 1,DATA3

Ch 3 – Macros and Macro Processors 3/20/2024


Macro Instruction defining Macros
 Macros can be defined within a macro.

 Inner macro definition is not defined until after the outer


macro has been called.

 Group of macros can be defined for subroutine calls


with some standardized calling sequence.

 Individual macros have names of the associated


subroutines (as given by the argument &SUB).

Ch 3 – Macros and Macro Processors 3/20/2024


Macro Instruction defining Macros
MACRO
DEFINE &SUB Macro name: DEFINE
MACRO
&SUB &Y Dummy macro name
Definition CNOP 0,4 Align boundary
of macro Definition
BAL 1,*+8 Set reg 1 to parameter list pointer
DEFINE of macro
DC A(&Y) Parameter list pointer
&SUB
L 15,=V(&SUB) Address of subroutine
BALR1 14,15 Transfer control to subroutine
MEND
MEND
DEFINE COS
COS AR
BAL 1,*+8
DC A(AR) Address of AR
L 15,=V(COS) V denotes Address of external symbol
BALR 14,15
Ch 3 – Macros and Macro Processors 3/20/2024
2 pass Macro Processor
 Assumptions
 Functionally different from assembler
 No nested macro calls or macro within macro definitions

 Assembler scans and processes lines of text.


 A line can refer to another line by its address or name
 Address or name must be available to assembler

 Macro definitions do not refer to anything outside


themselves.
 Macro calls refer only to macro definitions
Ch 3 – Macros and Macro Processors 3/20/2024
Implementation of Macro Processor
There are four basic task that a macro-processor must perform
 Recognize macro definition: A macro instruction processor
must recognize macro definition identified by MACRO &
MEND pseudo-operations.
 Save the Definition: The processor must store the macro the
time of expansion of calls. definitions which will be needed
at the time of expansion od macro calls.
 Recognize the Macro call: The processor must recognize
macro calls that appear as operation mnemonics.
 Expand calls & substitute arguments: The macro call is
replaced by the macro definition. The dummy arguments are
replaced by the actual data.

Ch 3 – Macros and Macro Processors 3/20/2024


2 pass Macro Processor
 Pass 1: handles macro definitions
 Input macro source deck
 Output macro source deck copy (for pass 2)
 Macro Name Table (MNT) (to store names of defined
macros)
 Macro Definition Table (MDT) (to store definition body)
 Macro Definition Table Counter (MDTC) (next available
entry in the MDT)
 Macro Name Table Counter (MNTC) (next available entry in
the MNT)
 Argument List Array (ALA) (to substitute index markers for
dummy arguments before storing macro definition)
Ch 3 – Macros and Macro Processors 3/20/2024
2 pass Macro Processor
 Pass 2: handles macro calls and expansion
 Copy of input macro source deck
 Output expanded source deck (i/p for assembler)

 MDT

 MNT from Pass 1


 ALA

 Macro Definition Table Pointer (MDTP) (indicates the


next line of text to be used during macro expansion)

Ch 3 – Macros and Macro Processors 3/20/2024


2 pass Macro Processor
 Macro Definition Table
 Table of text lines
 80 byte string entries
 Each line of macro definition is stored in MDT
 MACRO is omitted but MEND is kept to indicate the end.
 Name line is kept to facilitate keyword replacement
 Macro Name Table (MNT)
 Each entry consists of a character string (macro name) and pointer to
entry in MDT
 Argument List Array (ALA)
 Dummy arguments in definition replaced by index markers (eg. #1) in
pass 1
 Index markers replaced by arguments in macro call

Ch 3 – Macros and Macro Processors 3/20/2024


2 pass Macro Processor
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Macro Pass 1
PASS 1

MDTC <― 1
MNTC <― 1
No
Read next Yes
END psedo- GO TO PASS 2
source card op?

MACRO No Write copy of


psedo-op? source card
Yes Read next
source card
Read next
source card Substitute index
MDTC <― MDTC + 1 notation for
Enter Macro name arguments No
and current value of Enter macro MEND
MDTC in MNT entry name card into Enter line psedo-op?
no MNTC MDT into MDT
Yes

MNTC <― MNTC + 1 Prepare Argument MDTC <― MDTC + 1


List Array
Ch 3 – Macros and Macro Processors 3/20/2024
Macro Pass 2
PASS 2

Read next
source card
(from pass 1) No
END Yes Supply expanded
Search MNT
pseudo-op? source file to
for match
assembler processing
with opcode

MACRO No Write into expanded


name found? source card file
Yes MEND Yes
Substitute arguments
MDTP <― MDT index from macro call pseudo-op?
from MNT entry No
Get line from MDT
Set up Argument list array Write into expanded
source card file
MDTP <― MDTP + 1

Ch 3 – Macros and Macro Processors 3/20/2024


Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024
Ch 3 – Macros and Macro Processors 3/20/2024

You might also like