0% found this document useful (0 votes)
80 views67 pages

SPOS Unit 2

SPOS unit 2 notes ppt

Uploaded by

unknown virus
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)
80 views67 pages

SPOS Unit 2

SPOS unit 2 notes ppt

Uploaded by

unknown virus
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/ 67

Unit -2

Macro Processor and Compiler


Introduction, Features of a Macro facility: Macro instruction arguments, Conditional
Macro expansion, Macro calls within Macros, Macro instructions, Defining Macro,
Design of two pass Macro processor, Concept of single pass Macro processor.
Introduction to Compilers: Phases of Compiler with one example, Comparison of
Compiler and Interpreter.
Introduction to Macro Processor
◦ Macro instructions are considered as the extension of the basic assembler
language.
◦ A macro instruction is convenient for the programmer in terms of notation.
◦ A macro is a single-line abbreviation used for a group of instructions.
◦ It allows the programmer to write shorthand version of a program (module
2
programming)
◦ The macro processor replaces each macro call with the corresponding sequence
of statements (expanding)
Macro Processor
Working of Macro Processor
1. Recognizes macro definitions
2. Saves the macro definition
3. Recognizes macro calls
4. Expands macro calls
Source Macro Expanded Compiler or
3

Code obj
Processor Code Assembler
(with macro)
Macro Definition
MACRO Start of Definition
<Macro Name> <List of Parameters [p1, p2, …pn]>
Macro Name (with label or argument(s) which is optional)
-------------
------------- Sequence to be abbreviated
4
-------------
MEND End of Definition
Macro Definition (cont..)
Example:
MACRO
ADDS &arg1, &arg2, &arg3
L 1, &arg1
A 1, &arg2
ST 1, &arg3 5

MEND
Macro Call
<Macro Name> <List of Actual parameters[ a1, a2, …an]>

For Ex.🡪

ADDS DATA1, DATA2, DATA3 6


Macro Expansion/Invocation
//Source Code with Macro definition & // Processed code after macro
macro call Expansion
MACRO PROG START 0
ADDS &arg1,&arg2,&arg3 BALR 15,0
L 1, &arg1
USING *,15
A 1, &arg2
ST 1, &arg3 L 1, DATA1
MEND A Macro
1, DATA2
PROG START 0 ST Expansion
1,DATA3
BALR 15,0
SR 4,4
USING *,15
ADDS DATA1, DATA2, DATA3 DATA1 DC F’3’
SR 4,4 DATA2 DC F’4’ 8
DATA1 DC F’3’ DATA3 DS 1F
DATA2 DC F’4’ END
DATA3 DS 1F
END
MACRO
ADDS &arg1,&arg2,&arg3
L 1, &arg1
A 1, &arg2
ST 1, &arg3
MEND
Macro Expansion (cont..)
Source program with Macro Definition & Macro call
Source program after macro expansion
MACRO
ADDS &arg1,&arg2,&arg3 PROG START 0
L 1, &arg1 BALR 15,0
A 1, &arg2 USING *,15
ST 1, &arg3 L 1, DATA1
MEND A 1, DATA2
PROG START 0
ST 1, DATA3
BALR 15,0 1st Macro
SR 4, 4
USING *,15 call
ADDS DATA1, DATA2, DATA3
L 1, D4
SR 4,4 A 1, D5 2nd Macro
ADDS D4, D5, D6 ST 1, D6 call
DATA1 DC F’3’ DATA1 DC F’3’
DATA2 DC F’4’ 10
DATA2 DC F’4’
DATA3 DS 1F DATA3 DS 1F
D4 DC F’1’
D4 DC F’1’
D5 DC F’2’
D5 DC F’2’
D6 DS 1F
END D6 DS 1F
END
Parameter Types/Substitution
▪Positional / Formal Argument
▪Keyword Argument
▪Default Argument
▪Mixed Argument
8/4/2021 12
Default Argument
//MACRO DEFINITION

