0% found this document useful (0 votes)
17 views

Chapter 3 FM

This document discusses system programming and its components. It defines system programming as designing programs that allow hardware and software to interface effectively. Typical system programs include operating systems, firmware, compilers, loaders, and runtime libraries. The document then describes various components of system software like text editors, loaders, assemblers, compilers, debuggers, and machine structure. It provides details on each component and their functions.

Uploaded by

avsek540
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chapter 3 FM

This document discusses system programming and its components. It defines system programming as designing programs that allow hardware and software to interface effectively. Typical system programs include operating systems, firmware, compilers, loaders, and runtime libraries. The document then describes various components of system software like text editors, loaders, assemblers, compilers, debuggers, and machine structure. It provides details on each component and their functions.

Uploaded by

avsek540
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 86

Chapter 3

Department of Computer Science, AMU


Dr. Faraz Masood
System programming

Project analysis slide 2


System programming involves designing and writing computer programs that allow the computer hardware to interface
with the programmer and the user, leading to the effective execution of application software on the computer system.

Typical system programs include:

• Operating system
• Firmware,
• Programming tools such as compilers, assemblers,
• I/O routines
• Interpreters
• Scheduler
• Loaders
• Linkers
• Runtime libraries of the computer programming languages.
Component of
System Software
Project analysis slide 2
If we think of the computer system as a layered model, the system software is the interface between the hardware and user
applications

Components are :

• Text editors
• Loaders
• Assemblers
• Macro processors
• Compilers
• Debuggers
• Machine Structure
• Machine language and Assembly Language.
Component of
System Software
Project analysis slide 2
i. Text Editor :

A text editor is a type of program used for editing plain text files. Such programs are sometimes known as "notepad"
software, following the Microsoft Notepad.

ii. Loaders :

• A loader is the part of an operating system that is responsible for loading programs and libraries.
• It is one of the essential stages in the process of starting a program, as it places programs into memory and prepares
them for execution.
• Loading a program involves reading the contents of the executable file containing the program instructions into
memory, and then
• carrying out other required preparatory tasks to prepare the executable for running.
• Once loading is complete, the operating system starts the program by passing control to the loaded program code.
Component of
System Software
Project analysis slide 2
iii. Linkers :
Linker is a program that takes one or more objects generated by a compiler and combines them into a
single executable program.

iv. Assembler :
Assembler is a computer program which is used to translate program written in Assembly Language in to
machine language.
The translated program is called as object program.
Assembler checks each instruction for its correctness and generates diagnostic messages, if there are mistakes
in the program.

Various steps of assembling are:


• Input source program in Assembly Language through an input device.
• Use Assembler to produce object program in machine language.
• Execute the program.
Component of
System Software
Project
Compiler :
analysis slide 2
• A compiler is a program that translates a program written in HLL to executable machine language.
• The process of transferring HLL source program in to object code is a lengthy and complex process as compared to
assembling.
• Compliers have diagnostic capabilities and prompt the programmer with appropriate error message while compiling
a HLL program.
• The corrections are to be incorporated in the program, whenever needed, and the program has to be recompiled.
• The process is repeated until the program is mistake free and translated to an object code.
• Thus the job of a complier includes the following:
• To translate HLL source program to machine codes.
• To trace variables in the program.
• To include linkage for subroutines.
• To allocate memory for storage of program and variables.
• To generate error messages, if there are errors in the program.
Component of
System Software
Project
Interpreter :
analysis slide 2
• The basic purpose of interpreter is same as that of complier.
• In compiler, the program is translated completely and directly executable version is generated.
• Whereas interpreter translates each instruction, executes it and then the next instruction is translated and this goes on until
end of the program.
• In this case, object code is not stored and reused. Every time the program is executed, the interpreter translates each
instruction freshly.
• It also has program diagnostic capabilities. However, it has some disadvantages as below:
1. Instructions repeated in program must be translated each time they are executed.
2. Because the source program is translated fresh every time it is used, it is slow process or execution takes more time.
Approx. 20 times slower than complier.
Component of
System Software
Project analysis slide 2
Macro Processor :

• A macro processor is a program that copies a stream of text from one place to another, making a systematic set of
replacements as it does so.
• Macro processors are often embedded in other programs, such as assemblers and compilers.
• Sometimes they are standalone programs that can be used to process any kind of text.

Debugger :

• A debugger is a computer program used by programmers to test and debug a target program.

