You are on page 1of 50

The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes.

Distribution and modifications of the content is prohibited.

System Programming and Compiler


Construction
CSC 601

Subject Incharge
Varsha Shrivastava
Assistant Professor
email: varshashrivastava@sfit.ac.in
Room No: 407

1
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

CSC 601 System Programming and


Compiler Construction
Module 2
Assembler

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 2
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Content as per syllabus

 Introduction
 Elements of Assembly Language programming,
Assembly scheme
 Pass structure of assembler
 Assembler Design:
 Two pass assembler Design and
 Single pass Assembler Design for X86 family processor
 Data structures used

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 3
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Introduction
• Assembler is system software which converts an assembly
language
• The input to the assembler is a source code written in assembly
language (using mnemonics) and the output is an object code.

• Basic Assembler functions:


• Translating mnemonic language to its equivalent object code.

• Assigning machine addresses to symbolic labels.

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 4
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Elements of Assembly Language programming

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 5
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Elements of Assembly Language programming

Assembler languages-structure
<Label> <Mnemomic> <Operands> Comments
• Label
• symbolic labeling of an assembler address (command
address at Machine level)
• Mnemomic
• Symbolic description of an operation
• Operands
• Contains of variables or address if necessary
• Comments
• ignored by assembler
• used by humans to document/understand programs

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 6
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Statement Format

AREA refers to the memory word with which the name AREA is associated
AREA+5 refers to the memory word 5 words away from the word with the name
AREA, Here 5 is the displacement or offset from AREA
AREA(4) refers indexing with index register 4, i.e. operand address is obtained by
adding the content of index register 4 to the address of the AREA
St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 7
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Mnemonic Operation Codes

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 8
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Mnemonic Operation Codes

Mnemonic Numeric Description


Op code Op-code
STOP 00 It stops the execution.
ADD 01 It adds the content of operand2 into specified register.
SUB 02 It subtracts the contents of operand2 from the specified register.
MUL 03 It multiplies the content of operand2 into specified register.
MOVER 04 It moves the content of operand2 into specified register
MOVEM 05 It moves the content of Register into operand2 (memory)
COMP 06 Compare content of register with the content of operand2 and set
suitable condition code
BC 07 It branches on condition
DIV 08 It divides the contents of register by the operand2
READ 09 It reads the input and stores it into operand2
PRINT 10 Prints the content of operand2

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 9
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Mnemonic Operation Codes

If the Op-code is BC then operand 1 is one of the condition code,


otherwise one of the register from the table given below and operand 2
refers to the memory location

Register Description Condition Description


Codes
AREG A Register
BREG B Register LT Less Than

CREG C Register LE Less than or equal to

DREG D Register EQ Equal to


GT Greater than
GE Greater than or equal to
ANY Any

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 10
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Mnemonic Operation Codes

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 11
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Machine Instruction Format

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 12
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Example:-

ALP and its equivalent Machine Language Program

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 13
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Elements of Assembly Language programming

Types of Assembly Language


Statements
ASSEMBLY
LANGUAGE
STATEMENTS

ASSEMBLER
IMPERATIVE DECLARATIVE
DIRECTIVE
STATEMENTS STATEMENT
STATEMENTS

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 14
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Elements of Assembly Language programming

Types of Assembly Language


Statements
Imperative statements
• An imperative statement in assembly
language indicates the action to be
performed during execution of assembly
statement
Ex:- SUB BREG,X
(Subtract the content of X from the content of BREG)

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 15
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Elements of Assembly Language programming

Types of Assembly Language Statements


Declarative Statement
• These statements is used to declare variables and constants.
There are two types of Declarative statement:-
1. Declare Storage
2. Declare Constant
Declare Storage declares the storage area or declares the
constant in program. The DS statement reserves areas of
memory and associates names with them.
Syntax:- <Symbol> DS <constant>
Eg:- ONE DS 1
The 1 word memory location is reserved and it associate name
ONE
St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 16
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Elements of Assembly Language programming

Types of Assembly Language Statements


Declarative Statement
• These statements is used to declare variables and constants.
There are two types of Declarative statement:-
1. Declare Storage
2. Declare Constant
Declare Constant The DC statement is used to construct
memory words containing constants.
Syntax:- <Symbol> DC ‘<value>’
Eg. ONE DC 1
The memory location containing the value 1 and is associates
name ONE.
Note :- Its just initializes the memory word to given values. These values may be changed
during the program execution, which means constant values are not secured by assembler.
Instead of constant ,literals can be used.
St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 17
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Elements of Assembly Language programming

Types of Assembly Language Statements


Use of Constants

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 18
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Elements of Assembly Language programming

Types of Assembly Language Statements


Use of Constants

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 19
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Elements of Assembly Language programming

Types of Assembly Language Statements


Assembler Directives
• These statements instructs the assembler to
perform certain action during the assembly of a
program.
• START Syntax :- START <constant>
Eg:- START 100
This directive indicates that the first word generated by the assembler is
stored in memory location specified by <constant>
• END  Syntax :- END
• This directive specifies the end of the source of the program

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 20
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