MACRO
ADDS &arg1=, &arg2=, &arg3=4 Formal Parameter Value
L 1, &arg1 &arg1 D1
A 1, &arg2 &arg2 D2
ST 1, &arg3
MEND &arg3 100

/MACRO CALL
8/4/2021 17

ADDS &arg1=D1, &arg2= D2


……

ADDS &arg2= D2, &arg1=D1 //it overrides default value


Macro with Mixed Parameter Lists
Macro can use all three types of parameters together
Example:- //MACRO DEFINITION

MACRO
ADDS &arg1, &arg2=45, &arg3=
L 1, &arg1
A 1, &arg2
ST 1,&arg3
MEND

8/4/2021 //MACRO CALL 18

ADDS D1, &arg3= D3


……
ADDS D1, &arg3=D6, &arg2=400
Macro vs. Subroutine
Macro: Subroutine
▪ Every macro call is replaced by its ▪ Every Subroutine (function)
definition.
call transfers control to the
first instruction of subroutine
▪ After expansion of program, length which is called.
increases. So, more memory is
required.
▪ After processing subroutine
▪ Processing time is less as call, the program size
compared to subroutine
8/4/2021
processing, because there is no
remains same. 19

context switching during macro


processing.
▪ Processing time is increased
due to context switching.
Advanced Macro Facilities
▪Macro Calls within Macros
(Nested Macro Calls)
▪Macro instruction defining Macros
▪Conditional Macro Expansion
▪Expansion time variables
▪Expansion
8/4/2021 time Loops 20
Macro Calls within Macros
(Nested Macro Calls)
Macro definition of one macro may generate call to another macro is called as nested macro
calls.
Example.
MACRO
ADD1 &arg
L 1, &arg
A 1, =F’1’
ST 1, &arg
MEND
MACRO
8/4/2021 21
ADDS &arg1, &arg2 ,&arg3
ADD1 &arg1
Call to macro
ADD1 &arg2
MEND
Macro Instruction Defining Macros
Macro definition may defines another macro Expanded Macro
Ex.
MACRO
◦ CNOP 0,4
DEFINE &SUB
◦ BAL
MACRO
1,*+8
&SUB &Y ◦ DC
CNOP 0,4
A(AR)
BAL 1,*+8 ◦
Outer L
DC A(&Y) Inner
Macro 15,V(COS)
L 15,V(&SUB) Macro
◦ BALR 14,15
BALR 14,15
MEND
MEND
8/4/2021 23

Macro calls
◦ DEFINE COS
◦ COS AR
AIF , .FINI, AGO
.FINI :- it is macro label and don’t appear in output of macro processor.
AIF(&count EQ 1).FINI:- The pseuodo op directs the macro processor to
skip the statement labeled .FINI, if “&count” value is 1; otherwise , the
macro processor is to continue with the statement following AIF
pseuodo op.
AGO:- it is unconditional branch pseudo op like “go to”.

8/4/2021 26
Need of Conditional Macro Expansion
Lets consider example:
:
:
LOOP1 A 1, A1
A 2, A2
A 3, A3
:
:
LOOP2 A 1, A3
A 2, A2
:
:
LOOP3
8/4/2021 A 1, A1 30

:
:
A1 DC F’5’
A2 DC F’15’
A3 DC F’10’
Conditional Macro Expansion
MACRO // Expanded Source code
&a0 VARY &CNT, &a1, &a2, &a3
&a0 A 1, &a1
:
AIF (&CNT EQ 1) .FINI LOOP1 A 1, A1
A 2,& a2 A 2, A2
AIF (&CNT EQ 2) .FINI
A 3, &a3
A 3, A3
.FINI MEND :
: :
:
LOOP1 VARY 3, A1, A2, A3 LOOP2 A 1, A1
: A 2, A2
:
:
LOOP2 VARY 2, A3, A2
: :
: LOOP3 A 1, A1 31
LOOP3 VARY 1, A1
:
:
: :
A1 DC F’5’ A1 DC F’5’
A2 DC F’15’
A2 DC F’15’
A3 DC F’10’
A3 DC F’10’
Design of Two pass macro Processor
Pass-1 :
◦ Recognizes Macro Definition
◦ Stores Macro Instruction

