0% found this document useful (0 votes)
30 views139 pages

UNIT-I SPOS (Autosaved)

The document provides an overview of system software, including its definition, types, and functions. It distinguishes between system software and application software, explaining the role of language processors such as assemblers, compilers, and interpreters in converting high-level and assembly languages to machine code. Additionally, it covers the architecture of system software, its goals, and the phases of a compiler, emphasizing the importance of user convenience and efficient resource management.

Uploaded by

aditi.22310223
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)
30 views139 pages

UNIT-I SPOS (Autosaved)

The document provides an overview of system software, including its definition, types, and functions. It distinguishes between system software and application software, explaining the role of language processors such as assemblers, compilers, and interpreters in converting high-level and assembly languages to machine code. Additionally, it covers the architecture of system software, its goals, and the phases of a compiler, emphasizing the importance of user convenience and efficient resource management.

Uploaded by

aditi.22310223
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/ 139

UNIT-I

Subject-LPCC

Prof. Shweta Tiwaskar


[email protected]
Department of Computer Engineering

BRACT’S, Vishwakarma Institute of Information Technology, Pune-48


(An Autonomous Institute affiliated to Savitribai Phule Pune University)
(NBA and NAAC accredited, ISO 9001:2015 certified)

1
Introduction
• What is System Software?
• Is a collection of programs that bridge the gap
between the level at which users wish to
interact with the computer and the level at
which computer is capable of operating

2
What is Software ?
• Software is collection of many programs.

• Two types of software:

– System software-
These programs assist general use application
programs.
Ex:- Compiler , Assembler etc.

– Application software-
These are the software developed for the specific goal.
3
System Software
• It forms a software layer which act as a
intermediary between the user and the computer
• It is a collection of programs that facilitate
execution of programs and use of resources in a
computer system
• Each program in the system software is called a
system program
• System Programming is the collection of
techniques used in the design of system programs

4
Introduction
• Software
– Application software usually used by end-user
• It is concerned with the solution of some problem,
using the computer as a tool, instead of how computers
actually work.
– System software
• System software consists of a variety of programs that
support the operation of a computer (ex: text editor,
compiler, debugger)
• One characteristic in which most system software differ
from application software is machine dependency
• A system software programmer must know the target
machine structure

5
System Software
Users
Concept
Application Program

Utility Program
Debugger Macro Processor Text Editor
(Library)

Complier Assembler Load and Linker

OS
Memory Process Device Information
Management Management Management Management

Bare Machine (Computer)


6
Need of System Software (Language
Processors)

Operating System, Language


Processors, Device Drivers

CPU, Hard disks, CD-ROMs,


Keyboard, Mouse, Printer, Camera,
Scanners, Fax machines, Video
Adapters, Network Cards, Sound
cards

7
System Software Types
• Operating Systems : (Software that manages hardware, software resources,
and provides common services for computer programs)

• Device Drivers : (Software that Interface with specific device attached to computer)

8
Types of Languages
• Binary Code (Machine language) – Uses 0/1
• Assembly Languages - Uses Mnemonics
• High Level Languages (HLLs) - Uses English like
words

9
Language Processors (LP)
• LP = System Software which converts Source
code to Machine readable code
– Source Code (Written in HLLs, Assembly Lang)
– Object Code / Machine Code - Code in machine
readable form

10
LP Types
• Assemblers – Assembly level code to Machine
level code
• Interpreters – HLLs to machine level, but line
by line
• Compilers - HLLs to machine level, but in one
go

11
Introduction
– System Software consists of a variety of
programs that support the operation of
a computer.
– The software makes it possible for the
users to focus on an application or other
problem to be solved, without needing
to know the details of how the machine
works internally.

12
System Software and Machine
Architecture
• System Software vs Application
– One characteristic in which most system
software differs from application software is
machine dependency.
– System programs are intended to support the
operation and use of the computer itself, rather
than any particular application.
• Examples of system software
– Text editor, assembler, compiler, loader or linker,
debugger, macro processors, operating system,
database management systems, software
engineering tools, …
13
Goals of System Software
• User Convenience
Provide convenient method of using a computer
system
• Efficient Use
Ensure efficient use of computer resources

14
Fill in the Blanks
• Each program in the system software is called
a ____
• _____facilitate execution of programs and use
of resources in a computer system
• Goals of system software ___, _____

15
Assembler
• Assembler:-
• These are the system programs which
translate the assembly language program into
the machine language program.
Target program /
Source program M/C Lang. Prog.
Assembly Lang. Assembler
Prog.

Database

16
Complier
• Complier:-
• These are the system programs which
translate the High level language program into
the machine language program.

Source program Target program /