• Debuggers may use instruction-set simulators, rather than running a program directly on the processor to achieve a

higher level of control over its execution.


Component of
System Software
Project analysis slide 2
Machine structure

The above structure consist of..

1. Instruction interpreter
2. Location counter
3. Instruction register
4. Working register
5. General register
Component of
System Software
Project analysis slide 2
Machine structure

• The Instruction Interpreter Hardware is basically a group of circuits that perform the operation specified by the
instructions fetched from the memory.

• The Location Counter can also be called as Program/Instruction Counter simply points to the current instruction being
executed.

• The working registers are often called as the "scratch pads" because they are used to store temporary values while
calculation is in progress.

• This CPU interfaces with Memory through MAR & MBR:


• MAR (Memory Address Register) – contains address of memory location (to be read from or stored into)
• MBR (Memory Buffer Register) – contains copy of address specified by MAR Memory controller is used to transfer data
between MBR & the memory location specified by MAR
• The role of I/O Channels is to input or output information from memory.
What is a
Semantic Gap?
Project analysis slide 2
• A semantic gap is a gap between the application domain and the
execution domain.

• Here application domain represents the program written in a high-


level language.

• However, the execution domain represents the program written in


machine language.

• Simply, the semantic gap bridges a gap between a user and a


computer.
What is a
Semantic Gap?
Project analysis slide 2
The semantic gap is further classified into two:

Specification Gap
• It is the gap between the application domain and the programming
language domain.
• This gap describes the gap between two different specifications describing
the same task.
• However, the semantic gap has to be bridged by the software designer.

Execution Gap
• It is a gap between the programming language domain and the execution
domain.
• This gap is between the two programs written in different programming
languages.
• Where one of the programs is in the languages closer to machine language.
• The language processor, such as a compiler or interpreter, bridges the
execution gap.
What is a
Semantic Gap?
Project analysis slide 2
Different types of domains between which these gaps exist.

•Application Domain – The specification language here is just a language convenient to the user.

•Programming Language Domain – The specification language here is any programming language.

•Execution Domain – The specification language here is the machine language.


Problem Oriented Language

Specification Gap Execution Gap

Application PL Execution
Domain Domain Domain

11/06/2023 14
Procedure Oriented Language

Specification Gap Execution Gap

PL Execution
Application
Domain Domain
Domain

11/06/2023 15
Language Processor

Project analysis slide 2


A language processor is a software tool or a set of programs that translates or processes human-readable
programming languages into a form that a computer can understand and execute.

1.Translation Bridge: Language processors act as a bridge between human-readable programming languages and
machine-executable code, enabling effective communication between programmers and computers.

2.Error Identification: They are equipped with error-checking mechanisms that identify and report errors in the
source code, helping programmers correct their mistakes and produce reliable software.

3.Multiple Types: Language processors come in various types, including compilers, interpreters, and assemblers,
each with a specific role in converting high-level code into machine code or a close variant.

4.Semantic Gap Reduction: They reduce the semantic gap between user intent (expressed in high-level languages)
and computer execution (in machine language), ensuring that the computer correctly interprets the programmer's
instructions.

5.Fundamental Phases: Language processors typically involve two fundamental phases: analysis, where source
code is examined for validity, and synthesis, where valid source code is transformed into target language code
while preserving meaning.
Levels of Languages

Project analysis slide 2


Machine language

Project analysis slide 2


• Sometimes referred to as machine code or object code, machine language is a collection of binary digits or bits that
the computer reads and interprets.
• Machine language is the only language a computer is capable of understanding.
• The exact machine language for a program or action can differ by operating system on the computer.
• The specific operating system will dictate how a compiler writes a program or action into machine language.
• Computer programs are written in one or more programming languages, like C++, Java, or Visual Basic.
• A computer cannot directly understand the programming languages used to create computer programs, so the program
code must be compiled.
• Once a program's code is compiled, the computer can understand it because the program's code has been turned into
machine language.
Machine language

Project analysis
Machine language example: slide 2
Below is an example of machine language (binary) for the text "Hello World".
01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010
01101100 01100100

Below is another example of machine language (non-binary), which will print the letter "A" 1000 times to the computer
screen.

169 1 160 0 153 0 128 153 0 129 153 130 153 0 131 200 208 241 96
Assembly Language

