0% found this document useful (0 votes)
51 views35 pages

SPCC Module 3 Macros-converted

The document outlines the syllabus and key concepts of the course CSC 602: System Programming and Compiler Construction, focusing on macros and macro processors. It explains macro definitions, features, and the design of a single pass macro processor, including the handling of macro instruction arguments and conditional macro expansion. Additionally, it discusses the implementation of macros and the structure of a 2-pass macro processor for managing macro definitions and calls.

Uploaded by

zeel94996
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)
51 views35 pages

SPCC Module 3 Macros-converted

The document outlines the syllabus and key concepts of the course CSC 602: System Programming and Compiler Construction, focusing on macros and macro processors. It explains macro definitions, features, and the design of a single pass macro processor, including the handling of macro instruction arguments and conditional macro expansion. Additionally, it discusses the implementation of macros and the structure of a 2-pass macro processor for managing macro definitions and calls.

Uploaded by

zeel94996
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/ 35

System Programming and Compiler

Construction
CSC 602

Subject Incharge
Varsha Shrivastava
Assistant Professor
email: [email protected]
Room No: 407

1
CSC 602 System Programming and
Compiler Construction
Module 3
Macros and Macro Processor

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 2
Contents as per syllabus

▪ Introduction
▪ Macro definition and call
▪ Features of Macro facility: Simple,
parameterized, conditional and nested.
▪ Design of Single pass Macro Processor, data
structures used.

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 3
Introduction

• Macro instructions or Macros are single line


abbreviations for groups of instructions.

• Single instruction is used to represent a block of


code.

• For every occurrence of this one line macro


instruction, the macro processing assembler will
substitute the entire block.

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 4
Macro Instruction

• Macro is an abbreviation for a series of operations.

• Example: Start of
definition
Macro
A MACRO name
1,DAT INCR INCR
A A
Repeated Sequence to be
A 1,DAT
code A
abbreviated
A 2,DAT INCR
A 1,DAT A
A 2,DAT
A 3,DAT A
A 2,DAT A End of
A 3,DAT
definition
A A
3,DAT MEND
A
St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 5
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; {
printf(“enter a no : ”); int a;
scanf(“%d”,&a); printf(“enter a no : ”);
Square(a); scanf(“%d”,&a);
} ((a)*(a));
}

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 6
Macros in Assembly code (8086)
macro print msg
mov dx, offset msg start:
mov ah, 09h
int 21h .
endm .
data segment
mov dx, offset msg
Msg1 db “hello world$”
data ends mov ah, 09h
code segment int 21h
. .
. .
print msg1
.
code ends
. end start
end start

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 7
Basic Macro Processor functions

Expanded program
A program with
Macro A program without
Macro definitions and
Processor Macro definitions
Macro invocations

Assembler

Object program

Macro Processor is an in-built functions of Assembler


St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 8
Features of Macro Facility

• Macro Instruction Arguments

• Conditional Macro Expansion

• Macro calls within Macros

• Macro Instruction defining Macros

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 9
Features of Macro Facility

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.

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 10
Features of Macro Facility
Macro Instruction Arguments
A 1,FIVE
A 2,FIVE
A 3,FIVE
------
------
------

A 1,FOUR
A 2,FOUR
A 3,FOUR

FIVE DC F’5’
FOUR DC F’4’

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 11
Features of Macro Facility
Macro Instruction Arguments
A 1,FIVE MACRO
A 2,FIVE ADDM &ARG
A 3,FIVE A 1, &ARG
------ A 2, &ARG
------ A 3, &ARG
------ MEND
------
------
A 1,FOUR ------
A 2,FOUR ADDM FIVE
A 3,FOUR

FIVE DC F’5’ ADDM FOUR


FOUR DC F’4’
FIVE DC F’5’
FOUR DC F’4’

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 12
Features of Macro Facility
Macro Instruction Arguments
When we pass more than one argument there
are 2 ways to specify these arguments:-

1.Positional Arguments
2.Keyword Arguments.

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 13
Features of Macro Facility

Positional Parameter Keyword Parameter