A Simple Assembly Scheme

There are 2 phases in specifying an Assembler:-

1.Synthesis Phase (the fundamental


information requirement will arise in this
phase
2.Analysis Phase

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 21
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

A Simple Assembly Scheme

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 22
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

A Simple Assembly Scheme- Synthesis Phase Eg.

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 23
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Data Structure used in Synthesis Phase

1. Symbol Table
2. Mnemonics Table

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Analysis Phase

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Analysis Phase: Implementing Memory Allocation

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Example

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Example

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Example

Data Structures of an Assembler During Analysis and Synthesis


Phase

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Data Structures

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Task Performed : Analysis Phase

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Task Performed : Synthesis Phase

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Assembler :Design

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Assembler :Design

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Difficulties : Forward Reference

Forward Reference Problem


When a symbol is used before it is defined, such a reference is
called as Forward Reference. Since the symbol has not yet been
defined, its address is not known. So, the Assembler cannot
assemble the entire instruction at once. This is called as Forward
Reference Problem (FRP).

It can be solved using back patching method


[NOTE: These tables and algorithms in this book are for a hypothetical machine.
So some major changes might be required, for actual implementation of
Assembler (or even simulation of these algorithms)]

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
Solution : Forward Reference problem
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

(FRP)
FRP can be solved in following 2 ways:
Multi-pass Approach
Single-Pass approach

In a Multi-pass approach, assembler assembles the


mnemonics and constructs ST in pass 1. In the Pass 2,
Input file is read again along with ST and an assembled
object code file is generated in the output. This is a 2-Pass
Approach, but some complex programs may need more
than just 2 passes for completing the assembly of the
program. Such an approach is called Multi-pass Approach.
St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
Solution : Forward Reference problem
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

(FRP)
FRP can be solved in following 2 ways:
Multi-pass Approach
Single-Pass approach

In a Single-pass approach, the Assembler makes use of a


special data structure called Forward-Reference Table
(FRT) to keep track of only the forward-referenced
symbols. At the end of pass 1, this new data structure is
used to update definition addresses of forward-
referenced symbols in ST and in the program.

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Backpatching

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Backpatching

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Assembler Design

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Assembler Design

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Assembler Design

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Two-Pass Assembler

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Data Structures in Pass-1

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Data Structures in Pass-1

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

OPTAB (Operation-Code table)

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

Flow Chart of Pass-1 of Two pass Assembler


Initialize

Read next
instruction

EN
END Process Literals Go to Pass 2
D yes

No

Instruction
END
has a label
Insert symbol (label) in the symbol table (ST)
yes

No
Type of instructionType
IS START ORIGIN DS/DC LTORG

Get Instruction Change Update Process current pool of


length Location address in ST literals
counter
Find Storage space

Generate Intermediate Code

Modify Location counter


The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

University Questions
• What are Assembler Directives? Explain with examples (May ’06 [Comps] – 6M,
Dec ’08 [Comps] – 5M)
• State the reason for assembler to be a multi-pass program. (May ’05 [Comps] – 4M,
May ’04 [IT] – 4M, Dec ’06 [Comps] – 4M)
• Explain Forward Reference Problem in Assembler. (May ’05 [IT] – 4M, Dec ’05 [IT] –
4M)
• What is Assembly Language? What feature of assembly language required us to
build a two-pass assembler? (Dec ’05 [Comps] – 10M)
• What is Forward Reference Problem? How is it handled in a two-pass assembler?
Explain with the help of database. (June ’07 [IT] – 10M)
(Hint: For this question, explain FRP and also explain how ST is used by a two-pass
assembler. Give a sample ALP program, construct its ST stating that it is the output of
pass 1 and use it to assemble the program in Pass 2. Give final output file only, all the
tables not needed)
• Explain working of each of the two passes of a two-pass assembler, with the help
of databases. (Dec ’04 [Comps] – 10M, Dec ’05 [IT] – 10M)

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
The material in this presentation belongs to St. Francis Institute of Technology and is solely for educational purposes. Distribution and modifications of the content is prohibited.

University Questions

• Explain with neat flowchart and database working of two-pass


assembler. (Dec ’04 [IT] – 12M, Dec ’07 [IT] – 10M, June ’08 [Comps] –
10M, Dec ’08 [Comps] – 10M, June ’08 [IT] – 10M) [Often asked as a
compulsory question]
• Explain with the help of flowchart and data structures, the working of a
single-pass assembler. (May ’04 [Comps] – 10M, May ’05 [Comps] – 10M,
May ’05 [IT] – 10M, May ’06 [IT] – 10M, Dec ’07 [IT] – 10M, May ’07
[Comps] – 10M).
• What are the different types of ALP statements?
• What is forward-reference problem?

St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava
THE END!

Have a nice day!


St. Francis Institute of Technology 12 Jan 2022 CSC 601:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 50

You might also like