Project analysis
• Each personal slide 2that manages the computer's arithmetical, logical, and control activities.
computer has a microprocessor
• Each family of processors has its own set of instructions for handling various operations such as getting input from
keyboard, displaying information on screen and performing various other jobs.
• These set of instructions are called 'machine language instructions'.
• A processor understands only machine language instructions, which are strings of 1's and 0’s.
• However, machine language is too obscure and complex for using in software development.
• So, the low-level assembly language is designed for a specific family of processors that represents various instructions
in symbolic code and a more understandable form.

Advantages of Assembly Language :


• It requires less memory and execution time;
• It allows hardware-specific complex jobs in an easier way;
• It is suitable for time-critical jobs;

• It is most suitable for writing interrupt service routines and other memory resident programs.
Language Translators

Project analysis slide 2


Source Code Translator Object Code

A Language Translator is the program which converts a users program written in some language to
another language.
• The language in which the users program is written is called the source language.
• The language to which the source language is converted is called the target language.

Why Language Translators ?

Computer only understands object code (machine code). It does not


understand any source code.
The Programmer writes the source code and then translator converts it
in machine readable format (object code).
Language Translators

Project analysis slide 2

Language
Translators

Assembler Compiler Interpreter


Language Translators

Project
Assembler :analysis slide 2
Language
Translators
An assembler is a program that takes basic computer
instructions and converts them into a pattern of bits that
Assembler Compiler Interpreter
the computer's processor can use to perform its basic
operations

Assembly Code Assembler Object Code

Characteristics of Assembler :

• Assembly language programming is difficult


• Takes longer time to write code
• Takes longer to debug Difficult to maintain
Language Translators

Project
Compiler : analysis slide 2
Language
Translators

Complier is a system program that translates an input


Assembler Compiler Interpreter
program in a high level language into its machine
language equivalent

Target
Source Code
Compiler Program/Object/Exe
(HLL)
Cpde

HLL Features :

High degree of machine independence Good data structures


Improved debugging capability Good documentation
Language Translators

Project
Interpreter analysis
: slide 2 Language
Translators

An interpreter is a program that directly executes the


Assembler Compiler Interpreter
instructions in a high-level language, without converting it
into machine code.

Interpreter Features :

Error correction is quite easy for the interpreter as the interpreter provides a line-by-
line error.
Source programs are compiled before time and stored as machine-independent code,
which is then linked at run-time and executed by an interpreter.
Assemblers

Project analysis slide 2


Need of assembly language programming

1. Performance issues

For some applications, speed and size of the code are critical.
An expert assembly language programmer can often produce code that is much smaller and much faster
than a high level programmer can.
Example – embedded applications such as the code on a smart card, the code in a cellular telephone,
BIOS routines, inner loops of performance critical applications etc.
2. Access to the machine

Some procedures need complete access to the hardware, something which is impossible in high level
languages.
Example – Low levels interrupts and traps handlers in an operating system etc.
* The trap is a signal raised by a user program instructing the operating system to perform some functionality immediately. In contrast, the interrupt is a signal to the CPU
emitted by hardware that indicates an event that requires immediate attention.
Assemblers

Project analysis slide 2


Loader : It is the part of the operating system (a system software) that is responsible for loading programs from
secondary storage devices into memory, preparing them for execution and then executing them.

Linker :
A large program can be splitted up into many modules. All these modules have to be connected logically
and linked to form a single program.
Assemblers

Project analysis slide 2


Role of Loader and Linker
Assemblers

Project analysis slide 2


https://defuse.ca/online-x86-assembler.htm#disassembly

ADD ECX, R9D ;


ADD ECX, R8D ;
ADD ECX, EDX ;
MOVD XMM0, ECX;
Assemblers
Elements of Assembly Language
Project analysis slide 2
An assembly language programming provides three basic features which simplify programming when
compared to machine language.
1. Mnemonic Operation Codes :

Mnemonic operation code / Mnemonic Opcodes for machine instruction eliminates the need to
memorize numeric operation codes. It enables assembler to provide helpful error diagnostics. Such as
indication of misspelt operation codes.
2. Symbolic Operands :

Symbolic names can be associated with data or instructions. These symbolic names can be used
as operands in assembly statements. The assembler performs memory binding to these names; the
programmer need not know any details of the memory bindings performed by the assembler.
3. Data declarations :
Data can be declared in a variety of notations, including the decimal notation. This avoids manual conversion
of constants into their internal machine representation.
Assemblers

Project analysis slide 2