Pass-2 :
◦ Recognizes Macro Calls
◦ Expands calls and substitute actual arguments
8/4/2021 33
Specifications of Data Structures
Pass-1 Data structures/ programs :
◦ Source program with macro definitions and macro calls
◦ Output file without macro definitions & with macro calls
◦ Macro Definition Table (MDT)
◦ Macro Name Table (MNT)
◦ Argument List Array (ALA)
◦ Macro Definition Table Counter (MDTC) : Integer Variable
◦ Macro Name Table Counter (MNTC): Integer Variable

Pass-2 Data structures/programs :


◦ Input file without macro definitions & with macro calls
34
◦ Expanded output file without macro definitions & macro calls (Free from Macro)
◦ Refers Macro Definition Table (MDT) created by Pass-1
◦ Refers Macro Name Table (MNT) created by Pass-1
◦ Argument List Array (ALA) to map formal parameters with actual
◦ Macro Definition Table Pointer (MDTP): Integer Variable
Two Pass Macro Processor
Data structures used in two pass macro processor:-

1. Macro Definition Table(MDT) – Store definition of macro


2. Macro Name Table (MNT)– Store name of macro along with address of macro
definition.
3. Argument List Array (ALA) – used to substitute index markers for dummy
arguments before storing a macro definition.
4. Macro definition table counter (MDTC) – used to indicate the next available
entry in the MDT
5. Macro name table counter (MNTC) – used to indicate the next available entry
in MNT. 35
6. Macro definition table pointer (MDTP) – used to indicate the next line of text
to be used during macro expansion.
Formats of Data Structures
● Macro Name Table (MNT)
MNTC Macro Name MDTC

1
ADDS 1
2
.
.

8/4/2021 36
Formats of Data Structures (Cont..)
● Macro Definition Table (MDT)
MDTC Macro Definition Instruction Entry (80 bytes per entry)
1
& Lab ADDS &A1, &A2
2 #0 A 1, #1
3 A 1, #2
4 MEND
5
:
:
:
Formats of Data Structures (Cont..)

● Arguments List Array (ALA)

Index Formal Arguments Actual Arguments

0 &Lab -
1 &A1 -
2 &A2 -
8/4/2021 38
3
:

:
Pass1 No

MDTC 🡨 1
END Yes GO TO
Pseudo
MNTC🡨 1 op ? PASS2

Read Next Source Card Write copy of source card

MACRO
Pseudo No
op ? Read Next Source Card
Yes
Prepare macro name card
Read Next Source Card and add into MDT Substitute index Notation
for Arguments
Enter Macro Name &
MDTC🡨MDTC+1
Current value of MDTC in
MNT Enter Line into MDT
8/4/2021

MNTC🡨MNTC+1 MDTC🡨 MDTC+1


Prepare ALA Add MEND in MDT,
MEND
Yes Pseudo No
MDTC🡨 MDTC+1
op ?
39 21
Source Code : Intermediate Code after pass1:
MAC START 100
MACRO
MAC
&A0 START
ADD1 100
&A1, &A2, &A3 MAC START 100
&A0 MACRO
L 1, &A1 TOTAL EQU 5
&A0 L
ADD1 2, &A2&A2, &A3
&A1, L 1, D1
&A0 LAR 1, 2&A1
1,
SR 2, 2
LMUL 1, &A3
2, &A2
ST
AR N, 2
1, 1 A 1, =F’5’
MEND ADD1 LOOP1, D1, D2, D3
MUL 1, &A3
MACRO
ST
SUB
N, 1
&P1, &P2
ST 2, 1
MEND
L 1, &P1 AR TOTAL, 2
MACRO
S 1, &P2 SUB X, Y
SUB
ST &P1,
2, 1 &P2 BR 14
LMEND 1, &P1
S 1, &P2 D1 DC F’3’
ST 2, 1 D2 DC F’45’
MEND D3 DC F’21’
TOTAL EQU 5 X DC F’10’
L 1,D1 Y DC F’20’
8/4/2021
SR 2,2 40
A 1,=F’5’ END
ADD1 LOOP1, D1, D2, D3 47
ST 2, 1
AR TOTAL, 2 35
SUB X, Y
BR 14
D1 DC F’3’
Macro Name Table : Macro Definition Table :
MNTC Macro Name MDTC MDTC Macro Card
1 1