High level Lang. Compiler M/C Lang. Prog.
Prog.

Database

17
Cross Assembler:-
• Cross Assembler:-
• These are the system programs which will automatically
translate the Assembly Language program compatible with
M/C A, in to the machine language program compatible with
M/C A, but the underlying M/C is M/C B

Target program /
Source program M/C Lang. Prog.
Assembly Lang. Cross Assembler
Compatible with
Prog. Compatible M/C A
with M/C A

M/C B
18
System Software
• Text editor
– To create and modify the program
• Compiler and assembler
– Translate these programs into machine
language
• Loader or linker
– The resulting machine program was loaded
into memory and prepared for execution
• Debugger
– To help detect errors in the program
19
System Software and Machine Architecture
• Machine dependent
– Instruction Set, Instruction Format, Addressing
Mode, Assembly language …
• Machine independent
General design logic/strategy, Two passes
assembler…
Machine independent
Machine Dependent

Computer

20
System Software
and Machine Architecture

• Machine dependent system software


– System programs are to support the operation and use of the target
computer.
– The difference between different machine
• Machine code
• Instruction formats
• Addressing mode
• Registers
• Machine independent system software
– General design and logic is basically the same:
• Code optimization
• General design and logic of an assembler

21
Programming Languages
• Machine language:
• It is computer’s native language having a sequence of zeroes
and ones (binary). Different computers understand different
sequences. Thus, hard for humans to understand: e.g.0101001...

• Assembly language:
• It uses mnemonics for machine language. In this each instruction
• is minimal but still hard for humans to understand:
• e.g. ADD AH, BL

• High-level languages:
• FORTRAN, Pascal, BASIC, C, C++, Java, etc.
Each instruction composed of many low-level instructions,
closer to English. It is easier to read and understand:
e.g. hypot = sqrt(opp*opp + adj * adj);

22
Fill in the Blanks
• _______ is used to create and modify the
program.
• ________is used to detect errors in the
program.
• ____ , computer program which translate
the program from high level language to
machine language.
• _______, computer program which
translate the program from assembly
language to machine language.
23
We know…
• Source Program – Assembly Language
• Object Program - From assembler
- Contains translated instructions
and data values from the source program
• Executable Code - From Linker
• Loader - Loads the executable code to the
specified memory locations and code gets
executed.

24
Fill in the Blanks
• _____ is a program which load programs from
a secondary to main memory so as to be
executed
• _____ is a computer program that takes one
or more object files generated by a compiler
and combines them into a single executable
file

25
Machine and Assembly Languages
Library routines
(machine language)
MIPS, 80x86,
PowerPC, etc.

Executable
Assembly Machine machine
language language language Memory
Assembler

program program

Loader
program content

Linker
add $2,$5,$5 00a51020
add $2,$2,$2 00421020
add $2,$4,$2 00821020
lw $15,0($2) 8c620000
lw $16,4($2) 8cf20004
sw $16,0($2) acf20000
sw $15,4($2) ac620004
jr $31 03e00008

Figure : Steps in transforming an assembly language program to an executable


program residing in memory.

26
Assembly Process
• Convert assembly language file (.asm)
into an executable file (.obj) .

• First Pass:
– scan program file
– find all labels and calculate the corresponding addresses;
this is called the symbol table
• Second Pass:
– convert instructions to machine language,
using information from symbol table

27
Assembly
code

Assembler

Object code
28
D. Assembler
1. Assembly language programming
2. Simple assembly scheme,
3. Pass structure of assembler,
4. Design of two pass assembler

29
Elements of assembly language programming

• Mnemonic operation code (Mnemonic


opcode)
• Symbolic operands
• Data declarations
Assembly language is a machine dependent, low level
programming language which is specific to a certain
computer system (or a family of computer system)

30
Assembly Language Syntax

• Each line of a program is one of the following:


– an instruction
– an assembler directive (or pseudo-op)
– a comment
• Whitespace (between symbols) and case are ignored.
• Comments (beginning with “;”) are also ignored.
• An instruction has the following format:

LABEL OPCODE OPERANDS ; COMMENTS

optional mandatory
31
Introduction-Macro Processor

• Macro : a abbreviation for a group of


instructions.
• Acts as a preprocessor to assembler
• Source prg : assembly program with macros
• Target prg : assembly program without macros
Source Macro Target
Program processor program

32
Data structures
• Macro Name Table (MNT)
• Macro Definition Table (MDT)

33
Macro Name Table (MNT)

Name of No. of Starting End Index


macro paramete Index (row)
rs (row)
SAMPLE1 0 1 2

SAMPLE2 0 3 4

SAMPLE3 0 5 6