Statement format :

An assembly language statement has the following format :


[ Label] <Opcode> <operand Spec> [, operand Spec> ..]
https://www.sim8085.com/

1.LXI H, 2501H : "Get address of first number in H-L pair. Now H-L points to 2501H"
2.MOV A, M : "Get first operand in accumulator"
3.INX H : "Increment content of H-L pair. Now, H-L points 2502H"
4.ADD M : "Add first and second operand"
5.INX H : "H-L points 4002H"
6.MOV M, A : "Store result at 2503H"
7.HLT : "Stop"
Assemblers

Project analysis slide 2


Statement format :

An assembly language statement has the following format :


[ Label] <Opcode> <operand Spec> [, operand Spec> ..]
https://www.sim8085.com/

1.LXI H, 2501H : "Get address of first number in H-L pair. Now H-L points to 2501H"
2.MOV A, M : "Get first operand in accumulator"
3.INX H : "Increment content of H-L pair. Now, H-L points 2502H"
4.SUB M : "Subtract first to second operand"
5.INX H : "H-L points 4002H"
6.MOV M, A : "Store result at 2503H"
7.HLT : "Stop"
Assemblers

Project analysis slide 2


Statement format :

An assembly language statement has the following format :


[ Label] <Opcode> <operand Spec> [, operand Spec> ..]
https://www.sim8085.com/

1.LHLD 2501H : "Get 1st 16-bit number in H-L pair"


2.XCHG : "Save 1st 16-bit number in DE"
3.LHLD 2503H : "Get 2nd 16-bit number in H-L pair"
4.MOV A, E : "Get lower byte of the 1st number"
5.ADD L : "Add lower byte of the 2nd number"
6.MOV L, A : "Store result in L-register"
7.MOV A, D : "Get higher byte of the 1st number"
8.ADC H : "Add higher byte of the 2nd number with CARRY"
9.MOV H, A : "Store result in H-register"
10.SHLD 4004H : "Store 16-bit result in memory locations 2505H and 2506H"
11.HLT : "Stop"
Assemblers

Project analysis
Mnemonic Operation Codes :
slide 2

Instruction Format :
Assemblers

Project analysis
An Assembly slide
and equivalent machine 2 program :
language
Assemblers

Project analysis
An Assembly slide
and equivalent machine 2 program :
language
Assemblers

Project
Assemblyanalysis slide
Language Statements : 2 Assembly
language
Statements

Imperative Declaration Assembler


Statements Statements Directives

a) Imperative Statements : It indicates an action to be performed during the execution of the assembled
program. Each imperative statement typically translates into one machine instruction.
Eg. MOV, ADD, MULT
Assemblers

Project
Assemblyanalysis slide
Language Statements : 2 Assembly
language
Statements

Imperative Declaration Assembler


Statements Statements Directives

b) Declaration Statements : Two types of declaration statements is as follows

[Lable] DS <Constant>

[Label] DC <Value>
The DS (Declare Storage) statement reserves areas of memory and associates names with them.

E.g. A DS 1
B DS 150

First statement reserves a memory of 1 word and associates the name of the memory as A.
Second statement reserves a memory of 150 word and associates the name of the memory as B.
Assemblers

Project
Assemblyanalysis slide
Language Statements : 2 Assembly
language
Statements

Imperative Declaration Assembler


Statements Statements Directives

b) Declaration Statements : Two types of declaration statements is as follows

[Lable] DS <Constant>

[Label] DC <Value>
The DC (Declare Constant) Statement constructs memory word containing constants
e.g. ONE DC ‘1’
Associates the name ONE with a memory word containing the value ‘1’ . The
programmer can declare constants in decimal, binary, hexadecimal forms etc., These
values are not protected by the assembler. In the above assembly language program the
value of ONE Can be changed by executing an instruction
MOVEM BREG,ONE
Assemblers

Project
Assemblyanalysis slide
Language Statements : 2 Assembly
language
Statements

Imperative Declaration Assembler


Statements Statements Directives

c) Assembler Directives: Assembler directives instruct the assembler to perform certain actions during
the assembly of a program. Some Assembler directives are described in the following
START <Constant>

Indicates that the first word of the target program generated by the assembler
should be placed in the memory word with address <Constant>
END [ <operand spec>]
It Indicates the end of the source program
Assemblers

Project
Assemblyanalysis slide
Language Statements : 2 Assembly
language
Statements

