SPCC Chap 1&2
SPCC Chap 1&2
Programming
Unit 1
Introduction
Introduction
• What is System?
– System is the collection of various components
• Ex:- College is a system.
• College is a system because it consist of
various components like various departments,
classrooms, faculties and students.
• What is Programming?
– Art of designing and implementing the programs.
• In college system, what is program?
• A LECTURE can be a program.
Because it has input and output.
• Input-> The information that teacher
is delivering.
• Output-> The knowledge student has
been received.
• So system programming is an art of
designing and implementing system
Programs.
What is Software ?
• Software is collection of many programs
• Two types of software
–System software: These programs assist
general user application programs
• Ex:- Operating System , Assembler etc.
– Application software
• These are the software developed for the
specific goal. Ex. Media Player, Adobe
Reader.
• System Program:-
“These are programs which are required for
the effective execution of general user
programs on computer system.”
• System Programming:-
“ It is an art of designing and
implementing system programs.”
Course Objective and Course Outcomes
Syllabus
Syllabus
Text Editors
• Text editor’s example is Notepad.
• Editor is a computer program that allows a
user to create and revise a document.
• A text editor is a program in which the
primary elements being edited are character
strings.
• A text editor is a type of program used for
editing plain text files.
• With the help of text editor you can write your
program(e.g. C Program or Java Program).
Debugger
• A debugger is a computer program used to test and find bugs (errors) in other
programs.
• A debugger is also known as a debugging tool.
• 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.
• This allows debuggers to stop or halt the program according to specific conditions.
However, use of simulators decreases execution speed.
• When a program crashes, debuggers show the position of the error in the target
program. Most debuggers also are capable of running programs in a step-by-step
mode, besides stopping on specific points.
• They also can often modify the state of programs while they are running.
Loaders
• A loader is a program that takes
object code as input and prepares
them for execution.
• It initiates the execution.
• Functions:
1. Allocation
2. Linking
3. Relocation
4. Loading
Allocation
• Loader allocates space for programs in
main memory.
Linking
• If we have different modules of our
program.
• Loader links object modules with each
other.
Relocation
• Adjusting all address dependent
location.
• E.g. If we have two Programs Program
A and Program B.
• Program A is saved at location 100.
• And user wants to save Program B on
same location. That is physically not
possible.
• So loader relocates program B to
some another free location.
Loading
• Physically loading the machine
instructions and data into main
memory.
Assembler
• Assembler is a translator which
translates assembly language
program into machine language.
Assembler
Assembly Machine
Language Language
Program
Macro Processor
• Macro allows a sequence of source
language code to be defined once and
then referred many times.
• Syntax:
Macro macro-name[set of parameters]
// Macro
Body
MEND
• A macro processor takes a source with
macro definition and macro calls and
replaces each macro call with its body.
Compiler
• Compiler is a translator which converts
the high level language into low level
language.
• Benefits of writing a program in a high
level language :
• Increases productivity: It is very easy to
write a program in a high level language.
• Machine Independence: A program
written in a high level language is
machine independent.
Debugger
• Debugging tool helps programmer for
testing and debugging programs.
• It provides some facilities:
• Setting breakpoints.
• Displaying values of variables.
Assembly
Language
• Assembly language is low level language.
• Declarative/Declaration
Statements:
• Assembler
Directive:
Imperative
Statements
• Imperative means mnemonics
• These are executable statements.
• Each imperative statement indicates an
action to be taken during execution of the
program.
• E.g MOVER BREG, X
STOP
.
READ X
ADD AREG, Z
Declarative
Statements
• Declaration statements are for
reserving memory for variables.
• We can specify the initial value of a
variable.
• It has two types:
• DS // Declare Storage
• DC // Declare Constant
DS(Declare
Storage):
• Syntax:
• [Label] DS <Constant specifying
• E.g. size> X DS 1
DC (Declare
Constant):
Syntax: DC <constant specifying
[Label ] value> DC ‘5’
E.g Y
Assembler
Directive
• Assembler directive instruct the
assembler to perform certain actions
during assembly of a program.
• Some assembler directive are:
• START <address constant>
• END
Advanced Assembler
Directives
• 1.
ORIGIN
• 2. EQU
• 3.
USING
• 4. DROP
• 5.
Sample Assembly
1.
Code
START 100 It is an AD statement becoz it has
Assembler directive START
8
Identify the types of
statements
State.No IS DS AD
1 AD
2 IS
3 IS
4 IS
5 IS
6 DS
7 DS
8 AD
Advanced Assembler
Directives
• ORIGI
N
• EQ
U
• LTOR
G
Definitions
• LC:
• Symbol:
• Literals:
• Procedure
s:
How LC
Sr. NO Operates? LC
1 START 100
2 MOVER AREG, X
3 MOVER BREG, Y
5 MOVEM AREG, X
6 X DC ‘10’
7 Y DC ‘15’
8 END
How LC
Sr. NO Operates? LC
1 START 100
6 X DC ‘10’ 104
7 Y DC ‘15’ 105
8 END
Identify symbol, literals, AD,
IS, DS, Label
• START 100
• MOVER BREG, =‘2’
• LOOP MOVER
AREG, N
• ADD BREG, =‘1’
• ORIGIN LOOP+5
• LTORG
• ORIGIN NEXT +2
• LAST STOP
• N DC ‘5’
• END
Solution (From Previous
Example)
Sr. No AD DS IS Symb Literal
ol
Label
1 AD
2 IS =2
3 IS N LOOP
4 IS =1
5 AD
6 AD
7 AD
8 IS LAST
9 DS
10 AD
Machine
Structure
Machine
Structure
• Consider any hypothetical assembly
language.
• It supports three registers:
• AREG
• BREG
• CREG
• Machine instruction
Format:
• It supports 11 different
OPERATIONS.
• STOP
• ADD
• SUB
• MULT
• MOVER
• MOVEM
• COMP
• BC
• DIV
• READ
• PRINT
• In this hypothetical machine,
• First operand is always a CPU register.
• Second operand is always memory
operand.
• READ and PRINT instructions do not
use first operand.
• The STOP instruction has no operand.
• Each symbolic opcode is associated
with machine opcode.
• These details are stored in machine
opcode table(MOT).
• MOT contains:
• 1. Opcode in mneonic form
• 2. Machine code associated with the
opcode.
Symbolic Opcode Machine Code for Size of instruction
opcode (in number of
words)
STOP 00 1
ADD 01 1
SUB 02 1
MULT 03 1
MOVER 04 1
MOVEM 05 1
COMP 06 1
BC 07 1
DIV 08 1
READ 09 1
PRINT 10 1
Symbolic Opcode Machine Code for opcode
START 01
END 02
LTORG 03
ORIGIN 04
EQU 05
Sr. NO Declarative Statement Machine Opcode
01 DS 01
02 DC 02
Sr. Symbolic opcode Machine opcode
No
1 AREG 01
2 BREG 02
3 CREG 03
ASSEMBLER
• An assembly language program can be
translated into machine language.
• It involves following steps:
• 1. Find addresses of variable.
• 2. Replace symbolic addresses by
numeric addresses.
• 3. Replace symbolic opcodes by
machine operation codes.
• 4. Reserve storage for data.
Step 1
• We can find out addresses of variable
using LC.
• First identify all variables in your program.
• START 100
• MOVER AREG, X
• MOVER BREG, Y
• ADD AREG, X
• MOVEM AREG, X
• X DC ‘10’
• Y DC ‘15’
• END
Step 1
Sr. NO LC
1 START 100
6 X DC ‘10’ 104
7 Y DC ‘15’ 105
8 END
Sr. No Name of Address
Variable(Symb
ol)
1 X 104
2 Y 105
Step2: Replace all symbolic address
with numeric address.
• START 100
• MOVER AREG, 104
• MOVER BREG, 105
• ADD AREG, 104
• MOVEM AREG, 104
• X DC ‘10’ Memory is reserved but no
code is generated.
• Y DC ‘15’
• END
Step3: Replace symbolic opcodes
by machine operation codes.
LC Assembly Instruction Machine Code
101 MOVER AREG, 104 04 01 104
105
106
107
Question For U
START 102
READ X READ Y
MOVER AREG, X
ADD AREG, Y
MOVEM AREG, RESULT
PRINT RESULT
STOP
X DS 1
Y DS 1
RESULT DS 1
END
Question For u
START 101
READ N
MOVER BREG, ONE
MOVEM BREG, TERM
AGAIN MULT BREG, TERM
MOVER CREG, TERM
ADD CREG, ONE
MOVEM CREG, TERM
COMP CREG, N
BC LE, AGAIN
MOVEM BREG, RESULT
PRINT RESULT
STOP
N DS 1
RESULT DS 1
ONE DC ‘1’
TERM DS 1
TWO DC ‘2’
END
Assembler
• An Assembler is a translator which
translates assembly language code into
machine language with help of data
structure.
• Literal Table:
Literal address
• MOT:
Mnemonic Machine Class Length
Opcode
• Pool Table:
Literal Number
Look for Modularity
• If your program is too long…
• U can make modules of it.
Forward Reference Problem
• Using a variable before its definition is
called as forward reference problem.
• E.g.
• START 100
• MOVEM AREG, X
• MOVER BREG, Y
• ADD AREG, Y
• X DC ‘4’
• Y DC ‘5’
• END
• In example variable X, Y are making
forward reference.
• So, We can solve it by using back
patching.
Consider another
example
Apply LC
Try to convert it into machine
code
Try to convert into machine
code
Backpatching
• The operand field of instruction
containing a forward reference is left
blank initially.
• Step 1: Construct TII(Table of
incomplete instruction)
• Step 2: After encountering END
statement symbol table would contain
the address of all symbols defined in the
source program.
SYMBOL NAME ADDRESS
X 104
ONE 105
TEN 106
• Now we can generate machine
code…
Assembler
Directive
• ORIGIN
• LTORG
• EQU
Pass 1 Assembler
• Pass 1 assembler separate the labels , mnemonic
opcode table, and operand fields.
• Determine storage requirement for every assembly
language statement and update the location counter.
• Build the symbol table. Symbol table is used to store
each label and each variable and its corresponding
address.
• Pass 2 Assembler: Generate the machine code
How pass 1 assembler
works?
• Pass I uses following data structures.
• 1. Machine opcode table.(MOT)
• 2. Symbol Table(ST)
• 3. Literal Table(LT)
• 4. Pool Table(PT)
BACK EQU L1
ORIGIN NEXT+5
MULT CREG, =‘4’
STOP
X DS 1
END
Apply LC
START 200
MOVER AREG, =‘5’ 200
MOVEM AREG, X 201
L1 MOVER BREG, =‘2’ 202
ORIGIN L1+3
LTORG
=‘5’ 205
=‘2’ 206
• 1. C: Constant
• 2. S: Symbol
• 3. L: Literal
• 4. RG: Register
• 5. CC: Condition codes
• E.g. MOVER AREG, X
• For a symbol or literal the reference
field contains the index of the
operands entry in symbol table or
literal table.
•
• So IC for above line is:
• (IS, 04) (RG, 01) (S, 0)
• For example…
• START 200
• IC:(AD, 01) (C, 200)
Intermediate Code
(AD, 01) (C, 200)
200 (IS, 04) (RG,01) (L, 0)
201 (IS, 05) (RG,01) (S,0)
202 (IS, 04) (RG,02) (L,1)
203 (AD, 03) (C, 205)
205 (DL, 02) (C,5)
206 (DL, 02) (C, 2)
207 (IS,01) (RG, 01) (L, 2)
208 (IS, 02) (RG, 02) (L,3)
209 (IS, 07) (CC, 02) (S, 3)
210 (DL,02) (C,1)
211 (DL,02) (C,2)
212 (AD, 04) (C, 202)
212 (AD, 03) (C, 212)
212 (IS, 03) (RG, 03)(L, 4)
213 (IS, 00)
214 (DL, 01, C, 1)
215 (AD, 02)
215 (DL, 02) (C,4)
START 205
Example No.2
MOVER AREG, =‘6’
MOVEM AREG, A
LOOP MOVER AREG, A
MOVER CREG, B
ADD CREG, =‘2’
BC ANY , NEXT
LTORG
ADD BREG, B SUB
NEXT AREG, =‘1’ BC LT,
BACK STOP
LAST ORIGIN LOOP+2 MULT
CREG, B
A BACK
ORIGIN LAST+1
B END
DS 1
EQU LOOP
DS 1
• PASS 2 assembler requires two
scans of program to generate
machine code.
• It uses data structures defined by pass
1. like symbol table, MOT, LT.
Design of two pass assembler
• Tasks performed by the passes of a two
pass assembler are as follows:
• Pass 1:
1.Separate the symbol, mnemonic opcode,
and operand fields.
2. Build the symbol table.
3. Perform LC processing.
4. Construct intermediate representation(or
IC).
• Pass 2:
1. Synthesize the target program.
Two Pass Assembler
Analysis Phase Vs. Synthesis Phase
Pass 1
Algorithm
Pass 2
Algorithm
Comparison between Pass 1 and
Pass2
Sr. No Pass 1 Pass 2
ORIGIN L1+3
LTORG
=‘5’ 205
=‘2’ 206
BACK EQU L1
ORIGIN NEXT+5
MULT CREG, =‘4’ 212
STOP 213
X DS 1 214
END
=‘4’ 215
Symbol Table and Literal Table
index Symbol Name Address
•
0 X 214
1 L1 202
2 NEXT 207
3 BACK 202
START 100
MOVER AREG, X
ADDER BREG, X
ADD AREG, Y
X DC ‘2’
X DC ‘3’
Z DC ‘3’
END
• START 100
• MOVER AREG, X
• ADDER BREG, X Invalid opcode
• ADD AREG, Y Undefined symbol Y
• X DC ‘2’
• X DC ‘3’ duplicate definition of Symbol X.
• Z DC ‘3’
• END
Home-Work
2. Explain following Assembler Directives with example.
1. ORIGIN
2. LTORG
3. EQU
7.Refer the program from question no. 1 and write down the
Intermediate code and machine code for the same.