SPCC Module 3 Macros-converted
SPCC Module 3 Macros-converted
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
St. Francis Institute of Technology 09 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 4
Macro Instruction
• 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
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
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
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
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
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
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
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
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
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
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
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
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
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