Module 2 Assemblers
Module 2 Assemblers
Construction
CSC 602
Subject Incharge
Varsha Shrivastava
Assistant Professor
email: varshashrivastava@sfit.ac.in
Room No: 407
1
CSC 602 System Programming and
Compiler Construction
Module 2
Assembler
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 2
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 Hypothetical / X86
family processor
Data structures used
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 3
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.
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 4
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 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 5
Elements of Assembly Language programming
ASSEMBLER
IMPERATIVE DECLARATIVE
DIRECTIVE
STATEMENTS STATEMENT
STATEMENTS
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 6
Elements of Assembly Language programming
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 7
Elements of Assembly Language programming
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 10
General Machine Architecture
IBM 360 Architecture
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 11
General Machine Architecture
IBM 360 Architecture
Instruction
interpreter
Location Counter
Working
Registers
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 15
General Machine Architecture
IBM 360 Architecture
MAR and
MBR
MAR and
MBR
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 20
IBM 360/370 Structure
• Registers
• 16 general purpose registers of 32 bits each
• Can be used for various arithmetic and logical
operations as base registers
• Can be used as temporary storage for
operations
• In base register form, they aid in formation of
address
• 4 floating point registers of 64 bits each
• 64 bit Program Status Word (Flags)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 21
IBM 360/370 Structure
• Data
• B’ 0000 0001 0010 1100
• X’ 0 1 2 C
• Prefix
• X – Hexadecimal
• B – Binary
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 22
IBM 360/370 Structure
Instructions
Register Operands
• RR format 1 2
OPCODE R1 R2
0 7 8 11 12 15
• RX format
Register Storage
Operand 1 Operand 2
OPCODE R1 X2 B2 D2
0 7 8 11 12 15 16 19 20 31
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 23
IBM 360/370 Structure
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 24
IBM 360/370 Structure
• Special Features
• Has hardware protection in blocks of 2048 bytes
• Has elaborate interrupt structure
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 25
Pseudo Operation Code (Pseudo-op)
“Pseudo Op can also be termed as Assembler
Directives”
START : START is a pseudo-op that tell the
assembler where the beginning of the program is
and allows the user to give a name to the
program.
Eg: TEST START
END : END is a pseudo-op tell the assembler that
the end of the program has been reached
Eg: END
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 26
Pseudo Operation Code (Pseudo-op)
USING : USING is a pseudo-op that indicates to
the assembler which general purpose register to
use as a base register and what its content will be.
Eg: USING *,15
USING BEGIN+2,15
DROP : DROP is a pseudo-op tell which indicates
unavailable base register.
Eg: DROP 15
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 27
Pseudo Operation Code (Pseudo-op)
DS : DS is a pseudo-op which make the assembler
reserve a space in memory. DS is only for define
storage.
Eg: BLOCK DS 5F
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 28
Pseudo Operation Code (Pseudo-op)
LTORG : LTORG is a pseudo-op which tells the
assembler to place the encountered literals at an
earlier locations.
Eg: BLOCK DS 5F
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 29
Machine Operation Code (Machine-op)
BALR : Branch and link- load the register with next
address and branch to the address in the second
field. (RR format instruction)
Eg: BALR 15,0
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 30
Machine Operation Code (Machine-op)
BCT : Branch on Count- RX Instruction
This command decrements the given register by
one and if the result is positive, branches to the
address in the second field
Eg: BCT 3, LOOP
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 31
Assembly language program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 32
Types of Assembler
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 33
Assembler Process
ASSEMBLY
LANGUAGE PASS1 PASS2 EXECUTABLE
PROGRAM IMAGE
SYMBOL
TABLE
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
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 34
Problem of Forward Reference
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 35
Example : Assembly Program
JOHN START 0
USING *, 15
L 1, FIVE
A 1,FOUR
ST 1, TEMP
FOUR DC F’4’
FIVE DC F’5’
TEMP DS 1F
END
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 36
Problem of Forward Reference
JOHN START 0
USING *, 15
L 1, FIVE
A 1,FOUR
ST 1, TEMP
FOUR DC F’4’
FIVE DC F’5’
TEMP DS 1F
END
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 37
Assembler
SOURCE PROGRAM FIRST PASS SECOND PASS
JOHN START 0
USING *, 15
L 1, FIVE
A 1,FOUR
ST 1, TEMP
FOUR DC F’4’
FIVE DC F’5’
TEMP DS 1F
END
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 41
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 42
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 43
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 44
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 45
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 46
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 47
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 48
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 49
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 50
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 51
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 52
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 53
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 54
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 55
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 56
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 57
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 58
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 59
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 60
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 61
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 62
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 63
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 64
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 65
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 66
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 67
Example : Assembly Program
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 68
Intermediate Generated Code
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction
Department of Computer Engineering Ms. Varsha Shrivastava 69
Points to remember :-
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 70
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Example
Statement 11 – L DATABASE,=A(DATA1)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 71
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Example
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 72
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Example
Statement 16 – A AC,=F’5’
Statement 16 – A 2, 46(0,15)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 73
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Example
Statement 17 – ST AC,SAVE(INDEX)
Statement 17 – ST 2, 58(3,15)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 74
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Example
Statement 18 – A 3, 50(0,15)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 75
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Example
Statement 19 – C 3, 54(0,15)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 76
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Example
Statement 20 – BC 7, 6(0,15)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 77
Department of Computer Engineering Ms. Varsha Shrivastava
Final Generated Code
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 78
Department of Computer Engineering Ms. Varsha Shrivastava
Practice :-Compute Pass 1 & Pass 2
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 79
Department of Computer Engineering Ms. Varsha Shrivastava
Solution :-
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 80
Department of Computer Engineering Ms. Varsha Shrivastava
Practice :-
For the following assembly language program Construct ST, LT, BT
and formulate the corresponding object code using these tables
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 81
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
1. Specify the problem
2. Specify data structures
3. Define format of data structures
4. Specify algorithm
5. Look for modularity [capability of one
program to be subdivided into
independent programming units.]
6. Repeat 1 through 5 on modules.
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 82
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
1. Specify the problem :
Pass1: Define symbols & literals.
1) Determine length of m/c instruction [MOTGET1]
2) Keep track of Location Counter [LC]
3) Remember values of symbols until pass2 [STSTO]
4) Process some pseudo ops, eg. EQU, DS etc [POTGET1]
5) Remember Literals [LITSTO]
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 83
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
1. Specify the problem :
Pass2: Generate object program
1) Look up value of symbols [STGET]
2) Generate instruction [MOTGET2]
3) Generate data (for DS, DC & literals)
4) Process pseudo ops [POTGET2]
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 84
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
2. Specify Data Structure:
Pass1: Databases
• Input source program
• “LC” location counter is used to keep track of each instruction’s
address.
• Machine operation table (MOT) [Symbolic mnemonic & length]
• Pseudo operation table [POT], [pseudo mnemonic & action]
• Symbol Table (ST) stores each table & it’s value.
• Literal Table (LT), to store each literal (variable) & it’s location.
• Copy of input to used later by PASS-2.
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 85
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
2. Specify Data Structure:
Pass2: Databases
• Copy of source program input to Pass1.
• Location Counter (LC)
• MOT [Mnemonic, length, binary m/c op code, etc.]
• POT [Mnemonic & action to be taken in Pass2
• ST [prepared by Pass1, label & value]
• Base Table [or register table] indicates which registers are
currently specified using ‘USING’ pseudo op & what are
contents.
• Literal table prepared by Pass1. [Lit name & value].
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 86
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
Use of Databases by Assembler Passes
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 87
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
3. Format of Data Structure:
• Machine Operation Table
• The op-code is the key and it’s value is the binary op
code equivalent, which is used for use in generating
machine code.
• The instruction length is stored for updating the location
counter.
• Instruction format is use in forming the m/c language
equivalent
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 88
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
3. Format of Data Structure:
• Machine Operation Table (MOT)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 89
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
3. Format of Data Structure:
• Pseudo Operation Table (POT)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 90
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
3. Format of Data Structure:
• Symbol Table (ST) and Literal Table (LT)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 91
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
3. Format of Data Structure:
• Base Table (BT)
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 92
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
What goes where?
• ST (Symbol Table) – Labels & their values
• LT (Literal Table) – Data (F’10’, F’49’, F’4’…)
• MOT (Machine Op Table) – Machine opcodes, i.e.,
instructions along with their length (2/4/6 bytes) in
Pass1.
• In Pass2, we store the name, length, binary code and
format (RR, RX, SS…).
• POT (Pseudo Op Table) – Pseudo opcodes or action to be
taken in Pass1 and Pass2
• BT (Base Table) – the registers used for base addresses
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 93
Department of Computer Engineering Ms. Varsha Shrivastava
Problems with single pass assembly?
• Forward Reference
• Omits the operand address if the symbol has not yet
been defined
• Enters this undefined symbol into SYMTAB and indicates
that it is undefined
• Adds the address of this operand address to a list of
forward references associated with the SYMTAB entry
• When the definition for the symbol is encountered,
scans the reference list and inserts the address.
• At the end of the program, reports the error if there are
still SYMTAB entries indicated undefined symbols.
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 94
Department of Computer Engineering Ms. Varsha Shrivastava
Pass 1-
Assembler
Flow chart
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 95
Department of Computer Engineering Ms. Varsha Shrivastava
Pass 2-
Assembler
Flow chart
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 96
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler Design
General Design Procedure of Assembler
5. Look for Modularity :-
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 97
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 98
Department of Computer Engineering Ms. Varsha Shrivastava
Assembler
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 99
Department of Computer Engineering Ms. Varsha Shrivastava
University Questions
State the reason for the assembler to be a multi pass
program
What is forward reference problem? How is it handled in
two pass assembler? Explain with the help of flowchart the
working of a two pass assembler.
Explain the databases used by each pass of the 2-pass
assembler in detail.
With reference to assembler explain the following tables
with suitable examples:
(a) POT (b) MOT (c) ST (d) LT
Explain the working of single pass assembler. Show the
structure of the database used
Practice Assembler programs
St. Francis Institute of Technology 08 Feb 2021 CSC 602:System Programming & Compiler Construction 10
Department of Computer Engineering Ms. Varsha Shrivastava 0
THE END!