Argument List Array :


Ind Formal Args Actual Args
ex
1
2
3
4 8/4/2021 41

5
6
34
PASS 2
Read Next Source card

Search MNT for matching operation


code

Macro Name No Write Expanded Source Card


Found ?
Yes
MDTP🡨 MDT Index from
END Pseudo No
MNT Entry Op?
Yes
Set Up ALA
Supply expanded source file to
MDTP🡨 MDTP +1 Assembler Processing

Get Line From MDT


42

Substitute arguments from Macro


Call

Yes MEND Pseudo No Write Expanded Source Card


Op?
ALA Intermediate Code after pass1 Expanded Source Code :
Ind Formal Actual
ex (input file for pass 2): MAC START 100
TOTAL EQU 5
1 &A0 LOOP1
L 1, D1
2 &A1 D1 MAC START 100 SR 2, 2
TOTAL EQU 5 A 1, =F’5’
3 &A2 D2
L 1, D1 LOOP1 L 1, D1
4 &A3 D3
SR 2, 2 L 2, D2
5 &P1 X A 1, =F’5’ AR 1, 2
6 &P2 Y ADD1 LOOP1, D1, D2, D3 MUL 1, D3
ST 2, 1 ST N, 1
ST 2, 1
AR TOTAL, 2 AR TOTAL, 2
SUB X, Y L 1, X
BR 14 S 1, Y
8/4/2021 D1 DC F’3’ ST 2, 1
43
D2 DC F’45’
D3 DC F’21’ BR 14
X DC F’10’ D1 DC F’3’
, 5, 6, 7 for ADD1 D2 DC F’45’
11, 12, 13 for SUB Y DC F’20’ D3 DC F’21’
END X DC F’10’
Click on link for Example of Macro :View
What is Compiler?
•Compiler is a software which converts a program
written in high level language (Source Language) to low
level language (Object/Target/Machine Language) .
Let’s have a look at, what happens inside Black
Box

8/4/2021 46
Grammar is a set of production rules that defines the syntax of a language

Terminals: A set of terminals which we also refer to as tokens, these set of tokens forms strings.
Non-Terminals: CFG has a set of non-terminals (variables). These variables represent the set of
strings. Further, they contribute to the formation of a language generated by grammar.

Production: A grammar has a set of production rules. These rules specify how to combine
terminals and non-terminals to form a string

Example of Context-free Grammar:


Let us consider a grammar production

stmt -> if (expr) stmt else stmt


Code Optimizer
● Optimization: Compilers can apply various optimization
techniques to the code, such as loop unrolling, dead
code elimination, and constant propagation, which can
significantly improve the performance of the generated
machine code.
● It transforms the code so that it consumes fewer
resources and produces more speed.
● The meaning of the code being transformed is not
altered.
● Optimization can be categorized into two types:
machine-dependent and machine-independent.
Symbol Table
▪Compiler uses symbol table to keep track of scope and binding information about
names.
▪Symbol table is searched every time when a name is encountered in source text.
▪Changes occur in symbol table if a new name or new info about existing name is
discovered.
▪Compiler should grow S.T. dynamically at compile time or the symbol table can be kept
fixed.
8/4/2021 55
The Symbol Table
▪When identifiers are found, they will be entered into a symbol table, which will hold all relevant
information about identifiers.
▪When the symbol is first encountered by the lexer, we do not yet know the scope.
▪That is determined later by the parser.
▪This information will be used later by the semantic analyzer and the code generator.