34
Macro Definition Table (MDT)

1 LOAD A
2 ADD B
3 LOAD X
4 SUB Y
5 LOAD P
6 DIV Q

35
Modified MNT & MDT
1 LOAD A
Name No. of Starting 2 ADD B
of parameter Index
macro s 3 MEND
SAMPLE 0 1 4 LOAD X
1
5 SUB Y
SAMPLE 0 4 6 MEND
2

7 LOAD P
SAMPLE 0 7
3 8 DIV Q
9 MEND

36
Introduction-Compiler

• Translator for conversion of HLL to machine language


• Source prg : High Level Language program (e.g.
sample.c)
• Target prg : machine language program (e.g.
sample.obj)

Source Compiler Target


Program program

Error
Messages
37
The phases of a compiler
Source program

Lexical Analysis

Syntax Analysis

Semantic Analysis
Symbol Error
table handler
manager
Intermediate code generation

Code optimization

Code generation

38
Target program
• Lexical Analyzer:
• It combines characters in the source file, to
form a "TOKEN". A token is a set of characters
that does not have 'space', 'tab' and 'new
line'.
• It is also called "TOKENIZER"
• It also removes the comments, generates
symbol table and relocation table entries

39
• Syntactic Analyzer:
• This unit check for the syntax in the code. For
ex:
• {
• int a; int b; int c; int d;
• d=a+b-c*;
• }
• This unit checks this internally by generating
the parser tree

40
Parse Tree
Therefore this unit is also called PARSER

41
• Semantic Analyzer:
• This unit checks the meaning in the
statements. For ex
{
• Int i;
• Int *p;
• P=i;
}
The above code generates the error “Assignment
of incompatible type”

42
• Code Optimization
• This unit optimizes the code in following
forms:
I) Dead code elimination
II) Sub code elimination
III) Loop optimization