Imperative Declaration Assembler


Statements Statements Directives

c) Assembler Directives: Assembler directives instruct the assembler to perform certain actions during
the assembly of a program. Some Assembler directives are described in the following
START <Constant>

Indicates that the first word of the target program generated by the assembler
should be placed in the memory word with address <Constant>
END [ <operand spec>]
It Indicates the end of the source program
Machine Dependent and Machine
Independent features of Assembler
• M/C Dependent Features
• A] Instruction format & addr. mode:-
• B] Program Relocation
• Machine Independent Assembler Features
• 1) Literals
• 2) Symbol defining statements
• 3) Expressions
Immediate Addressing Mode:
MVI B 45 (move the data 45H immediately to register B)
LXI H 3050 (load the H-L pair with the operand 3050H immediately)

Register Addressing Mode


MOV A, B (move the contents of register B to register A)
Assembler’s functions
• Convert mnemonic operation codes to their machine
language equivalents
• Convert symbolic operands to their equivalent machine
addresses
• Build the machine instructions in the proper format
• Convert the data constants to internal machine
representations
• Write the object program and the assembly listing

11/06/2023 43
Assembly Lang to M/C lang Program
1. Find address of variables and labels.
2. Replace Symbolic addr by numeric addr.
3. Replace Symbolic opcodes by machine opcode.
4. Reserve storage for data.
Opcode Register Memory
START 101 LC
READ X 101 operand
+ 09 0 108
READ Y 102 + 09 0 109
MOVER AREG, X 103 + 04 1 108
ADD AREG, Y 104 + 01 1 109
MOVEM AREG, RESULT 105 + 05 0 110
PRINT RESULT 106 + 10 0 110
STOP 107 + 00 0 000
X DS 1 108
Y DS 1 109
RESULT DS 1 110
END
Variable Address
Opcode Register Memory
X 108
START 101 LC
READ X 101 operand
+ 09 0 108 Y 109
READ Y 102 + 09 0 109
MOVER AREG, X 103 + 04 1 108 RESULT 110
ADD AREG, Y 104 + 01 1 109
MOVEM AREG, RESULT 105 + 05 0 110
PRINT RESULT 106 + 10 0 110
STOP 107 Figure: After LC Processing
+ 00 0 000
X DS 1 108
Y DS 1 109
RESULT DS 1 110
END
Required M/C Code
LC Opcode Register Address
101 09 0 108
102 09 0 109
START 101 LC Opcode Register
READ X 101 + 09 0 108 103 04 1 108
READ Y 102 + 09 0 109 104 01 1 109
MOVER AREG, X 103 + 04 1 108
ADD AREG, Y 104 105 05 0 110
+ 01 1 109
MOVEM AREG, RESULT 105 + 05 0 110 106 10 0 110
PRINT RESULT 106 + 10 0 110 107 00 0 000
STOP 107 + 00 0 000
X DS 1 108 Memory
108
operand
Y DS 1 109 109
RESULT DS 1 110 110
END
111
Analysis Phase
• Primary function of the Analysis phase is to build the symbol
table.
• It must determine the addresses with which the symbolic names
used in a program are associated
• It is possible to determine some addresses directly like the address
of first instruction in the program (ie.,start)
• Other addresses must be inferred
• To determine the addresses of the symbolic names we need to fix
the addresses of all program elements preceding it through
Memory Allocation.
• To implement memory allocation a data structure called
location counter is introduced.

11/06/2023 48
Synthesis Phase: Example
Consider the following statement:
MOVER BREG, ONE
The following info is needed to synthesize machine instruction
for this stmt:
1. Address of the memory word with which name ONE is
associated [depends on the source program, hence made
available by the Analysis phase].

2. Machine operation code corresponding to MOVER [does


not depend on the source program but depends on the
assembly language, hence synthesis phase can determine
this information for itself ]
Note: Based on above discussions , the two data structures
required during the synthesis phase are described next
11/06/2023 49
Data structures in synthesis phase
Symbol Table --built by the analysis phase
• The two primary fields are name and address of the symbol used to
specify a value.
Mnemonics Table --already present
- The two primary fields are mnemonic and opcode,
along with length.
Synthesis phase uses these tables to obtain
• The machine address with which a name is associated.
• The machine op code corresponding to a mnemonic.

• The tables have to be searched with the


• Symbol name and the mnemonic as keys