MACRO MACRO
M1 &P1,&P2,&P3 M1 &P1=,&P2=,&P3=
---- ----
---- ----
----- -----
MEND MEND
M1 A, B, C M1 &P1=A, &P2=B, &P3=C

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 14
Features of Macro Facility

Default Parameter
MACRO
M1 &P1=A, &P2=B
----
----
-----
MEND
M1 &P1=, &P2=C

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 15
Features of Macro Facility
Macro Instruction Arguments
Original Source Original Source
Expanded Source Expanded Source
(single argument) (Multiple argument)
MACRO MACRO
INCR INCR &ARG1,&ARG2
&A A 1, &ARG1
RG A 1,DATA1 A 2, &ARG2
A 1, MEND A 1,DATA1
A 2,DATA1
&ARG A 2,DATA2
A 3,DATA1
A 2, INCR DATA1,DATA2
ARG
INCR DATA1 A 1,DATA2
A 1,DATA2 A 2,DATA1
A 3, INCR DATA2,DATA1
A 2,DATA2
&ARG
INCR DATA2 A 3,DATA2
MEND

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 16
Features of Macro Facility

Conditional Macro Expansion


• AIF and AGO 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

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 17
Features of Macro Facility

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
A 3,DATA3
3,DATA1,DATA2,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

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 18
Features of Macro Facility

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

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 19
Features of Macro Facility

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
L 1,DATA3
DATA1,DATA2,DATA
ADD1 DATA3 A 1,=F’1’
3
ST 1,DATA3

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 20
Features of Macro Facility

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).
St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 21
Features of Macro Facility

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
BALR 1 4,15 Transfer control to
subroutine
MEND
DEFINE MEND
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
St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 22
Implementation of Macros

There are 4 tasks that any macro instruction


processor perform:
1. Recognize macro Definition
2. Save the Definition
3. Recognize calls
4. Expand calls and Substitute arguments.

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 23
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

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 24
2-Pass Macro Processor

• Pass 1: handles macro definitions (Database)


• Input macro source deck
• Output macro source deck copy (for pass 2)
• Macro Definition Table (MDT) (to store definition body)
• Macro Name Table (MNT) (to store names of defined
macros)
• 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)

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 25
2-Pass Macro Processor

• Pass 2: handles macro calls and expansion


(Database)
• 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)

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 26
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
Index Card
15 &LAB INCR &ARG1, &ARG2,
&ARG3
16 #0 A 1,#1
17 A 2,#2
18 A 3,#3
19 MEND
MD
T
St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 27
2-Pass Macro Processor
• 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

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 28
Macro Pass 1 Flow Chart

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 29
Macro Pass 2 Flow Chart

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 30
Example
MACRO
&LAB INCR &ARG1, &ARG2, &ARG3
&LAB A 1, &ARG1
A 2, &ARG2
A 3, &ARG3
MEND
.
.
LOOP1 INCR DATA1,DATA2,DATA3
.
.
LOOP2 INCR DATA1,DATA2,DATA3
.
.

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 31
Example Pass1

MNT ALA
Inde Card MDT Index Index Argument
1x INCRbbbb 1 0 &LAB
1 &ARG1
2 &ARG2
MD 3 &ARG3
Index T Card
1 &LAB INCR &ARG1,&ARG2,&ARG3
2 #0 A 1,#1 MDTC 45
16
2
3
3 A 2,#2 MNT 12
C
4 A 3,#3
5 MEND

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 32
Example Pass 2

MNT ALA
Inde Card MDT Index Index Argument
1x INCRbbbb 1 0 LOOP1bbb
1 DATA1bbb
2 DATA2bbb
MD 3 DATA3bbb
Index T Card
2 #0 A 1,#1
3 A 2,#2 MDTP 5
4
2
1
3
4 A 3,#3
5 MEND

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 33
University Questions

• Define Macro. Explain macro calls within macro


giving example
• What is positional parameter in macro?
• Explain two pass macro processor with flowchart
and databases
• Explain the different ways of parameter passing in
macros?
• Detail the different features used in macro
processing.

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 34
Practice Questions

St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 35

You might also like