43
Dead code elimination:
{
Int a= 10;
If(a>5)
{…..}
Else
{
}
• compiler knows the value of 'a' at compile time,
therefore it also knows that the if condition is always
true. Hence it eliminates the else part in the code
44
• Sub code elimination:
Int a, b, c;
Int x,y;
{
int a, b, c; int x, y;
/* ... */
x = a + b;
y = a + b + c;
/* ... */
}
45
• {
• int a, b, c;
• int x, y;
• /* ... */
• x = a + b;
• y = x + c; // a + b is replaced by x
• /* ... */
• }

46
• Loop optimization:
For ex:
{ int a;
for (i = 0; i < 1000; i++ )
{ /* ... */
a = 10;
/* ... */
}}

47
• if 'a' is local and not used in the loop, then it
can be optimized as follows
• { int a;
• a = 10;
• for (i = 0; i < 1000; i++ )
• {
• /* ... */
• }}

48
Code Generation

• It takes the optimized representation of the


intermediate code and maps it to the target
machine language
• It translates the intermediate code into a
sequence of (generally) re-locatable machine
code
• Sequence of instructions of machine code
performs the task as the intermediate code
would do
49
Symbol Table

• It is a data-structure maintained throughout


all the phases of a compiler
• All the identifier's names along with their
types are stored here
• The symbol table makes it easier for the
compiler to quickly search the identifier
record and retrieve it
• The symbol table is also used for scope
management

50
Interaction of Lexical analyzer with parser

token
Source Lexical parser
program analyzer
getNexttoken()

symbol
table

51
• Some terminology:
– Token: a group of characters having a collective
meaning. A lexeme is a particular instant of a token.
• E.g. token: identifier, lexeme: pi, etc.
– pattern: the rule describing how a token can be
formed.
• E.g: identifier: ([a-z]|[A-Z]) ([a-z]|[A-Z]|[0-9])*

• Lexical analyzer does not have to be an individual


phase.
• But having a separate phase simplifies the design
and improves the efficiency and portability.
52
Specification of tokens
• In theory of compilation regular expressions
are used to formalize the specification of
tokens
• Regular expressions are means for specifying
regular languages
• Example:
• Letter_(letter_ | digit)*
• Each regular expression is a pattern specifying
the form of strings
53
Regular expressions
• Ɛ is a regular expression, L(Ɛ) = {Ɛ}
• If a is a symbol in ∑then a is a regular
expression, L(a) = {a}
• (r) | (s) is a regular expression denoting the
language L(r) ∪ L(s)
• (r)(s) is a regular expression denoting the
language L(r)L(s)
• (r)* is a regular expression denoting (L(r))*
• (r) is a regular expression denting L(r)
54
Automatic construction of lexical
analyzer using LEX

Lex

– generates C code for the lexical analyzer (scanner)


– Token patterns specified by regular expressions

55
Lex
lex.yy.c
• Lex Source file  Lex compiler
C compiler a.out
Input stream  Scanner Sequence of tokens

56
Lexical Analyzer Generator - Lex

Lex Source program


Lexical Compiler lex.yy.c
lex.l

C
lex.yy.c a.out
compiler

Input stream a.out


Sequence
of tokens

57
Lex program structure
… definitions …
%%
… rules …
%%
… subroutines …

58
Three parts to Lex
1. Declarations
It has global C and Lex declaration
– Regular expression definitions of tokens
%{ This is a sample Lex program written by....%}
digit --> [0-9]
number -- > {digit} +

2. Transition Rules
pattern
– Regular Expression +Action when matched
{number} { printf("The number is %s\n", yytext); }
junk { printf("Junk is not a valid input!\n"); }
quit { return 0; }

3. Auxilliary Procedures
– Written into the C program…..
– int main() is required

%% separates the three parts

59
LEX program
%{/*recognition of Verb*/%}
%%
[/t ]+ ;
is I
am I
are I
were I
was I
be {printf("%s: is a verb\nn, yytext); }
[a-zA-Z]+ {printf(“%s:is not a verb\n”, yytext);}

%%
Main()
{
yylex();
} 60
Assignment 3
• Write a program to implement a lexical analyzer for parts of
speech.
INPUT:
Ram ran quickly

OUTPUT:

Noun: Ram
Verb: ran
Adverb: quickly

61
%{
/* Part of speech program */
%}
%%
Ram|
SAM { printf(“%s, is a Noun”,yytext); }
ran |
walk { printf(“%s, is a verb”,yytext); }
quickly |
Slowly { printf(“%s, is a adverb”,yytext); }
[A-Za-z]+ {printf(“%s, is not a verb”,yytext);
%%
Main()
{
yylex();
}

62
%{
/* Part of speech program */
%}
%%
int|
char!|
float { printf(“%s, is a datatype”,yytext); }
quickly |
Slowly { printf(“%s, is a adverb”,yytext); }
[A-Za-z]+ {printf(“%s, is not a verb”,yytext);
%%
Main()
{
yylex();
}

63
Example
% {
% }

delim [ \t\n]
ws {delim}+
letter [A-Za-z]
digit [0-9]
id {letter}({letter}|{digit})*
number {digit}+(\.{digit}+)?(E[+\-]?{digit}+)?

%%

{ws} { /* no action and no return */ }


if {return(IF);}
then {return(THEN);}
else {return(ELSE);}
{id} {yylval = install_id(); return(ID);}
{number} {yylval = install_num();return(NUMBER);}

%%

64
• Available variables
– Yylval –value associated with token
– yytext (null terminated string)
– yyleng (length of the matching string)
– yyin : the file handle
• yyin = fopen(args[0], “r”)
• Available functions
– yylex() (the primary function generated)-starts
the analysis
– input() - Returns the next character from the
input
– int main(int argc, char *argv[])
• Calls yylex to perform the lexical analysis
• Int yywrap(void) wrapup, return 1 if
done, 0 if not done

65
Regular Expression in Lex
• A- matches A
• (abc) –matches abc
• [abc]- matches a, b or c
• [0-9] - matches any digit
• [0-9]+ - matches any integer

66
Transition diagrams
• Transition diagram for relop

67
Transition diagrams (cont.)
• Transition diagram for reserved words and
identifiers

68
Transition diagrams (cont.)
• Transition diagram for unsigned numbers

69
A. LOADERS AND LINKERS

• A loader is a program which load programs


from a secondary to main memory so as to be
executed.
• Loader is a program which accepts the object
program decks, prepare these programs for
execution by the computer, and initiates the
execution.

70
• Linker is a computer program that takes one or
more object files generated by a compiler and
combines them into a single executable file
• Many programming languages allow you to write
different pieces of code, called modules,
separately.
• This simplifies the programming task because you
can break a large program into small, more
manageable pieces.
• Eventually, though, you need to put all the
modules together.
• This is the job of the linker
71
• a linker also replaces symbolic addresses with
real addresses

72
Role of a Loader

Source Object Object

Program Assembler Loader program


ready for
Program execution

73
Role of a Loader and Linker
Memory

Source Object
Program Assembler Linker
Program Object
program
ready for
Executable execution

Code

Loader

74
Assembly language statements

• Imperative statements
– Indicate an action to be performed during the execution.
• Declarative statements
– [label] DS <constant>
– [label] DC <value>
• A DS 1
• B DS 200
• ONE DC ‘1’
• Assembler directives
– START <constant>
– END [<operand spec >]


75
An Assembly Language Program

76
Data structures of the assembler

77
Data structures for two pass assembler
• Mnemonic opcode table
• Symbol table
• Literal table
• Pool table

78
Declarative Statements
• Define Constant(DC) :The define constant
assembler directive allows you to put a data
value in memory at the time that the program
is first loaded
• Define Storage(DS) : The define storage
directive is used to reserve one or more
memory locations

79
Advanced Assembler Directives

• ORIGIN
• EQU
• LTORG

80
• LTORG :Use the LTORG instruction so that the
assembler can collect and assemble literals into a
literal pool
• Equate: The EQU assembler directive simply
equates a symbolic name to a numeric value.
Consider: Sunday EQU 1
• Monday EQU 2
• You could also write Sunday EQU 1
• Monday EQU Sunday + 1
• In this case, the assembler evaluates "Sunday + 1"
as 1 + 1 and assigns the value 2 to the symbolic
name "Monday
81
• Origin : The origin directive tells the assembler
where to load instructions and data into
memory
• ORG 1024

82
Mnemonic operation code (Mnemonic opcode)

Instruction Opcode Mnemonic

00 STOP

01 ADD

02 SUB

03 MULT

04 MOVER

05 MOVEM

06 COMP

07 BC

08 DIV

09 READ
83
10 PRINT
Code for declaration statements and directives

84
An assembly program

85
Data structures of assembler pass I

86
Overview of two pass assembler

87
Tasks of two pass assembler

• Pass 1
– Separate the symbol, mnemonic opcode &
operand fields.
– Build the symbol table
– Perform LC processing
– Construct intermediate representation.
• Pass 2
– Synthesize the target program.
88
An Intermediate code unit

Address Opcode Operands

89
Intermediate code – variant I

90
Intermediate code – variant II

91
Memory requirements using variant I and variant II

92
Symbol Table (ST)
• Symbol table is used for keeping the track of symbol
that are defined in the program
• It is used to give a location for a symbol specified
• Symbol is said to be defined when it appears in a
label field.
• In pass 1, whenever a symbol is defined, entry is
made in symbol table
• In pass2, symbol table is used for generating address
of a symbol
93
Literal Table
• We directly specify the value, literal is used to give a location for the
value
• literal must start with an equal sign (=), which indicates
to the assembler that a literal follows
• Literals are always encountered in the operand field of an instruction
• literals define read-only data, they must not be used in
operands that represent the receiving field of an
instruction that modifies storage
• A literal both defines data and represents data
• The address of the literal is assembled into the object
code of the instruction in which it is used
• In pass 1, whenever a Literal is defined an entry is made in Literal
table
• In pass2, Literal table is used for generating address of a Literal

94
Assignment 1

• Generate Symbol table, literal table & Intermediate code of


a two-pass Assembler for the given source code.
INPUT
START 200 LC=0
MOVER AREG X 200
ADD AREG =’3’ 201
MOVEM AREG Y 202
X DC 1 203
Y DS 1 204
END 205
95
• START: This instruction starts the execution of program from location 200.
INPUT
• MOVER: It moves the content of symbol into register AREG. 1START 200
2 MOVER AREG X 200
3 ADD AREG =’3’ 201
4 MOVEM AREG Y 202
5 X DC 1 203
• ADD: It adds the contents of X(which is stored in AREG )with Y. 6 Y DS 1 204
7 END 205
• MOVEM: It moves the content of register into memory operand Y.

• DS (Data Storage): It assigns a data space of 1 to Symbol X.

• DC (Define Constant) :It allows you to put a data value in memory at the time
that the program is first loaded

• END: It finishes the program execution

96
• Symbol Table INPUT
1 START 200
Symbol Address 2 MOVER AREG X 200
3 ADD AREG =’3’ 201
4 MOVEM AREG Y 202
• Literal TableLiteral Address
5 X DC 1
6 Y DS 1
203
204
7 END 205

• Step 1: START 200 (here no symbol or literal


is found
so both table would be empty)
• Step 2: MOVER AREG X 200
(here X is symbol so symbol table is made)
X is a symbol referred prior to its declaration
so it is stored in symbol
Symbol
X table with
Address
blank 97
• Step 3: ADD AREG =’3’ 201
(here =’3’ is literal so literal table is made)

Literal Address
=’3’

• Step 4: MOVEM AREG Y 202


Y is a symbol referred prior to its declaration so it is stored in symbol table
with blank address field.
INPUT
Symbol Address 1 START 200
X 2 MOVER AREG X 200
Y 3 ADD AREG =’3’ 201
4 MOVEM AREG Y 202
5 X DC 1 203
6 Y DS 1 204
7 END 205

98
• Step 5: X DC 1 203
• It is a data declaration statement i.e X is assigned
value of 1.
• But X is a symbol which was referred earlier in step 2
and defined in step 5
• This condition is called Forward Reference Problem
where variable is referred prior to its declaration
• This can be solved by back-patching
• So now assembler will assign X the address
specified by LC value of current step
INPUT
1 START 200
2 MOVER AREG X 200
3 ADD AREG =’3’ 201
4 MOVEM AREG Y 202
Symbol Address
5 X DC 1 203
X 203
6 Y DS 1 204
Y
7 END 205
99
• Step 6: Y DS 1 204
• It is a data declaration statement i.e Y is assigned data space of 1
• Y is a symbol which was referred earlier in step 4 and defined in step 6
• This condition is called Forward Reference Problem where variable is
referred prior to its declaration
• This can be solved by back-patching
• So now assembler will assign Y the address specified by LC value of
current step

INPUT
Symbol Address
X 203 1 START 200
Y 204 2 MOVER AREG X 200
3 ADD AREG =’3’ 201
4 MOVEM AREG Y 202
5 X DC 1 203
6 Y DS 1 204
7 END 205

100
• Step 7: END 205
• Program finishes execution and literal will
get address specified by LC value of END
instruction
• Here is the complete symbol and literal
table made by pass 1 of assembler
INPUT
1 START 200
2 MOVER AREG X 200
3 ADD AREG =’3’ 201
4 MOVEM AREG Y 202
5 X DC 1 203
Symbol Address 6 Y DS 1 204
X 203 7 END 205
Y 204

Literal Address
=’3’ 205
101
• Input Intermediate Code
• START 200 (AD,01) (C,200)
• MOVER AREG X 200 (IS, 01) AREG X
• ADD AREG =’3’ 201 (IS, 02) AREG (L,01)

• MOVEM AREG Y 202 (IS, 03) AREG Y


• X DC 1 203 (DL, 01) (C,01)
• Y DS 1 204 (DL, 02) (C,01)
• END 205 (AD, 02)

102
Generation of Symbol Table

• Generate Symbol table of a two-pass Assembler for the given


source code.
INPUT
START 200 LC=200
MOVER AREG X
ADD AREG Y
MOVEM AREG Z
X DC 1
Y DC 2
Z DS 1
END

103
Sample Example
– START 200
• LOAD MOVER AREG, A
• MOVER BREG, =‘2’
• ADD AREG, =‘2
• SUB BREG, =‘3’
• LTORG ------------------------------------
• MOVER AREG, =‘4
• ADD BREG, =‘2
• A DC 5---------------------------------------------
• END-----------------------------------------------)

104
Sample Example

– START 200
• LOAD MOVER AREG, A ----------------200
• MOVER BREG, =‘2’ --------------------201
• ADD AREG, =‘2’-----------------------202
• SUB BREG, =‘3’ -------------------------203
• LTORG ------------------------------------204 (Add of =‘2’)
• -------------------------------------------205 (Add of =‘3’)
• MOVER AREG, =‘4’------------------------206
• ADD BREG, =‘2’-------------------------207
• A DC 5----------------------------------------------208
• END-----------------------------------------------209 (Add of =‘4)
• ----------------------------------------------210(add of =‘2)
105
Symbol Table

Symbol Name Address


Load 200
A 208

Literal Table
Literal No. Literal Name Address
1 ‘=2’ 204
2 ‘=3’ 205
3 ‘=4’ 209
4 ‘=2’ 210

Pool Table
#1
#3

106
Sample Example
– START 200
• LOAD MOVER AREG, A
• ADD AREG, =‘2’
• MOVEM AREG, B
• LTORG ------------------------------------
• MOVER AREG, =‘4
• ADD AREG, =‘2
• A DC 5---------------------------------------------
• B DS 4
• END-----------------------------------------------)

107
Symbol Table

Symbol Name Address

Literal Table

Literal No Literal Name Address

108
Sample Example
• START 500
MOVER AREG,=‘7’
• MOVER BREG,X
• L1 MOVER BREG, =‘1’
ORIGIN L1+2
NEXT ADD AREG, =‘2’
X DS 1
END
109
Symbol Table

Symbol Name Address

Literal Table

Literal No Literal Name Address

110
Sample Example
• START 200
MOVER AREG,=‘7’
• MOVER BREG,X
• L1 MOVER BREG, =‘1’ ORIGIN
L1+3
LTORG
NEXT ADD AREG, =‘2’
X DS 1
END
111
Sample Example
• START 200 LC
MOVER AREG,=‘7’ 200
MOVER BREG,X 201
L1 MOVER BREG, =‘1’ 202
ORIGIN L1+3
LTORG 205 add of ‘=7’
206 add of ‘=1’
NEXT ADD AREG, =‘2’ 207
X DS 1 208
END 209 add of ‘=2’
112
Literal Address
‘=7’ 205
‘=1’ 206
‘=2’ 209

Pool table
#1
#3

Symbol Address
X 208
L1 202
Next 207
113
Sample Example
• START 200 LC
MOVER AREG,=‘7’ 200
MOVER BREG,X 201
L1 MOVER BREG, =‘1’ 202
ORIGIN L1+3 203
LTORG 205……ADD OF =‘7’
206……ADD OF =‘1’
NEXT ADD AREG, =‘2’ 207
X DS 1 208
END 209…..ADD OF =‘2’
114
Sample Example
INPUT
START 500
MOVER AREG X 500
ADD AREG =’3’ 501
MOVEM AREG Y 502
X DS 4 503 add of x
Y DC 2 507 add of y
END 508 add of ‘=3’
115
Symbol Address
X 503
Y 507

Literal Address
‘=3’ 508

Pool Table
#1

116
Sample Example
• START 500 LC
MOVER BREG, =‘6’
ADD BREG, X
LOOP MOVER AREG, =‘2
ORIGIN LOOP+4
N1 ADD AREG, =‘4’
SUB BREG, =‘3’
LTORG
NEXT EQU LOOP
MULT AREG, =‘3’
STOP
X DS 1
END
117
Sample Example
• START 500 LC
MOVER BREG, =‘6’ 500
ADD BREG, X 501
LOOP MOVER AREG, =‘2’ 502
ORIGIN LOOP+4 503
N1 ADD AREG, =‘4’ 506
SUB BREG, =‘3’ 507
LTORG 508 Add of ‘=6’
509 Add of ‘=2’
510 Add of ‘=4’
511 Add of ‘=3’

NEXT EQU LOOP 512


MULT AREG, =‘3’ 512
STOP 513
X DS 1 514
END 515

118
Symbol Address
X 514
LOOP 502
N1 506
NEXT 502

Literal Address
‘=6’ 508
‘=2’ 509
‘=4’ 510
‘=3’ 511
‘=3’ 515

Pool table
#1
#5

119
Algorithm : First pass of two pass assembler -1

1. loc-cntr := 0; (default value)


pooltab-ptr := 1;
POOLTAB [1]:=1;
littab-ptr := 1;

2. While next statement is not an END statement


(a) If label is present then
this-label := symbol in label field;
Enter (this-label, loc-cntr) in SYMTAB.
120
Algorithm : First pass of two pass assembler -2

(b) If an LTORG statement then


(i) Process literals LITTAB [POOLTAB [pooltab-
ptr]]... LITTAB [lit- tab-ptr – 1] to allocate
memory and put the address in the address field.
Update loc-cntr accordingly.
(ii) pooltab-ptr := pooltab-ptr + 1;
(iii) POOLTAB [pooltab-ptr] := littab-ptr;
(c) If a START or ORIGIN statement then
loc-cntr := value specified in operand field;

121
Algorithm : First pass of two pass assembler - 3

(d) If an EQU statement then


(i) this-addr := value of <address spec>;
(ii) Correct the symtab entry for this-label to (this-label,
this-addr).

(e) If a declaration statement then


(i) code := code of the declaration statement;
(ii) size := size of memory area required by DC/DS.
(iii) loc-cntr := loc-cntr + size;
(iv) Generate 1C '(DL, code) …..'.
122
Algorithm : First pass of two pass assembler - 4

(f) If an imperative statement then


(i) code := machine opcode from OPTAB;
(ii) loc-cntr := loc-cntr + instruction length from OPTAB;
(iii) If operand is a literal then
this-literal := literal in operand field;
LITTAB [littab-ptr] := this-literal;
littab-ptr := littab-ptr + 1;
else (i.e. operand is a symbol)
this-entry := SYMTAB entry number of operand;
Generate 1C '(IS, code)(S, this-entry)’;

123
Algorithm : First pass of two pass assembler - 5

3. (Processing of END statement)


(a) Perform step 2(b).
(b) Generate 1C '(AD.02)'.
(c) Go to Pass II.

124
• Input Intermediate Code
• START 200 (AD,01) (C,200)
• MOVER AREG X 200 (IS, 01) AREG X
• ADD AREG =’3’ 201 (IS, 02) AREG (L,01)
• MOVEM AREG Y 202 (IS, 03) AREG Y
• X DC 1 203 (DL, 01) (C,01)
• Y DS 1 204 (DL, 02) (C,01)
• END 205 (AD, 02)

(AD,01) (C,200) ----- Machine Code


200 (IS, 01) AREG X 200 01 01 203
201 (IS, 02) AREG (L,01) 201 02 01 205
202 (IS, 03) AREG Y 202 03 01 204
203 (DL, 01) (C,01) 203 00 00 001
204 (DL, 02) (C,01) 204 00 00 000
205 (AD, 02) 205 00 00 003

125
– START 200 (
• LOAD MOVER AREG, A
• MOVER BREG, =‘2’ ---
• SUB BREG, =‘3’
• LTORG
• MOVER AREG, =‘4’-----
• ADD BREG, =‘2
• A DC 5
• END-------------------------

126
– START 200 (AD,01) (C, 200) M/c code------
• LOAD MOVER AREG, A 200 (IS, 02) AREG A 200 02 01 208
• MOVER BREG, =‘2’ ---201 (IS,02) BREG (L,01) 201 02 02 204
• ADD AREG, =‘2’----202 (IS,03), AREG, (L,01) 202 03 01 204
• SUB BREG, =‘3’ -----203 (IS,04) BREG (L,02) 203 04 02 205
• LTORG ------------------204 (AD,03) 204 00 00 002
• -----------------------205 205 00 00 003
• MOVER AREG, =‘4’-----206 (IS,02) AREG,(L,03) 206 02 01 209
206
• ADD BREG, =‘2’----207 (IS, 03) BREG, (L,04) 207 03 02 210
• A DC 5------------------------208 (DL,01) (C,05) 208 00 00 005
• END-------------------------209 (AD,02) 209 00 00 004
• -----------------------10 210 00 00 002

127
Algorithm : Second pass of two pass assembler - 1

1. code-area-address := address of code-area;


pooltab-ptr := 1;
loc-cntr := 0;
2. While next statement is not an END
statement
(a) Clear machine-code-buffer;

128
Algorithm : Second pass of two pass assembler -
2

(b) If an LTORG statement


(i) Process literals in LITTAB[POOLTAB[pooltab-ptr]} ...
LITTAB [POOLTAB [pooltab-ptr+l] ]—1 similar to
processing of constants in a DC statement, i.e.
assemble the literals in machine.code, buffer.
(ii) size := size of memory area required for literals;
(iii) pooltab-ptr := pooltab-ptr + 1;

129
Algorithm : Second pass of two pass assembler -
3

(c) If a START or ORIGIN statement then


(i) loc-cntr := value specified in operand field;
(ii) size := 0;
(d) If a declaration statement
(i) If a DC statement then
Assemble the constant in machine-code-buffer.
(ii) size := size of memory area required by DC/DS;

130
Algorithm : Second pass of two pass assembler -
4

(e) If an imperative statement


(i) Get operand address from SYMTAB or LITTAB.
(ii) Assemble instruction in machine-code-buffer.
(iii) size '.- size of instruction;
(f) If size != 0 then
(i) Move contents of machine-code-buffer to the
address code-area-address + loc-cntr;
(ii) loc-cntr := loc-cntr + size;
131
Algorithm : Second pass of two pass assembler -
5
3. (Processing of END statement)
(a) Perform steps 2(b) and 2(f).
(b) Write code-area into output file

132
State TRUE/FALSE
• Literal are processed in PASS II
• Undefined symbols are detected in Pass-I
• Incorrect opcodes are detected in pass-I
• Output of an assembler is an object program

133
QUIZ
• In a two pass assembler, adding literals to literal
table and address resolution of local symbols
are done using ?

• a.First pass and second respectively


• b.Both second pass
• c.Second pass and first respectively
• d.Both first pass

134
QUIZ
• In a two pass assembler the pseudo code EQU
is to be evaluated during ?

• a. Pass 1
• b.Pass 2
• c.not evaluated by the assembler
• d.None of above

135
QUIZ
• An assembler is
A. programming language dependent
b.syntax dependant
c. machine dependant
d.data dependant

136
QUIZ
• An imperative statement
A. Reserves areas of memory and associates
names with them
b.Indicates an action to be performed during
execution of assembled program
c. Indicates an action to be performed during
optimization
d.None of the above
137
QUIZ
• In a two-pass assembler, the task of the Pass II
is to
a. separate the symbol, mnemonic opcode and
operand fields.
b.build the symbol table.
c. construct intermediate code.
d.synthesize the target program.

138
QUIZ
• Assembler is a machine dependent, because
of?
a. Macro definition table(MDT)
b. Pseudo operation table(POT)
c. Argument list array(ALA)
d. Mnemonics operation table(MOT

139

You might also like