11/06/2023 50
Example
START 100
MOVER BREG, N LC = 100 (1 byte)
MULT BREG, N LC = 101 (1 byte)
STOP LC = 102 (1 byte)
N DS 5 LC = 103

Symbol Address

N 103

11/06/2023 51
• Since there the instructions take different amount of
memory, it is also stored in the mnemonic table in
the “length” field

Mnemonic Opcode Length


MOVER 04 1
MULT 03 1

11/06/2023 52
Data structures of an assembler
During analysis and
Synthesis phases Mnemonic Opcode length
ADD 01 1
SUB 02 1
Mnemonic Table

Source Synthesis Target


Analysis
--------------------------- Phase Program
Program Phase
------>

Symbol Address
 Data Access
N 104 -- > Control
Access
Symbol Table

11/06/2023 53
A simple Assembly Scheme
Design Specification of an assembler
There are four steps involved to design the specification of an
assembler:
• Identify information necessary to perform a task.
• Design a suitable data structure to record info.
• Determine processing necessary to obtain and maintain the
info.
• Determine processing necessary to perform the task

11/06/2023 54
Tasks Performed : Analysis Phase
• Isolate the labels, mnemonic, opcode and operand fields of a statement.

• If a label is present, enter (symbol, <LC>) into the symbol table.

• Check validity of the mnemonic opcode using mnemonics table.

• Update value of LC.

11/06/2023 55
Tasks Performed : Synthesis Phase

• Obtain machine opcode corresponding to the mnemonic from the mnemonic


table.

• obtain address of the memory operand from symbol table.

• Synthesize a machine instruction or machine form of a constant, depending on


the instruction.

11/06/2023 56
Forward Reference
• It is a reference to the entity which precedes its
definition in the program.
percent_profit := (profit * 100) / cost_price;
……..
……..
long profit;

11/06/2023 57
Difficulties: Forward Reference
• Forward reference: reference to a label that is defined later in the
program.

Loc Label Operator Operand

1000 FIRST STL RETADR


1003 CLOOP JSUB RDREC
… … … … …
1012 J CLOOP
… … … … …
1033 RETADR RESW 1

11/06/2023 58
Single Pass Translation
• The problem of forward reference can be handled using a
technique called as back patching.
START 100
MOVER AREG, X
L1 ADD BREG, ONE
ADD CREG, TEN
STOP
X DC ‘5’
ONE DC ‘1’
DC ‘10’
TEN END
START 100
MOVER AREG, X 100 04 1 ___
L1 ADD BREG, ONE 101 01 2 ___
ADD CREG, TEN 102 06 3 ___
STOP 103 00 0 000
X DC ‘5’ 104
ONE DC ‘1’ 105
TEN DC ‘10’ 106
END

Instruction Address Symbol Making a forward reference


100 X
101 ONE
102 TEN

Figure : Table of Incomplete Instructions


Machine Instruction After Backpatching

100 1 104
101 2 105
102 3 106
00 0 000
Backpatching
• The problem of forward references is handled using a
process called backpatching.
• Initially, the operand field of an instruction containing a forward
reference is left blank
• Ex: MOVER BREG, ONE can be only partially synthesized since
ONE is a forward reference
• The instruction opcode and address of BREG will be assembled
to reside in location 101
• To insert the second operand’s address later, an entry is added
as Table of Incomplete Instructions (TII)
• The entry TII is a pair (<instruction address>, <symbol>) which
is (101, ONE) here

11/06/2023 63
Backpatching
• When END statement is processed, the symbol table would
contain the addresses of all symbols defined in the source
program
• So TII would contain information of all forward references
• Now each entry in TII is processed to complete the instruction
• Ex: the entry (101, ONE) would be processed by obtaining the
address of ONE from symbol table and inserting it in the
operand field of the instruction with assembled address 101.
• Alternatively, when definition of some symbol L is encountered,
all forward references to L can be processed

11/06/2023 64
Two Pass Translation
• Handles forward references easily.
• Requires 2 scans of the source program.
• LC processing is performed in the 1st pass and symbols are
stored in the symbol table.
• Second pass synthesis Target Program.
Two Pass Assembler
• Read from input line
• LABEL, OPCODE, OPERAND

Source
program

Intermediate Object
Pass 1 Pass 2
file codes

OPTAB SYMTAB SYMTAB

11/06/2023 66
General Design Procedure of Two Pass
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.
Pass structure of assembler
Data Structures