Lexical Syntax Semantic Code


Analyzer Analyzer Analyzer Generator 56

Symbol
Table
Symbol Table Entries
We will store the following information about identifiers.

▪The name (as a string).


▪The data type and value.
▪The block level.
▪8/4/2021
Its scope (global, local, or parameter). 57
▪Its offset from the base pointer (for local variables and parameters only).
•https://
www.javatpoint.com/
compiler-phases
The Structure of a Compiler

Code Generator
[Intermediate Code Generator]

Non-optimized Intermediate Code


Scanner
[Lexical Analyzer]

Tokens

Parser Code Optimizer


[Syntax Analyzer]
Optimized Intermediate
Parse tree Code

Semantic Process
Code Generator
8/4/2021 [Semantic analyzer]
Target machine
Abstract Syntax Tree w/ Attributes code

59
Interpreter
It is a translator (language processor) which directly executes
operations specified in source program on input supplied by
user.

SOURCE PROGRAM
Interpreter Output
Input
8/4/2021 60
Interpreter vs Compiler
Interpreter Compiler
Scans the entire program and translates it as a whole into
Translates program by one statement at a time.
machine code.

It takes less amount of time to analyze the source code It takes large amount of time to analyze the source code
but the overall execution time is slower. but the overall execution time is comparatively faster.

No intermediate object code is generated, hence less Generates intermediate object code which further
memory are required. requires linking, hence requires more memory.

Continues translating the program until the first error is It generates the error message only after scanning the
met, 8/4/2021
in which case it stops. Hence debugging is easy. whole program. Hence debugging is comparatively 61
hard.

Programming language like Python, Ruby use


Programming language like C, C++ use compilers.
interpreters.
Case Study- GNU m4 Macro
Processor
▪GNU M4 is an implementation of the traditional Unix macro processor
▪m4 is a macro processor, it copies its input to the output, expanding macros as it

goes.

▪Macros are either built-in or user-defined, and can take any number of
arguments.
8/4/2021 62

https://www.gnu.org/software/m4/
Case Study- GNU m4 Macro Processor
1. m4 is a general-purpose macro processor included in most
Unix-like operating systems,

2. Kernighan and Ritchie developed m4 in 1977,


Case Study- GNU m4 Macro
Processor
The format of the m4 command is:
m4 [option...] [macro-definitions...] [input-file...]

m4 --def foo --debug a is equivalent to 🡪


m4 --define=foo --debug= -- ./a

8/4/2021 64
Example of Macro in C
language
#include <stdio.h>
#define NAME "TechOnTheNet.com"
#define AGE 10
int main()
{
printf("%s is over %d years old.\n", NAME, AGE);
return 0;
}
References
1) John Donovan, “Systems Programming”, McGraw Hill, ISBN 978-0--07-460482-3

2) Dhamdhere D., "Systems Programming and Operating Systems", McGraw Hill, ISBN 0 - 07 - 463579 – 4

3) https://www.gnu.org/software/m4/manual/m4.html#Syntax

Ebooks:

▪ https://www.elsevier.com/books/systems-programming/anthony/978-0-12-800729-7

▪ https://www.kobo.com/us/en/ebook/linux-system-programming-1

▪ https://www.e-booksdirectory.com/details.php?ebook=9907

MOOCs Courses Links:


8/4/2021 66
▪ nptel video lecture link: https://nptel.ac.in/courses/106/105/106105214/

▪ https://onlinecourses.nptel.ac.in/noc19_cs50/preview

▪ https://www.udemy.com/course/system-programming/
Thank You !!
8/4/2021 67

You might also like