Source Target
Program Pass I Pass II Program

Intermediate Code

Figure: Overview of Two Pass Assembler


Design of a Two Pass
Assembler
• Pass I:-
1. Separate the symbol, mnemonic, opcode and operand.
2. Build Symbol Table.
3. Perform LC Processing.
4. Construct Intermediate Representation.
• Pass II:-
1.Process Intermediate Representation to synthesize the target
program.
Pass I

• Pass I uses the following


data structures
1. Machine Opcode table (OPTAB)
2. Symbol Table (ST)
3. Literal Table (LT)
4. Pool Table (PT)
1. OPTAB contains opcode, class and opcode
length.
2. SYMTAB contains symbol and address.
3. LITTAB contains literal and address.
4. POOLTAB contains starting literal number
of
each pool.
START 200 LC
MOVER AREG, =‘5’ 200
MOVEM AREG, X 201
L1 MOVER BREG, =‘2’ 202
ORIGIN L1+3
LTORG 205
206
NEXT ADD AREG,=‘1’ 207
SUB BREG,=‘2’ 208
BC LT, BACK 209
LTORG 210
211
BACK EQU L1 212
ORIGIN NEXT+5
MULT CREG,=‘4’ 212
STOP 213
X DS 1 214
END
START 200
START 200 LC
Symbol Address Literal Address Pool Table
MOVER AREG, =‘5’ 200
MOVEM AREG, X 201 0
L1 MOVER BREG, =‘2’ 202
ORIGIN L1+3
LTORG 205
206
NEXT ADD AREG,=‘1’ 207
SUB BREG,=‘2’ 208
BC LT, BACK 209 MOVER AREG,=‘5’ 200
LTORG 210
211 Symbol Address Literal Address Pool Table
BACK EQU L1 212
=‘5’ --- 0
ORIGIN NEXT+5
MULT CREG,=‘4’ 212
STOP 213
X DS 1 214
END
MOVEM AREG,X 201
START 200 LC
MOVER AREG, =‘5’ 200 Symbol Address Literal Address Pool Table
MOVEM AREG, X 201 X ---- =‘5’ --- 0
L1 MOVER BREG, =‘2’ 202
ORIGIN L1+3
LTORG 205
206
NEXT ADD AREG,=‘1’ 207
SUB BREG,=‘2’ 208
BC LT, BACK 209
LTORG 210 L1 BREG,=‘2’ 202
211 MOVER
BACK EQU L1 212 Symbol Address Literal Address Pool Table
ORIGIN NEXT+5
MULT CREG,=‘4’ 212 X ---- =‘5’ --- 0
STOP 213
L1 202 =‘2’ ---
X DS 1 214
END
ORIGIN L1+3 203
START 200 LC
MOVER AREG, =‘5’ 200 Symbol Address Literal Address Pool Table
MOVEM AREG, X 201
X ---- =‘5’ --- 0
L1 MOVER BREG, =‘2’ 202
ORIGIN L1+3 L1 202 =‘2’ ---
LTORG 205
206
NEXT ADD AREG,=‘1’ 207
SUB BREG,=‘2’ 208
BC LT, BACK 209
LTORG 210 LTOR 205
211 G 206
BACK EQU L1 212 Symbol Address Literal Address Pool Table
ORIGIN NEXT+5
MULT CREG,=‘4’ 212 X ---- =‘5’ 205 0
STOP 213
L1 202 =‘2’ 206 2
X DS 1 214
END
NEXT ADD AREG, =‘1’ 207

START 200 LC Symbol Address Literal Address Pool Table


MOVER AREG, =‘5’ 200
MOVEM AREG, X 201 X ---- =‘5’ 205 0
L1 MOVER BREG, =‘2’ 202
L1 202 =‘2’ 206 2
ORIGIN L1+3
LTORG 205 NEXT 207 =‘1’ ----
206
NEXT ADD AREG,=‘1’ 207
SUB BREG,=‘2’ 208
BC LT, BACK 209 SUB 208
LTORG 210 BREG,=‘2’
211
BACK EQU L1 212
Symbol Address Literal Address Pool Table
ORIGIN NEXT+5 X ---- =‘5’ 205 0
MULT CREG,=‘4’ 212
STOP 213 L1 202 =‘2’ 206 2
X DS 1 214
END
NEXT 207 =‘1’ ----

=‘2’ -----
BC LT, BACK 209
START 200 LC
MOVER AREG, =‘5’ 200 Symbol Address Literal Address Pool Table
MOVEM AREG, X 201
L1 MOVER BREG, =‘2’ 202 X ---- =‘5’ 205 0
ORIGIN L1+3
L1 202 =‘2’ 206 2
LTORG 205
206 NEXT 207 =‘1’ ----
NEXT ADD AREG,=‘1’ 207
SUB BREG,=‘2’ 208 BACK ---- =‘2’ ----
BC LT, BACK 209
LTORG 210 LTOR 210
211 G 211
BACK EQU L1 212
Symbol Address Literal Address Pool Table
ORIGIN NEXT+5
MULT CREG,=‘4’ 212 X ---- =‘5’ 205 0
STOP 213
X DS 1 214 L1 202 =‘2’ 206 2
END
NEXT 207 =‘1’ 210 4

=‘2’ 211
BACK EQU L1 212

START 200 LC Symbol Address Literal Address Pool Table


MOVER AREG, =‘5’ 200
X ---- =‘5’ 205 0
MOVEM AREG, X 201
L1 MOVER BREG, =‘2’ 202 L1 202 =‘2’ 206 2
ORIGIN L1+3
LTORG 205 NEXT 207 =‘1’ 210 4
206
NEXT ADD AREG,=‘1’ 207
BACK 202 =‘2’ 211
SUB BREG,=‘2’ 208
BC LT, BACK 209 ORIGIN NEXT+5 213
LTORG 210
211 Symbol Address Literal Address Pool Table
BACK EQU L1 212
ORIGIN NEXT+5 X ---- =‘5’ 205 0
MULT CREG,=‘4’ 212
L1 202 =‘2’ 206 2
STOP 213
X DS 1 214 4
NEXT 207 =‘1’ 210
END
BACK 202 =‘2’ 211
MUL CREG,=‘4’ 212
T
Symbol Address Literal Address Pool Table
START 200 LC
MOVER AREG, =‘5’ 200 X ---- =‘5’ 205 0
MOVEM AREG, X 201
L1 MOVER BREG, =‘2’ 202 L1 202 =‘2’ 206 2
ORIGIN L1+3
NEXT 207 =‘1’ 210 4
LTORG 205
206 BACK 202 =‘2’ 211
NEXT ADD AREG,=‘1’ 207
SUB BREG,=‘2’ 208 =‘4’ ----
BC LT, BACK 209
LTORG 210 STOP 213
211 Symbol Address Literal Address Pool Table
BACK EQU L1 212
ORIGIN NEXT+5 X ---- =‘5’ 205 0
MULT CREG,=‘4’ 212
L1 202 =‘2’ 206 2
STOP 213
X DS 1 214 NEXT 207 4
=‘1’ 210
END
BACK 202 =‘2’ 211

=‘4’ ----
X DS 1 214
Symbol Address Literal Address
Pool Table
START 200 LC X 214 =‘5’ 205
MOVER AREG, =‘5’ 200 0
MOVEM AREG, X 201 L1 202 =‘2’ 206
L1 MOVER BREG, =‘2’ 202
2
NEXT 207 =‘1’ 210
ORIGIN L1+3 4
LTORG 205 BACK 202 =‘2’ 211
206
NEXT ADD AREG,=‘1’ 207 =‘4’ ----
SUB BREG,=‘2’ 208
BC LT, BACK 209 END
LTORG 210
Literal Address
211 Symbol Address Pool Table
BACK EQU L1 212 =‘5’ 205
ORIGIN NEXT+5 X 214 0
MULT CREG,=‘4’ 212 =‘2’ 206
L1 202 2
STOP 213
=‘1’ 210
X DS 1 214 4
NEXT 207
END =‘2’ 211
BACK 202 5
=‘4’ 215
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 [STSTO]
4) Process some pseudo ops[EQU,DS etc] [POTGET1]
5) Remember Literals [LITSTO]
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]
Step 2. Data structure:-
Pass1: Databases
• Input source program
• “LC” location counter used to keep track of each
instructions addr.
• M/c operation table (MOT) [Symbolic mnemonic &
length]
• Pseudo operation table [POT], [Symbolic mnemonic
& action]
• Symbol Table (ST) to store each lable & it’s value.
• Literal Table (LT), to store each literal (variable) & it’s
location.
• Copy of input to used later by PASS-2.
Step 2. 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].

You might also like