System Programming and Compiler Construction Techknowledge Book PDF
System Programming and Compiler Construction Techknowledge Book PDF
Assembly languages are a family of low-level An assembler Is a program that accepts an assembly
languages for programming computers, micro controllers, language as input program and produces its machine
microprocessors and other Integrated circuits. They language equivalent along with the Information for
Implement a symbolic representation of the numeric
the loader. Fig. 2.1.1 shows the basic schematic of an
machine codes and other constants needed to program a
assembler.
particular CPU architecture. An assembly level language in
thus specific to a certain physical or virtual computer
architecture. ~.,,,_mbly . ASS EMB ~---,Machlne ln81Nc11or) A
f,language .r--- ,, Info tlon I •- - -
j'~'.OQ~ rma or ,.,..,...
An assembler Is a program used to convert the
assembly level language statements Into the target
machine code. The assembler performs a one-to-one
mapping from mnemonic Instructions to machine
statements and data. Mnemonic is a code usually from 1 to Fig. 2.1.1 : Basic Structure of an Assem bler
S letters that represents an op-code followed by one or
more numbers. This is In contrast with high level languages
In which single statement generally results In many
machine Instructions. An assembly language Is a machine
dependent, low level programming language which Is
specific to a particular computer system.
Loader
2.1.1 Advantages of Assembly Language
1) Opcode Mnemonic•
An opcode Is a symbolic name for a single executable machine instruction, and there is at least one opcode mnern .
defined for each of the machine language Instruction. It eliminates the need to memorize numeric operation code;_n,c
2)
In this the value 1 is "moved" into the EAX operand (register).
Data Sections
They are instructions used to define data elements to
2) Declaration Statements
These are non executable statements. They declar
-
. e
hold data and variables. They define type of data, constants or storage areas m a program.
length and alignment of data. E.g. X OS 2
3) Assembly Directives/Pseudo Operators (In this OS = Declare Storage. It reserves memory area
of 1 word and associates the name X with it).
Assembly directives are Instructions that are executed
by an assembler at the assembly time. Symbolic 3) Assembler Directives
assemblers allow programmers to associate arbitrary
names (labels or symbols) with memory locations. It instructs the assembler to perform certain actions
during the assembly of a program.
2.2.2 Assembly Language Program Contains E.g. ABC START 1000
Three Kinds of Statements
END
(It indicates where to start assembly of the input
1) Imperative Statements
program is performed).
They are executable statements understood by the
machines. It indicates an action to be performed 2.2.3 An Assembly Language Program
during the execution of the assembled program. Each
Imperative statements typically translates into one When the user wants to communicate with the
machine instruction those are understood by the computer, he has a lot of languages :
machine. English Best for programmer
E.g. MOVA, 1 C/C++/JAVA
It states "Move" value one to register A
Assembly language
Machine language Best for machines
.! System Prog. & Compier Const. (MU) 2-3
we will now discussed assffllbly language which is the most mldline d~dMt ~n1t.1a1e
- PROGRAM COMMENTS
START Identifies the name of the pr01ram
TEST
BALR 15, 0 ~t register 15 to the address of the next instruction
BEGIN
USING BEGIN + 2, 15 Pseudo-opcode Indicating to assffllbler rqlster 15 Is base resist~. and its content
Is address of next Instruction
SR 4, 4 Oear register 4
Description
1) USING
The using instruction tells what register can be used as base register and what that register will contain. Using is a
pseudo-opcode to indicate the type of the register. A pseudo-opcode is an assembly language Instruction that
specifies an operation of the assembler. It is distinguished from the machine-op code which represents to the
assembler a machine instruction.
2) BALR
Base register is loaded with the BALR instruction whereas USING pseudo-opcode provides information to the
assembler.
3) START
4) END
5) DS and DC
The Define Storage and Define Constant are pseudo-opcodes that instructs an assembler to place a 10, 6, and 46 in 3
consecutive full words ("F") in memory and leave 10 more spaces for the data.
Assembler,
System Prog. & Compiler Con1t. (MU) 2 ◄ ~
6) LITERAL$
Literal constants (o~en referred to is llter1l1 or constlnts) are e,cpllclt Invariant values contained with source
proarams.
Assembled VersIon o, Example pro1r1m
Aoembltd Mnemonic PrOll'am
A.11tmbty Lln,u111 Pro,rarn R1l1t1V1 LOC1don
TEST START
BALR 15,0
BEGIN BALR 15,0 0
USING BEGIN +2, 15 2
SR 4, 4
SR 4,4 4
L 3, 30 (0, 15)
L 3 • F'10' 8
L 2, 42 (6, 15)
LOOP L 2, DATA (6) 12
A 2, 34 (0, 15)
A 2, =F'46' 16
ST 2, 42 (4, 15)
ST 2, DATA (6) 20
A 4, 38 (0, 15)
A 4, =F'4' 24
BCT 3, 6 (0, 15)
BCT 3, • -16 28
:
DATA DC F '1, 3, 3, 3, 3, 4, 5, 9, 0' 44 1
48 3
52 3
END
~ Index Register Value
e.g. A 3, _j 30
There are certain fundamental functions that any assembler should perform such as
A simple SIC (Slmpllfled Instructional Computer) Assembler : It gives the basic functions performed by a assembler
and describes a way to carry out these functions.
Assembler Algorlthms, Tables, Data Structure and Logic : It describes the data structure table and logic used by
almost all of the assemblers.
Assembler directives are pseudo-Instructions; they provide Instructions to the assembler Itself and are not tran.slated
Into machine operation codes. It helps us to do to the following tasks :
Assemble code and data Into specified sections
o Initialize memory
The steps that should be followed by the programmer for designing of an assembler are mentioned as following :
1. Specify the problem to identify the information necessary to perform a task.
2. Design a suitable data structure to record and store the information.
3. Determine the process necessary to obtain and maintain the information.
4. Specify the algorlthm.
5. Determine the process necessary to perform the task.
6. At last the programmer should look for the modularity of the program (i.e. program to be subdivided into
independent programming units). It helps all the modules to be tested and deployed separately.
Basic steps required In designing the modular system are as :
Modules created should be opaque to the rest of the system and initialized through a well-known interface.
Modules should not directly reference with one another or the application that loaded them.
Modules should not communicate directly they should use loosely coupled techniques, such as shared services, to
communicate with the application or with other modules.
Modules should not rely on static methods that can inhibit testability.
The mentioned basic steps are also used for the design of the loader, and complier.
y System p
rog. & Com lier Const. (MU) 2-6
Assemblers
2
-=~-S~P!::a~s~s~S~t~ru~c~t~u~re~o~f~A~s~s~e!!m~b~le~r~ - - - - -- - - - - - - - - - - - -
Source
Program
Intermediate I==:::>!
file
The simpler assembler uses three major internal data _ It includes the name and value for each label in the
structures : source program together with type, length and flag.
l. OPTAB: Operation Code Table
It is a dynamic table.
2• LOCCTR : Location Counter
3• It has the operation like insert, delete and search.
SYMTAB: Symbol Table
The intermediate file includes each source statement, - It usually uses a hash table.
assigned address and error indicator.
In PASS 1
1. OPTAB : It includes
Enter labels and addresses to all statements as they
Mnemonic operation code ¢> Machine code are encountered in source program.
It has instruction format and length Processing for assembler directives is performed.
Implementation: Save addresses for all values assigned to all labels for
case in PASS 2.
o It is a static table (No updates)
In PASS 2 (Multi pass)
o Array or hash table. (It is usually organized
as a hash table, with mnemonic operation Look up operand labels for addresses.
code as the key)
Assemble instructions.
In PASS 1: 1) It validates the op-codes
2) Computes Instruction length Write the object program.
LOCTR f-- LOCCTR + (instruction length) ~ -_'· ~hat is ~orward reference problem , ? How ls· tt is
' : re~olved in two-pass assembler? , ,.
; ... } •:;· ~ ,- f ' '
The current value of LOCCTR gives the address of the ·- \ ,. , ~ , (Dec.·1~; 6 Marks)
label encountered. a., Explain1-< forward reference problem anq how ~. Is
handled in assernble_r design, (May 19, 5 Marks)
_ SVMTAB : It includes
3
store values assigned to the labels.
It is used to
j System Prog. & Compiler Const. (MU) 2-7 Assemblers
When a symbol Is referred before It Is defined, It Is 2.6.2 Phalff used by the Assembly
called a forward reference. Language
The function of the assembler Is to replace each Basically Auembly languase uses two phases.
symbol by Its machine address and If we refer to that
symbol before It Is defined Its address Is not known by the 1 Analyala Phue
assembler such a problem Is called forward reference The tasks performed by this phase are as :
problem. Forward reference problem Is solved by making (a) It lsfotes the labels, mnemonic opcodes (An opcode is
different passes (I.e. One pass, Two pass, Multi pass) over the portion of a machine lansuage Instruction that
specifies the operation to be performed) and operand
the assembly code. E.g.:
fields of a statement.
class C { (b) If a label Is present enter the pair (symbol, location
counter (LC) contents) in a new entry of a symbol
public: tabie.
(c) Check the validity of the mnemonic opcode through
void muta for (int x) {myValue= x;}
mnemonic table.
int accessor () {return rnyValue;} (d) Perform processing of location counter Le. update the
' ,
value contained in LC by considering the opcode and
private : ~-
ii:it m).'Value;
} .' .f
operands of the statements.
2) Synthesis Phase
• J~
,,.,,._
.'< ~;. (.'
}; f
Source Target
Program Program
SYMBOL
10
,__A_G_A_IN_,.,.-,--_4 _,,.._+ Constructed during Analysis Phase
-+ DATAACCESS N . • · 113· · ,; and used during Synthesis phase
---• CONTROL TRANSFER
SYMBOL TABLE
Fig. 2.6.1 : Two Phases of an Assembler used by the Various Passes
Q. State the ·r.eason for the assembler to·be a multi pass p~g~~- ' . , ·, ' ' ·-''. · .. :· , .. (~ay 15, 5 Ma.rk~)
. . f' , H ,· ·• ❖ , • ) ,, -~ j! J} W., ~ .. J.1:,1lf -l. •l'lf1 •t.<• t tltlii."d,1\t" · ,0,,d ·
0 . With reference to assembler explain the following tables with suitable exl!lmples : • ' , /i,
. , ,, ·',
• • ' • •
· '' ~' ,, '
(i) P.OT (Ii) MOT (iii) ST · (Iv) LT . (Dec.15, M_ay 16, May 19, 'fo,:Matks)
W TtcHa ■ ldti
V P ubl lc 11 t l o n~
Aa
. & Com lier Conat. MU
2-8
What ire pllln with example. (Dec. 18,
var1oua dal•ba
__,., Uled Ill two pau UMmbltr design. EJC )
Q, ~I n th (De c
• detlgn of two dda ---· • 17, 10 ~ )
pa
118 1M l'l'i> llrw fthf low cha rUn
Q, Oraw flOWoh•rt f (May 19, 10 11...
two ...tmo lfr detlgn a n d ~ In dlCaJI. -~q )
Q. ewni.1 o I P11a -1 Of
(Dec. 19, 1o ~
.""....n With floW char t de
sign ot two PIi i uat mb l,r. )
mbl the two pass asse mble rs are IBM
A two Pass asse r. Examples of
more than one pass Is used by assemble
360 / 370 processor Th er means slngle pass asse mbler are as follows :
. e adv anta ses of two pass assemble over
- I
t Is used to ellmlnat
e forward reference problem. bOI
Numbers of passes are created In multi pass assembler to process the definition of sym s.
.
0
Converts the code to the machine code
the detail of task accomplished by
ass emb ler doe s two passes ove r the source file. Below mentioned is
A two pass r the assembly code.
h pass . The forw ard refe renc e problem is solved by making two passes ove
eac
es the symbols and literals.
s for labe l definition and introduc es them into the symbol table. It defin
{I) Pass 1 : It look
(LC)
(a) It keeps the track of location counter
uctions (MOT).
(b) Determines the length of machine instr
bols until pass 2 is done (ST).
(c) It keeps the track of the values of sym
, OS DC (POT).
(d) Process some pseudo-opcode e.g. EQU
(e) It stores the literals (LT).
mbly by translating the operations and 50
s 2 : Afte r the sym bol table is completed in PASS 1 it does the actual asse
{II) Pas
e object code.
on. The purpose of PASS 2 Is to generat
.
(a) It looks up the values of symbols (ST)
(b) It generates the instructions (MOT).
literals).
(c) It generates the data (For DS, DC and
G, DROP (POT).
Process some pseudo-opcodes e.g. USIN ..-- ...
(d)
Assembly j j ~ j PASS 2 ~
Machine
Language
Language - - . PASS 1 _ __, Program
Program
Symbol table
Machine operaUon table
Pseudo Op code table
ler
Fig. 2.7.1 : ASimple two Paaa Assemb
the PASS 2.
we specify the format and con tent of each of the data bases used in
(Ill) FORMAT of DATABASES : In this
Codes
Instruction length Instruction format
01 = 1 Half-Words = 2 bytes 000 = RR - Register to Register
10 = 2 Half-Words = 4 bytes 001 = RX - Register and Indexed Storage Operation
11 = 3 Half-Words= 6 bytes 010 = RS - Register and Storage Operation
011 = SI - Storage and Immediate Operation
100 = SS - Operation using all implied operand and storage
'!!!l"e TtcHu11ltllti
V Pubtll-1Jt1ons
lier Const. (MU) 2-10
A - Absolute value
literal is equivalent to define a constant explicitly and assign an address label for it literal table is often organized as a
hash table, using literal name or value as the key.
____
.,..
2-11
15 "Y" 00 00 00 l
Availability
Pseudo
Opcode
Table PO
Source
Program
Symbol
Table
ST
Ltteral
Table
LT
Base
Table
BT
Listing
10) PUNCH CARD workspace used to punch the assembled instructions in the format needed by the loader.
11) An output deck of assembled instructions in the format needed by the loader.
process of Pseudo-Opcodes
Pass 1
Found r - - - - - ,
OS EOU USING
DC DROP
Aaelgnstorage
Search Adjust LC to Ewluate loc811on to
proper Alignment erand field literals
EVAL
L ~length
I) DS and DC : These both are the assembler directive 11) EQU : Define the symbol and assign the value evaluate
that tells the assembler something it needs to know in by the expression in the operand field.
order to carry out the assembely process. Ill) USING and DROP : These pseudo-opcodes neither
DC stands for Define constant. In assembly language define symbols labels nor affect the location counter.
specified as So no processing required only save the USING and
DROP cards for pass 2.
<label> DC <value> Define Constant
iv) END : When END is encountered pass 1 is terminated
<label> OS <value> Define Storage
and literals are assigned the storage locations and we
<label> = Symbolic name, <value> = It indicates the goto pass 2. The mentioned steps are repeated for the
value supplied by the programmer. next instructions.
They can affect both LC and the definition of the
symbol In the pass 1.
a Ttdalu ■ tdi
"',I" P u b l ll;atlons
♦ S stemP ro lier Const. (MU)
2-14
r and storage Operat ions
c) RS-Reglste
(VII) Paaa 2 : Objec t Code la Gener ated . five-byt e instruct ion of the form OP
This Is a 1
D2, B2.
The first byte contain •
s the 8-bit in R1' ~3.
It genera tes object code Into the approp riate format stru.-.·
, ,10~
a) RR - Forma t Instruction
o
[o Pf ~~ i
7 8 1112 1518 19 20 31
It denote s registe r to register operati on
SI • storage and immed iate Operations
Each of the registe r specific ation field is evaluated and d)
the two fields are inserte d Into their respective four- immedia te Storage
operand s operand s
bit fields in the second byte of the RR-instruction. 2 1
_..:-~----r-
RR has 8-bit opcode and two 4-bit general register
field that typical ly specifie s any of 16 general registers. [oP I 12 'I ~1 I ?2]
O 7 8 1516 1920 31
-2bytes-
1 2
~ e)
ss - Storage and Storage Operations
j.__oP ·j R1 j R2 ] Storage operands
Length 1 2
0 78 1112 15
J
.:.....,_ __,,-'
Registe r operand s
82 1 02
b) RX - Register and Indexed Storage Operations 47
0
78 1 5 16 19 20 31 32 35 36
The registe r (Rl) and index fields are evaluated and
OP - Operati on code
the two fields are inserte d into their respective four-
R - Contents of general registe r used as operand
1
bit fields.
X - Contents of genera l registe r used as lnde><
We calcula te the effectiv e address of the operand and 1
we determ ine approp riate displac ement and base B - Contents of genera l registe r used as Base
1
registe r and also ensure D < 4096 and put base and
rd th D1- Displac ement
displac ement in the 3 and 4 bytes.
11 - Immedi ate data
RX -format
Registe
operandrs Storage operand 2 L - Operand length
1
Processing of Pseudo-Opcodes
Pass2
Read card
.--- -from Ille co
READ2
Found . - - - - - - .
Search
Due to the requirements that the full words must start EQU pseudo-op only prints the EQU card as a part of
on a byte whose address is a multiple of four we the printed listing.
adjust the location counter. In case of DC we form
constants and insert in the assembled program and iii) USING
increment the LC as per the length of the data field.
The operand fields of the pseudo-opcodes are
evaluated and then the corresponding base table
entry in marked as available.
s;{c::, Ttd1Kullld1i
V r llb l l< .a tlt'lns
P•••
.---
1
Relative Mnemonic
The operand field of the pseudo-opcodes are s,mple Address Instruction
v)
evaluated and then the corresponding base table
entry Is marked as unavailable.
END
--
~
p;o1r111"1
pRG sTART 0
•,15
-----
-----
us1NG
END Indicates the end of the source program and
termin ated the assembly, and the literals are
- L
1, FIVE 0 L 1, - (0, 15)
--.:...
4 A 1, - (0, 15)
--
generated for entries In Literal Table (LT). i--
1,
A
FOUR
(VIII) Example of Two P••• Aaaembler
,_ 8 ST 1, - (0, 15)
ilWMM&ID 1,
DC
TEMP
F '4' 12 4 --
pass assembler. Explain with database.
(May 16, 1 O Mark•) FIVE
-TEMP
DC F '5' 16
-
5
-
ABC START
USING
0
•, 15
DS
END
lF 20
-
L 1, FIVE we look up bit config uration for the mnemonic in a
A 1, FOUR table and put the bit config uration for the L in the
ST 1, TEMP
appropriate place.
FOUR DC F '4' Now we need the address of five. At this point we
FIVE pc F '5' don't know where FIVE is, so we canno t supply its
TEMP DS IF address. And no index registe r is used. We put in Ofor
END, the index register and we know that the register 15 is
being using as the base registe r, but we cannot
Solut ion: calculate offset so we write 1, - (0, 15).
START 0
PRG We maintain a locatio n counte r indicat ing the relative
USING •, 15 address of the instruc tion being processed this
-----------------------------~=-=:=~~~:_:.~~dl
...
1ocat1on counte r vaIue will
. be given as 12 and 20.
V p ulJ ll t• 11,jfi
-~Tl ar
1
jr System Prog. & Compiler Const. (MU) 2-17 Assemblers
It Is also known as the Single Pass Assembler for the (Ill) Two types of One Pass Aaembler
Intel 8086 or x86 / 8088 processor used In IBM PC. In
There are mainly two types of single-pass assembler :
single pass assembler the assembler passes over the source
exactly once and converts equivalent machine code of the 1) First type produces object code directly in memory for
whole program at one time. Due·to this It requires less time immediate execution e.g. LOAD AND GO ASSEMLBER.
but more memory space as it has a single chance to convert In this type of assembler no object program is written
the program.
out, no loader is needed. It is useful In a system with
frequent program development and testing. In this
(I) Problem faced by Single Pass Assembler
program are re-assembled nearly every time they are
(a) Forward reference : It occurs when a symbol is used executed in load and go assembler forward references
before it is declared.
became less difficult because the object program is
(b) Difficulty in determining the length of the branch produced in memory rather than being written out on
Instruction : If there is a forward reference for a
secondary storage.
branch instruction like "JUMP NEXT" where "NEXT"
could be within 128 bytes from the instruction or 2) The second type produces the usual kind of object
more. Thus the length of the instruction depends on program for later execution E.g. X86, 8088 Assembler,
the location of "NEXT". MASM Assembler.
(II) Solution for the above problem (IV) Single Pass Assembler (Intel x86 Assembler)
(a) The forward reference problem can be solved by using l) Memory and Registers
the FORWARD REFERENCE TABLE.
(b) It requires that all areas must be defined before they (a) The basic units of memory are as follows :
are referenced.
Units of memory Bytes Length in bits
(c) It is possible, although inconvenient, to do so for data
items. Byte 1 8
(e) Forward Reference Table in one-pass assembler : (b) The architecture supports a stack for storing
subroutine address as, parameters and other data.
(i) Removes the operand address if the symbol is
not defined. (c) Two stack pointer registers called SP (Stack pointer)
and BP (Base Pointer) are provided to address the
(ii) Undefined symbol is entered into SYMTAB and
stack.
marked as not defined.
(d) The index registers SI and DI are used to index the
source and destination addresses.
Tl~ T1dllulll1•ti
Y PuC l lt a ttons
Assernb1
er8
2-18
SySlem Prog. & Compiler Const. (MU) second address. For exarnpl
produce a e, if
{e) address 100, then the expressio 8
The memory Is used to store three components of a represen t s n B -1-
lfy the address 105. The 5 in the ex S
program to be executed, they are program code, data would s1gn . Pressi0
t specifying address usrng an offset . n
and stack. CPU contains four 16 bit registers to Is the o ffse is ca11
dresslng because the resulting add ed
address these components . relative a d ress •1
t O some other point. Another word fo s
2) Declarative Statements relative r Offset
. ement) from the start of the corresp0
Is d ,sp1ac . ndin
Declaration of constants and reservation of storage t To facilitate this, the programmer has tag
segmen .
are both done In this. erform two tasks.
(a) SEGMENT: It indicates the start of the segment. statements. The routine id is field of a MOT entry
contains identifier of the routine which processes that
(b) ASSUME : The assembler has to determine its offset
particular opcode.
(offset refers to a value added to a base address to
Add - FFH Rl
"JMPbbb" - FFH - R2
JNE 75 H OOH R2
4 Machine i L Registers
understanda ble code
OOH-Single ►
(operational codes) FFH-Mult1pl e
~ntroduction format Instruction format
1s supported is supported.
SYMBOL TABLE: (SYMTAB) It is a hash organized and include II . d
(b) information about the symbol defined ao
used in the source program. s a relevant
Poin1ef lo l!lst
CRT entry
Polnler lo fiJlt CRT entry
Pointer lo flrlll FAT 9f1,y
Souroo atalomont ff
Leng1h (Leng1h of tho Symbol)
Slzo (Size ol lhe Symbol)
Owner Segmont II (SYMTAB Entry)
Olfaet In Segment
eau
Type Dellned?
Segmentname
EOU
SYMBOL
11 OS (Data segment)
00 ES
01 cs STSRT#2
10 ss
11 OS
(e) FORWARD REFERENCE TABLE (FRT) : Information regarding forward referenced is put into the FRT in a linked list
fashion.
Table 2.8.5 : Forward reference table
Pointer to Next Entry Entry# in STSRT Instruction Address Usage Code Source Statement
(2) (1) (2) (1) (2)
-:!= Ttd!Ku11ledgi
V rub 11::.1t 1on~
~~~~~-=....,==~~ 20 ==========..;;A~s~se,-
2 •==
== ,,b,
~ ~
~ t~~~~~~~~
¥' SySl em Prog. & Com piler Const. (MU)
Kind of refer ence made.
nces.
Segm ent regis ters to be used for the refere
rred.
f erence occu de to the symb ol.
State men t num ber at which the forwa rd re ference that was ma .
the type of forwa rd re feren ce Direc tory. This direc tory IiSts all
Usage code Is used to Indic ate
a cross re
: CRT Is used for pro d ucIng
(f) CROSS REFERENCE TABL E (CRT ) nt numbers.
r of stateme
refer ence s to a symb ol In the ascending orde
S C •• reference table
Tabl e 2.8. : ro
Source State ment #
Poin ter to Next Entry
r
(V) Flow char t of a Slng le Paas Asse mble
Look up mnemonic In
MOTa ndoall
appropriate routine
Imperative
n_..L,:::Cc .
...- - , -'-'--- --""-- --, . Obtain Mio Evalua te Entrer In
,---'"" ""=:-- , r--Al-=lg-=
Opcode data Manipu late operan d · CAT
Entrer In Allocate Storage Seal presen t
alignment LC expres sion
SYMT AB and Convert SRTAB From
Consta nts new SRTAB requirements
CRT
Assemble
Instruction
Yes
Example 2.8.1
Table P. 2.8.1 : Ex ■ mplt for One PHI AaHmbler
SRCSTMT# OFFSET LABEL PROGRAM OPERAND
1 0000 XYZ SEGMENT
2 - - ASSUME CS : XYZ (# 1),
: OS : PQR (# 2),
: ES: LMN (# 3)
10 0006 MOV COUNT, 0000
:
: -
90 0020 ASSUME OS: LMN (# 1)
ES: PQR (# 2)
Solution:
Statement
SRCSTMT# OFFSET LABEL PROGRAM OPERAND
1 0000 XYZ SEGMENT
SRTAB
BV T1dKa ■ t1•i
Pub l l cat to n s
P ro
lier Con1t. MU 2·22
Symbo l Table E
ntrlea for •••••me nta (#2 and #3)
- SYMBOL EQU SEQ, DIFIN!D TYPE OFFSET OWNIR LENGTH SIZE SOURCE
STMT#(2 )
PTR•TO PTA-
~
(V/N) REG, (Y/N) SEG• (2) (2)
1ST FRT TO To 1ST
IN ENTRY (2)
(Y/N) LAST CRT ENTRy~:
SEG,
ENTRY (2)
l
#2 1
# 3 N
N:.....JLV'.._L~ N- . . L _L
_ -1-..:..
~....__L_M_ N _ _J___L __L _j____
~L- ,~N~U~L~L1 -
~ R7-;:N:-7r.y: - - : r - - - , - - , - - - - - - ~ - - - r - . - - -, -N~U~L
-PPQ ----
NULL
L_ j ~NULL
:_L_..:._N:..:U:.:L:.
-1._:N.::U:.:l:L
statemen t of exampl e (SRC Stateme nt 10)
Next
: l,......M_ o_v -rl-co_ u_N-T,-00-007 ]
I.--1- 0-.-l_o_oo-6......I _,
Queue
Cross Refere nce Table Entries for above stateme nt : CRT maintain ed 88
PTR • TO SOURCE STMT #
NXTCRTS ,
10
SYMTAB ENTRIE S
SEG• DEFINED TYPE OFFSET IN OWNER LENGTH SIZE SOURCE PTR• PTR· PTA··
SYMBO L EQ
(Y/N) SEG • (2) (2) STMT
(Y/N) REG· SEG, TO TO TO
# 1ST CRT
(Y/N) 1ST LAST
(2) ENTRY(2)
FRT CRT
ENTRY ENTRY
(2) (2)
-
N N V .. 0020 #2 1 90 TO FRT TO TO REAR
COUNT
FRONT OF CRT
OF CRT
---
y System Prog. & Compiler Const. (MU) 2-23 Assemblers
Statement of example
CS (01) #1
ss (10) -- STSRT# 1
DS (11) #2
ES {00} 1H' #2 ~
cs (01) #1
ss (10) -- STSRT# 2
~
DS (11) ~ #3
STSRTAB
Relative Mnemonic
PRC START
Address. Instruction
USING*, 15
0 L 1, -(0,15)
L l,FOUR
A I, FIVE 4 A 1, -(0, 15)
ST I, TEMP 12 4
FOUR DC F'4' 16 5
FIVE DC F'S'
20
TEMP DS IF
24 -
END
=.
V
T1cK ■ 1l~9
-
Pll bl 1t a t 1o n -.
•
=
~
System
~====~~p~rog~-~&~Co~m~pl~le~r~Co~n~s~t.J(~M~Ul
ond Paaa
) _ _ _ __:2~.2~4- - . - . - :-=:======== =====~~A~s~ se~rnbi
.. Review Questions ~
-
16 s wrtte
8
short note on assembler functions.
a. 3
20 a. What Is a Forward Reference Problem and how it is
4
solved in one pass and two pass assembler?
24 -
a. ExJ>lain the databases used in one pass assembler.
5
Symbol Table
a. Explain the format of databases used in multi l>aSs
6
Symbol Value Length Relocation assembler.
a. 7 What are advantages and disadvantages of two
(8 Bytes) (4 Bytes) {l Byte) (1 Byte)
pass assembler ?
PRG 0 01 R
a. 8 For the following program :
FOUR 12 04 R a) Show the symbol tables at the end of pass 1.
15
V System Prog. & Compiler Const (MU) 2.25 Assemblers
Q.9 Explain with neat flowchart and database I the START 4000
SAMPLE
working of two pass assembler.
USING • , BX
Q.10 Using any assembler language, write a sample
assembly program, with respect to that program, MOV BL, NUM1
describe how a 2 pass assembler will translate that.
ADD BL, NUM2
a. 11 Explain with the help of databases of each of the
passes of two pass assembler. MOV RESULT,BL
a. 14 Following program is for 8086 processor. Give 0. 15 Give analysis and design of single pass assembler
result for program after first pass and second pass with respect to flowchart, data structure and
of assembler with relative address of each algorithm.
instruction . .
□
s
l. iii:[ Macros & Ma cr
Module
0
pro ces sor
.. ·- - _ __ _ ___ - - · _ -·
Introduction, Macro definition
-- ------
it .
and call, Features of Macro fac ll Y ·
nested. Design of single pass macro processor, data structures used.
Simple, pa
rameterized, conditional and
Objeo1
program
Fig. 3.2.1 : Function, of MACRO PrOCHIOrl
Raad maoro dellnlUona lnlo a memory butter
N y
LP = line number In
the statement
y
LP: line pointer
Sequence of operation
(Assembly Language)
End of definition
Pseudo-operation MEND
Fig. 3.2.4 : Format of MACRO
1. Copy Code : Example
~panded Source
// Macro defined
~croNa;- { MACAO
XYZ
L 1, D1 L 1, 01
A 1, 01
A 1, D1
{
ST 1, TEMP ST 1, 01
MEND
Assembly Instructions L 1, D1
XYZ A 1, 01
{
ST 1, 01
XYZ
Pseudo-op
Fig. 3.2.5 : Copy code - example
The statements of expansion are generated each time the macro is invoked.
2. Example for Parameter Substitution
Source Expanded source
MACRO
xvz &01
L 1, &01
L 2, & D1
L 3, & 01
MEND 1, RECORDl
INCR RECORDl
{i 2,
3,
RECORDl
RECORDl
RECOR02 1, RECORD2
{i
INCR
2, RECORD2
a. Whal are the features of macro to be Included In the proooseore? (May 1•. S Marb)
In a simple macro processor each macro processor Is expanded with Its code as shown In the Fig. 3.3.1.
MACRO
TEST
L 1, Fl
L 1, Fl
A
ST
1,
1,
Fl
SWAP
{ A 1, Fl
STl, SWAP
MEND
L 1, Fl
TEST
{ A 1, Fl
ST 1, SWAP
TEST
L 1, Fl
TEST A 1, Fl
STl, SWAP
2. Parameterlsed Macro/ Posltlonal Macro (Macro definition with one, two or many arguments)
A parameterized macro is a type of macro that is able to insert given objects into its expansion.
Parameterized macro Is not able to modify the instruction that replaces the call. To solve this problem we use macro calls
within arguments, parameters, corresponding to these dummy arguments will appear in macro definition. Parameterized
macros are useful mechanism for performing in line expansion.
~
~
Sy11em p ----,
rog.& Congor Const (MU) 3-S
Sourc,
-r.p,ndtd source
;
I
MACRO 1
{
A 1, & Al LOOPl
RECORD2
A A 2,
2, &A2
RECORD3
A 3, &A3 A 3,
MEND
RECORD3
LOOP2 A 1,
{
!
RECORD2
LOOPl LAB RECORDl, A 2,
RECORDl
RECORD2, RECORD3 A 3,
DATA3
DC F' 6'
DATAl
OS F' 12'
LOOP2 LAB RECORD3, DATA2
OS F' 24'
RECORD2, DATA3
RECORD!
In mentioned example we have specified four arguments including STRG argument. In parameterised macroslhe
parameters and arguments are associated with each other according to their positions in the macro prototype andtht
macro Invocation statements.
GENER , , DIRECT, 3
There are generally three ways of specifying arguments to a macro call.
,Y System Prog. & Compiler Const. (MU) Macros & Macro Processor
# define min (x, y) ((x) < ( y) ? (x) : ( y)) L 1, &Al INSIDE RECORD2
x = min (a, b); ⇒ x = ( (a)< (bl ? (a) : (b) ); A 1,= F' 5' INSIDE RECORD3
y = min (1, 2); ⇒ y = ( (1) < (2) ? (1) : (2) ); ST 1, &Al INSIDE RECORDl
In this feature of macro facility when one MACRO MACRO EXPANSION OF INSIDE
expansion is taking place we can encounter another
{
OUTSIDE & 01, & 02, L 1, RECORD2
macro call or during one macro call another macro
&03, A 1 = F' 5'
call is invoked. In this macro bodies contain macro
calls and processes macro definition during INSIDE &02 ST 1, RECORD2
expansion time.
INSIDE &03
MACRO MACRO
{
INSIDE &01 L 1, RECORD3
Example 1 EXAMPLE 2 Call to
-- - - - -- Nested Example MEND A 1, = F' 5'
- 2
ST 1, RECORD3
MEND EXAMPLE! Macro Causes
Execution MIDDLE
Simple -- - - of DATAl, DATA2, L 1, RECORDl
macro Example
definition 1 DATA3 A 1, = F' 5'
MEND But not ST 1, RECORDl
MEND vice versa
DATAl DC f 5'
Fig. 3.3.3 : MACRO CALLS WITHIN MACROS
Fig. 3.3.5 : Example of NESTED MACROS
__.='
~
Ttdlluwldtt
h H l: l l l .ll ill tJ S
Macros & Macro Pro
3-7
Within the definition of the macro 'OUTSIDE' are ro call• within Macros
th Mac the order of the macro calls
ree separate calls to a previously macro 'INSIDE'. The use a stack to keep .
th Use ter table at each macro call.
of e MACRO 'MIDDLE' has shortened the length of th e Create a parame .
definition of 'MIDDLE' and thus has made It more easily ete r appears m the real Para ....
y param ·••ete
understood. Macro calls within macros can Involve several If a dumm d macro search the parameter tabi r
d f a calle ' . . h h
level. fiel o nd replace 1t wit t e approp e. Of
the calling macro a riate
real parameter.
conditional
assembly call
,
substitute real parameters evaluate the Boolean expression Find the appropriate Pop from stack
macro definition and set LP and
current parameter
write It to the output file y table pointer
Build parameter·table
LP= LP+ 1
LP=LP+1 LP = line number in
the statement Push LP and the pointer
to the last parameter table
onto the stack
y
N
return LP: line pointer
Fig. 3 .3.6 : Macro expander routine that allows macro calls within macros
------- ------ ------ ------ ------ ------ ------ ---1ti:~ ___. . ,,;
-:{,: T•~P7,',,•'
V ru11 ll t
.J System Prog. & Complier Const. (MU) 3-8 Macros & Macro Processor
MACRO
& AG DATA &C, &A2, &A3, & ARG2, &ARG 3
&AG D 2, &A2
AIF (&C EQ 1).FINI
(TEST if C = 1)
D 3, &A3
AIF (&C EQ 2).FINI
(TEST if C = 2)
MEND
START 1 D 2,VALUE 2
START 1 DATA 2, VALUE2,VALUE3 D 3,VALUE 3
START2 D 2,VALUE 2
START 2 DATA 1, VALUE 2
VALUE 1 DC F'O'
VALUE 2 DC F'l'
MEND
The macro time looping WHILE-ENDW Initialize register A depending upon &CODE.
IF (&CODE EQ 'R')
The While conditional statement is useful because It
allows us to repeat a block of code as long as a conditional CLEAR A
test is True, rather than just a simple number of t imes. It Is ELSE
useful in situations where we need to loop through code, LOCH &BUF
but do not know how many times we need to run the
ENDIF
portion of code.
Generate RD or WO instruction depending on &CODE.
E.g. of WHILE - ENDW Structure
$CHECK TD =X' &DEV'
MACRO
JEQ $CHECK
WHILE (NI= 0)
IF (&CODE EQ 'R')
N = N % 10
RD =X' &DEV'
N=N/10
STCH &BUF
SUM =SUM+ N
ELSE
ENDW
WD =X' &DEV'
ENDIF
MEND
______:..~~--- ---------- -.L...___,!M~E~N~D~ -----------""'.t,~:i• : ~, ~
..f System Prog. & Compiler Const. (MU) 3-10
Ma.cros & Macro Processor
For example,
JLT &DATA
)
&QUIT MEND
MEN~D~ - - - - - - - - - - -- - - - - - - - -
(b) RDCHAR
Fig. 3.3.8
In Fig. 3 .3 .a(a) recursiv e macro has defined.
the loop as end of record is found. 'Ne
RDCHAR in (Fig. 3.3.S(b)) calls recursive macro INPUT1 and INPUT1 exits from
can see th at macro &DATA is also called.
The proced ure EX PANO would be called when the macro was recognize . d d expansio n of macro begins.
an
this will cause problem because macro
When the end of RDCHAR will known, expansion of macro call would stop
PROGl
process or would 'forget' that It had been in the middle of the expansion of macro
such problem s as they allow recursive
Macro-p rocesso rs written In high level language (PASCAL OR C) does not have
macro calls. Example of recursiv e macro in C
End of
MACRO
{ MEND
MEND
'1 System Prog. & Compiler Const. (MU) 3-12 Macroe & Macro Processor
Let us call this macro with Able to identify dummy arguments In macro
DEFINE SIGN definition.
A new macro call ls created by
Syntax and semantics of all the allowed arguments.
SIGN ARG
and the generated calling sequence by macro processor
Ais. 3.5 Deelgn of Two Pan MACRO
BAL 3, • + 10 Proceasora
DC A(ARG)
20, = V (SIGN) The name two pass macro processor explains that the
L
19,20 macro processor algorithm uses two systematic scans
BALR
or passes over the Input source code,
3.4 lmplementatlon of Macro Processors All macro definitions are processed during the first
'fl~T•dl•..•••••i
Y Pub l lt a t t on s
~ s
;: ;-'~~,y;st~e~m~P;~~&~~~~~~~~~==~=~~=-=--~::::::::======:::::.::;::c,;;;.;;___--or
· mpller Const. (MU) 3-13
speclftc:atJon of OatabaS8S
Macros & Macro Prac-~
- ~
Fig. 3.5.2 : ALA during PASS 2 The next entry in MNT is indicated by the the
6.
Macro Name Table Counter (MNTC.
2. Macro Definition Table (MDT)
7. To replace the index marker entry for dumrny
The MOT consists of 80 bytes string per entry. MDT arguments before storing in MNT is stored in
stores each line of every macro definitions except
ALA.
the word MACRO. The MEND indicates the end of
the MACRO. 2. DATABASES Required for PASS 2
Macro Definition Table (80 bytes per entry) 1. Output from PASSl will be input to PASS2.
s. As each successive line Is read dummy arsuments In the macro definition are replaced with the positional Indicators
and stored In the ALA and MDT along with the MEND statements.
6. Control transfers to pass 2 In order to process macro calls when END pseudo-opcode Is encountered.
Macro
Prooo890r
No
MNTC = MNTC + 1
IGoto PASS 2 j
Construct Argument
List Array
MOTC = MOTC + 1
• Substitute Positional
Indicators for the
formal parameters
PASS2
.]
MDTI'+-MD'T
lndeX lrom MNT entrY
~ply expanded ~
"._. to ASSEMBLER
olfference t,etwe
3,7
Slbstitute arguments
t·,from Macro cal
It recognizes all the macro calls and their expansions. 6. As soon as END pseudo-op code is identified the
1. When a macro call is seen In PASS2 it checks the expanded source code is transferred to the 1. A procedure call
MNT to find the mnemonic for each Input. assembler.
or subprogram)
"'1•"1':",f'~~. f '. ,~- !.i.'/'' ,,
2. MDTP (Macro Definition Table Pointer) is used to ~ ~ e =--~ lr)',ty.,o·_p'a~s ,rnefcrd· processor nested macro 1'rforms a specll
initialize a pointer to the macro definitions stored in Ct· .definitfq_rj's are:not allowed. t.'-'t Is 1rouped
MDT given by MDT index field by the MNT° entry ~ tbe typical Ir
only. 3•6 Difference between MACROS and
3. Dummy arguments indices and their arguments to FUNCTIONS
the calls are stored in ALA.
1. A
. macro just re PIaces t he code assigned to it for e.g.
4. As each line is read, the values from the ALA are
substituted for the dummy arguments indices. if a macro 'Pl' · d
is efined with a value 3.14. Function
5. Expansion of the macro starts from the input deck as does not replace code but it performs what it is
MEND is encountered in MDT. supposed to do.
2.
- Macro are faster b II
overhead. :o~id:s~fu:n:c:tio:n~ca
-------------------1--~~~ ~~=~:e:ca:u:s:e~it~a:v
· ~ ~ ,~ ~
.r .
System Prog. & Compiler Const. (MU)
3-16
Macros & Macro Processor
3. No type restriction Is placed In arguments so that
one macro may serve for several data types. Note : The only difference between function and
longer definition. If a macro Is used twenty time Jn Difference betwNn Macro and
3.6.1
code In all Instances, the longer definition will be Subroutine
substituted In and thus code grow. A function
definition occurs only once.
I Q, HoW I, macro different trom llbroutlne ? (5 Marica) I
6. Macros do not have return type but a function has a
Macro
return type.
9. Macro does not alters the flow of execution but the Subroutine
function alters the flow of execution. Subroutines (FORM) can be called from both the
program they are defined In and other programs.
10. Macro calls a process at the translation time
function calls a process at the execution time. Subroutines can take any amount of parameters.
'Q., Explain difference'.·between.: R1ocedure ·calls and mac;ro c~lls. (10 Marks)
,,., ,, ii.
Sr, · ,., {.' 'ii' '.,' '1,1 ~•t ' I , { ' "f';"
. ,I).';,; t\,·,x.,I .• rlPto~,dure-C•ll~~ , ~lit, ,M . i'' Macro Calla
' ,,
'No. 'I ;II I I •, ;}/ r
" '
5 identlfied ~ 1. A roc;dure ca;I or subroutine (function, procedure, A macro Is a group of repetitive instructions In a
program which are codified only once and can be
;ferred 10 • o/ subprogram) or a sequence of code which
used as many time as necessary.
performs a speclflc task, as part of a~:;:;n~r~f;~
t.hat is grouped as one or more st
::3
CA O
s ,~
2.
3.
with the typical intention of doing one thing we 11 .
f Macro :
Sr. procedure ~ 11• synta>< O
No. fthe macro
5. Syntax of Procedure :
Declaration °
fthe macro .
code o
CALL Name of the procedure.
termination directive
Macro f the macro is done the following
The part which make a procedure are '
Declaration of the procedure
The declaration °
wav: MACRO (para meter 1, pararneter
Code of the procedure
Name Macro
Return directive
2...) ---------
-_JLT lo~n~o-f -th_e~p~r_o_
~e~r~m~in~a~t~
d
, e_ u_re_ _ _ _ _ __ _ Te:E><amP e
I of Macro Call ;
L6. Example of Procedure Call :
. . MACR O// Macro name
pos1t1on
Adding ProcNear; //Declaration of the procedure PUSH AX
Mov Bx, O; //Content of the procedure PUSH BX
Mov Bl, Ah PUSH DX
MovAh, 00 MOVAH, 02H
Add Bx, Ax MEND // End of Marco
Ret ; //Return directive
Add Endp ; //End of procedure declaration
I Q. 5
le.
Q.6 Explain conditional macro expansion with suitable examp
I
a. 1 Explain macro and databases used for one pass macro.
Introduction, functions of loaders, Relocation and Linking concept, Different loading schemes : Relocating
loader, Direct Linking Loader, Dynamic linking and loading.
- Assemblers solved the mentioned problem by allowing - Linkers does the address binding, assigning relative
the programmers to write programs in terms of addresses within each program, while the loader
symbolic names and binding the names to the machine assigns the actual addresses.
addresses.
- Linker also take care of complex name management
- The work of assigning the addresses is pushed off from and address binding. Linkers had to deal with object
the programmer to the computer. code generated by High-level programming languages
such as FORTRAN, PASCHAL, and COBOL etc.
- Loaders and Linkers has solved the mentioned problem
by loading and linking the program in the memory. - When program is larger than available memory, then
linker provides Overlay structure. It is a technique that
- Relocation and Library Search
let programmers share the same memory for different
o Mauchly (1947) has written about loading the parts of a program to, with each overlay located on
programs along with subprograms selected from demand when another part of the program called into
the catalog of programs stored on the magnetic it.
tapes and, try to relocate the subprograms' code
o They faded again as virtual memory appeared on
to reflect the addresses at which they were
PCs in the 1990s.
loaded.
Loaders a
nd
~~!S~y~s~te~m~P~rgogg.
- ;,_;&~C~o~m~p~l~le~r~C~o:gn~st~
. i!
(M~U~
) mmon code. Like every C program Use u~~1'
=====:-!4-~2;....=~::::=::~=::;
f :~::~~
0
Overlays are still used In memory-limited o co . tf s to1.1r
such as fopen, and prin . '~ei
embedded environments. Control sections
Wi th the advancement In hardware relocation and Read-only TEXT
Virtual memory, linkers and loaders actually became Writable DATA
less complex, because each program could again have
an entire address space. TEXT
Programs loaded, could be linked at a fixed addresses DATA
With the hardware rather than software relocation
taking care of any load-time relocation. TEXT
DATA
As multiple copies of the same program were
frequently run at the same time, compilers and Fig. 4 .1 .1 : Object Code for Multlple Sections
assemblers were modified to create object code In
multiple sections with one section for ready-only code Programs running under a GUI such as X Window
and another section for writable data. MS Windows all uses a piece of the GUI library. s, 0r
The code Is same among all running copies while other Most systems now provide shared libraries for
parts are unique to each copy. rograms to use, so all the programs that use a lib
P . rary
can share a single copy o f it.
The linker has to combine all the sections of each type
so that the linked program would have all the code In 0 Static Shared Library : The linker binds Prograrn
references to library routines to those specific
one place and all the data in another.
addresses at link time.
Control Sections 0 Dynamic Linked Library : Library sections and
symbols are not bound to actual addresses until
When different programs are running on a computer, the program that uses the library starts running
these different programs usually turn out to share a lot
hJIM'11M0
Q. Loading and !Inking scheme. (5 Marke)
1. Program translation.
BINARY
2. Linking of the program with other programs needed PROGRAMS
for the execution.
Loedllhe
3 Relocation of the program to execute from the LOADER p,ognun lnlC> the
specific memory area allocated to it. I : rNfflOrY
0
4 Loading of the program in the main memory for the ----+ Data now RAM
execution. -► Control flow
Aeaea
Loader
PROG 1
PROG2
LOADER
PROG2
PROG . ___. . ,
31-- ----:- ----.;i
PROG3
DATA
BASES
PROGN
PROGN
t
PROGRAMS
Souroe OBJEC T
, MODU LE 1 MEMO RY
Prog 1
,, E,cecu14bfe
,, f311111,Y
LOADE R PROG RAM
,, OBJEC T program
Souroo TRANS
Prog 2 MODU LE2
LATOR I
\ I
\
\ I
\ I
\ I
\ I
\ I
Source \
OBJEC T
Prog N MODU LE N
4.2.3~!!!!!!
Flg.:!.:.~~ !.~~~~
: Basic G~o~
schem e for LINKI~Je~c
f~O~b N ~t~M.::od:::.::u;:.:le:__s~ --- -=~ ~------ ._
- - - - - - - - - -~~~
- al related L Loaders perfo rm sever
Linkers an d
"Ill
of
Linking Is the process of combining various pieces conceptuallv separ ate tasks :
that can
code and data toget her to form a single executable Program loading : In thi~ a progr am_ is copied
fro111
editor
be loade d in the memo ry. Linker is also known as link the secondary storage into . a marn memory
for
to write
or binde r; many progr ammi ng languages allow us execution. Loading of data also involv es
tely. This
differ ent pieces of code, called modules, separa Allocation of storag e for data,
break a
simpl ifies the progr ammi ng task because we can
0
end linker put all the modules together. 0 Organizing data for virtua l memo ry to map virtual 1
addresses to disk pages . I
In additi on to comb ining modules, a linker also ; I
fore, Relocation : Duri~ g reloca tion a load addres
replaces symb olic addresses with real addresses. There by
only one is assigned to the differ ent parts of the program
we may need to link a progr am even if it contains in the progra m to show
adjusting the code and data
modu le. s of
the assigned addresses. When a progr am consist
a several subpr ogram s made from various
subprograms, all the subpr ogram s have to be put
at non-overlapping addresses so that they do not
conflict with each other . Linke r links all these
subprograms and creates one linked outpu t progra
m I
that starts from zero within th is the various I
subprograms relocated.
I
4.2. 1 Difference between Linker and Loader allocate space since overlap may occur or large
wastage of memory takes place.
Q, Explain difference between : Linker and Loaders,
(10 Marki) 2. Unkln1 : It combines two or more separate
object programs and resolve symbolic references
Llnker'9 Loadera between object decks. It also supplies the
Information needed to allow to reference between
A Linker or llnk editor Is a Loader Is a program that them.
computer program that loads machine codes of a
takes one or more object program Into the system 3. Relocation : It modifies the object program so that
files generated by a memory. It can be loaded at an address different from
compiler and combines the location originally specified and adjusts all
them Into a single address dependent location.
executable file, llbrary file, machine
4. loading : Physically It places the
or another object file.
Instructions and data Into the memory for the
Linker is a part of a llbrary Loader Is a part of an execution.
files. operating system.
4.4 Relocation and Linking Concept
Linker performs the linking Loader loads the program
operations. for the execution.
Computer programs comprises of several fragments
It also links the user defined Loading a program or modules all these fragments are need not be
functions to the user involves reading the controlled or contained within a single object file. In
defined libraries. contents of executable file such cases these modules are referred to each other
into memory by means of symbols. Typically, an object file has three
kinds of symbols :
Q. Explain Functioni ': g,: lo~der. (May 15;'5Marks) allowed to be called by other modules, also called as
public definition.
Q. Explain relocation.· , . (2 Marks)
2. Externally defined symbols : These are also
- The fundamental task of a loader is to
known as undefined symbols or external
o bringing an object program into memory and references. They call the other modules where these
symbols are defined.
o starting its execution
- An object program contains translated instructions and 3. Local symbols : It is used internally within the object
data from the source program. It also specifies file to facilitate relocation.
addresses in memory where these items are to be
loaded. - In addition to combining modules, a linker also
replaces symbolic addresses with real addresses.
To execute a program a loader performs the following
Therefore, we may need to link a program even if it
four functions
contains only one module.
1. Allocation : It is used to allocate space in memory
for the object programs. Translators cannot
J,,, Ttclu ■ lt~
V Pt1b t lt a l1ans
Loaders and .
. l1111ter,
• 4-8
• & Compiler Const. (MU) able cod e by
S Siem Pr ds and stores. The execut
Jumps, Ioa pass Wh itthe
need another relocation eri
linker mav ry.
is
Runtime
Llbra,Y
finally loaded into memo
...,,,es .
Unklnl Is of two.,,. .
Source (l) Static linking
program
(2) Dynamic linking
tines lJsed .
ti ''nklnl : It copies all library rou
Source Executable 1. Sta C
image · It require'"
Program program Bram Into the executable
the pro ry. s
more disk space and memo
llnklng concept
Fig. 4.4.1 : Relocation of es the undefined syn,bo/s
tput. It mlc llnkln1 : It resolv
the full library In the ou . h
- lin ke rs do no t Include led a
2. Dyna
ning.. T at m~ans that
the
ts from a group of files cal while program Is run
on ly includes the objec nced ntains undefmed symbols
Its symbols that are refere executable code still co
library. It only Includes in a li,praries that will Prov;d:
and arranges the objects plus a list of objects or
fro m other object files the
It has a feature to Involve definitions for the same.
'
program's address space. s to
umes a specific base addres
relocating code that ass requirements in segment
Relocation and · llnkJng
another base.
, it address
where an object will reside to reduce the relocation
- As compiler Is not aware ating Segment addressing helps
d base location. Reloc
oft en assumes a fixe olute requirement of a program.
olve re-targeting of abs
machine code may inv
., ..
.''·,
·',ottaet
Stateme·nt, ., • ; I
• ~
; ,;
Sr.No. • J
0000
0001 DATAl SEGMENT
0002
0002 ABC DW 25
0003 B DW ?
OS: OAMl
0044
END
.,,,.'!r.
.! system Prog. & Complier Const. (MU) 4-7 Loaders and Linkers
In this program, the ASSUME statement Is used to 4.5.1 Compile and Go Loader
declare segment register CS and OS for memory addressing.
Reference to A Is assembled as a dlsplacement of 196 from A compile and go loader Is one In which the assembler
the content of the CS register In statement 16. This avoids Itself does the processes of compiling then place the
assembled Instruction In the designated memory locations.
the use of an absolute address.
The assembly process Is first executed and then the
Linking requirement assembler causes a transfer to the first Instruction of the
Source
Program 2
Source Translalor
Program n 1 - - - -.i
__::~:::=-==--:__---:=-.:::~;~:i~~~;---
Fig. 4.5.2 : General Loading
Scheme
-___________
the starting of the
.by transferring control to rd t
-- ~ -- -- sto re on ca s inS ead of being
ex e;~ ::~ n this the data is
ate and go loader are . The object code is
All disadvantages of transl pro · emorv for execution h
overcome using this scheme sin
ce placed in mam mified locations in t e memory. At the end
ec . .
loaded at th.e spps to the specrfied location to begin the
translator hence less olute loader no
- Loader is smaller than the the loa. der 1um
f the loaded program. In abs .
wastage of memory. execution o cation is done. Absolute loader requrres single
.
every time the program linking or re1o
- Retranslation is not necessary pass operation, in that it checks
.
are used.
is executed since object decks Absolute Loader :
_ Single pass operation of
As the loader would be in
the same machine language Check H (Header Record) rec
ord to verify that
-
es in different languages 0
has been presented for loading
.
it is possible to write subroutin correct program
for the object decks. record, and move
Read each T (Text Record)
tructions and data to the
0 icated address in
- Loading involves binding ins object code into the ind
. Three approaches are
physical memory addresses memory.
used for loading. At E (End Record) record
, jump to the specified
o
Binding can be done address to begin execut
ion of the loaded
o Absolute loading :
(writing once).
beforehand, at compile time program.
ding is done at load time ader
o Relocatable Loading : Bin Algorithm for Absolute Lo
(Rewriting).
Start
g : Binding is postponed
o Dynamic Runtime Loadin read Header record
).
till execution time (Not writing Verify program length and na
me
read first text record
4.5.2 Absolute Loader
•M*dta ■ wh ile record type! = E do
begin
r fonn converts
loader s and mention if object code is in characte
a. Explain the design of absolute
it into internal representation
all data structures in detail. location in
(Dec. 14, 10 Marks) move object code to specified
memory
ich is used for Fig. 4.5.3 ord
The type of loader scheme wh s read next object program rec
. The loader simply accept
is called an Absolute Loader end
places it into main memory d
machine language code and E d
Jump to address specified in n recor
is similar to the "Compile And
specified by the assembler. It Stop
epts the machine language
~o" lo~ders. The loader acc
the core, then initiates
instructions and places it into
j
-
y System Prog. & Compiler Const. (MU) 4-9 Loaders and Linkers
Advantages
Disadvantages
loaded.
_ The programmer must specify to the assembler the address in the core where the program Is to be
absolute address
_ In case of multiple subroutines the programmer must remember the address of each and use that
explicitly in other subroutines to perform a subroutine linkage.
1001
OBJECT
PROGRAMS
ABSOLUTE
LOADER
1
:> READYFOA
EXECUTION
2001
MEMORY
the disk. The solution for this is to use a special type of a simple computer program activates a more complicated
program called Bootstrap Loader. The program's only job Is system of programs. In the startup process of a computer
to load software for the operating system to start. The system, a small program such as BIOS, initializes and tests
name bootstrap loader comes from the image of pulling that hardware, peripherals and external memory devices
one oneself up by ones bootstraps. are connected, then loads a program from one of them and
passes control to it, thus allowing the loading of larger
A bootstrap loader is a program that resides in the programs, such as an operating system.
computers EPROM, ROM, or other non-volatile memory
that automatically executed by the processor when turning
Bootstrapping
on the computer. The bootstrap loader reads the hard Actions taken when a computer is first powered on.
drives boot sector to continue the process of loading the
- The hardware logic reads a program from address O
computers operating system. The term boostrap comes
from ROM (Read Only Memory).
~ Ttdllualdti
V rub l 1<,11 11 ons
Loaders and Li
-~~~~~~~-!&~C~
o RO om~p~lle::r~Cons ~~t.~(~M~U~)b... ....:-....,_~4~-~10 2.,......,_.si,;;=-== -=~~~=::::;:~:~~n kera
ti bits are associated with each lnstrucr
M Is Installed by the manufacturer. Reloca on ld which Includes : ion or
d fie
0 ~M ~
oth contains bootstrapping program and some o Length of the entire program.
er routines that controls hardware
(e.g. BIOS). Length of the transfer vector portion.
0
Bootstrappln1 In • loader o operation performed by the Relocating Loaders.
assembles each segment independent!
Loads OS from disk into memory and makes It run. Assem bl er Yand
n to the loader, the text and the Inform .
passes o at,on
The location of OS on disk (or floppy) usually starts at for the relocation and the intersegment references.
the first sector
The BSS loader allows many procedure segments and
starting address In memory Is usually fixed to O
only single data segment.
0 No need to relocation The loader uses the transfer vector to solve the
This kind of loader Is simple problem of the subroutine linkage
0 No relocation The length of the whole program and the length of the
transfer vector are given by the assembler.
o No linking
Relocating loader will load all the information
° Called "absolute loader"
regarding subroutines and would place the equivalent
4.5.3 Relocating Loader addresses of the subroutines to each position of the
transfer vector. It will also load the transfer vector into
The task of relocating loader is to avoid reassembling the core.
of all subroutines when a subroutine Is changed and to
The output of relocation assembler using a BSS scheme
execute tasks of allocation and linking for programmer.
is the object program and the information about all
Relocating loaders adjust addresses of the executable code
other programs it referenced.
to compensate for variations in the address at which
If the relocation bit Is one, then the corresponding
loading starts. The computers which need relocating
address bit must be relocated otherwise the field Is not
loaders are those In which pointers are absolute addresses
rather than offset from the programs base address. Binary relocated.
Symbolic Subroutine (BSS) loader Is an example of Disadvantages
Relocating Loader. BSS performs all the four functions of The transfer vector Is used only for transfers and not
the loader. used for loadlng or storing of external data.
Allocation : It is done with the help of the program The size of the object program In the memory is
length and the information. Increased due to transfer vector.
Linking : It is done by transfer vector. The BSS loader processes procedure segments but
Relocation : With the help of the relocation bits. does not facilitate access to the data segments that can
be shared.
Loading : It is done by the loader.
wl•
V r Ttcl...-:','!r.
u1t1111
.i system Prog. & Compiler Const. (MU) ,4.11 Loaders and Unkers
i
a. Explain working of direct linking loader wtth example 4.5.4(A) O..lgn of Direct UnkJng Loader
s~owlng entries In different databaa11 built by DLL.
- Records of all the symbols referenced In the segment RLD cards are used to store those locations and
but not defined in the segment are also maintained. addresses on which the program content is
1 0 LOOPl START
4 8 L0START0
5 12 L0STARTl
6 16 DC B (L0START0)
7 20 DC B (L0STARTl + 10)
8 24 DC B (L0STARTl- L0START0- 2)
9 28 DC B (LlSTARTl)
10 32 DC B(LlSTART0 + LlSTARTl + 5)
11 END
u T1cll11eal1q1
.
. . P ut1 11c1 t1ons
• Loaders and LJ
~ System p 4-12
llker-e
~rog. & Compiler Const. (MUJ
-
Source Rt lltfvt -
s,mplt program
12 0 L1START1 START -
13 ENTRY
-
LlSTART0
15 4 llSTART0
16 14 DC A (LOSTARTO)
17 18 A (L0STARTl + l0)
DC
18 A (L0STARTl - LOSTARTO- 5)
22 DC
19 END
d L1START1
SAMPLE PROGRAM FOR LOOPi an
Object deck program for LOOP1
ESD Records
08 -
2 L0START0 LD 02
2 L0STARTl LD 02 12 -
3 LlSTARTl ER 03 - -
3 LlSTART0 ER 03 - -
Where,
TXT Records
Source Card Reference ' Relative Address Content What the assem61er did ?
6 16-19 08
7 20-23 22 12 + 10 = 22
.. .. ·8 24-27 02 12-08-02 =02
-·
9 28-31 0 Not known to LOOPl
10 32-35 +S
(5) LlSTART0, llSTARTl NOT
KNOWN TO ASSEMBLER.
• ,,....
S tern Prog. & Compiler Const. (MU) 4-13 Loaders and Linkers
RLDRecord1
Variable Source card ESD ID Len81h Atllhmetk Aelltlve
Name R1f1rence (by k) OptrltOr Addreu
L0START0 6 02 4 ♦ 16
L0STARTl 7 02 4 ♦ 20
LlSTARTl 9 03 4 ♦ 24
LlSTART0 10 03 4 ♦ 28
LlSTARTl 10 03 4 ♦ 28
ESD Records
13 LlSTART0 LD 02 04 N/A
TXTRecords
·.•
Source Card Relative Content ,. What the assembler did?
, I
Reference Address
'
16 14 0 0 LlSTART0 Not known
RLD Records
LOSTART0 16 03 4 + 14
L0STARTl 17 03 4 + 18
L0STARTl 18 03 4 + 22
L0START0 18 03 4 - 22
*
V
T11Hat11ldlf
fl ub 1; 1.111 to n s
Loaders a
. & Com lier Canel. MU 4-14
Global Exte (II) pASS 2
rnal Symbol Table (GEST) l. output of pASSl ls used as Input for PAs
GEST Is used t O . The Initial Program Load Address Pa s2.
correspond lns cor :tore the external symbols and their 2
the SD or LO ent e 8 dress In table which are defined by (IPLA) from PASSl rallleter
ry on an ESD card. Program toad Address Counter
3. Prepar
12 bytes per entry pA5Sl. ed b-i
◄ ... GEST prepared by PASSl.
-
4.
External Aaalgned Co,- s. The corresponde nce between the
symbols core address and name m .external
•Ymbol AddrNa . . ent1on
ESD and RLD card s Is given by Loeai E ed on
.. lOOPlbb"
100 symbol array. llterllil
"l0STARTO bb" 120
"l0STARTl bb" 130 4.s.4(D) Algorithm and Flowchar t for Dir.....
"llSTARTl bb" Linking Loader ...1
150
"llSTART0 b" 180 PASS : Gtves the definition of symbols and allllh..
1
Local External Symbol Table(LES segments -.."
A)
The Relation b t It gives the value of all the external svmb0 15
number on th R e ween the core address and the ID their assigned location for every segment 1 and
e LO card Is given by the LESA. · · n th
beginning of PASSl PLA rs the IPLA. An object cod _ e
Aaalgned cor.e addre•• of read and used for PASS 2. e ts
correspond ing ~ymbol
, . The record may be one of the five types ESD TXT
- ,. " ~
PAS81
Pi.A+- IPl.A
__...,..
r-r--_-_-_-___ Road Catd
Wr1leoopyof
a.rd 1or Pa• 2
~~GotoPa•2
i:>LA +- PLA +
SLENGTH
ER
Value+-PLA
No Yes
In PASS 2 the control is transferred to the loaded program by the loader after all the segments have been assigned
locations and the external symbols have been defined by PASS 1.
(1) At starting of PASS2 PLA is the IPLA same as PASS 1 and the execution start address (EXADDR) is set to IPLA.
Tn,ns fer to
1.c,oellon EXADOR
No
(-)
Flag
OVERLAY STRUCTURE
(U888 only 70k of memory 01
101,al 110k memory)
PROO 2 (20 k)
________,l
pAQ06(10 IC)
PROO 3 (30 k) PROO 5 (20 k)
0
PROG 1
20k
40k
70k
80k
~~~:;=;=====~================~==-.~~~=~.=::-==-=------------
Fig. 4.5.7: Storage Allocation of Each Procedure
Note : Proceduref eRqG 2.arid PR00•4- are never ·ln Dynamic linker Loader a special part of an operati
use at the same time; neither aie c and E. . · system that loads external shared libraries into a runnir,,,
, '
process and then binds those shared libraries dynami;
If we load only these procedures that are actually be
to the running process. This approach is ~
used at any particular time, the amount of core needed ls
called dynamic llnklng or late llnklng. It retrieves the
equal to the longest path of the overlay structure or flipper.
addresses of functions and variables contained in the
Here it happens to be 70K (Procedures PROG 1, PROG 2,
and PROG 3). library, execute those functions or access those variables,
and unload the library from memory.
Advantages (1) It Is often used to allow several executing programs
- No wastage of memory. to share one copy of a subroutine or library. Fore.~.
run time support routines for C could be stored~
- A system can be dynamically loaded whenever it is
Dynamic Link Library. A single copy of the routines~
required.
this library could be loaded into the memory of the
4.5.6 Dynamic Linking Loader computer. All C programs currently in executb'l
MU - Dec. 15. Dec. 19 could be linked to this one copy, instead of lin~nga
separate copy Into each object program.
a. EXplaln 'De~lgn\ et' DYhamic tfnki~g ,,Loa'oer. along
· ··w~h exampte·: · . , . , . (q&c,•15, 10 M~rki) (2) It provides the ability to load the routines on~ wtieo
cf , Explain dynamf~ii~kln9 l~ader l~etall. _ •: · ' . they are needed so lot of time and memorv is sa'led
. . (Dec. 19, ~OM~ks) if subroutines are large with lots of external
references.
.t system
(3)
Prog, & Compiler Const. (MU)
.
before the dynamic linker to work it actually
has to load the libraries.
loading happens.
.,.......
• , , , 11c 1 t 11 ns
~ Svatem p .......
'""· & Compllor Con ■ t . (MU) 4-20 loaders and lJ
l"l~
What Is loader ? Explain the four bas· el'\
0.2 IC ftin
OYftamlo Loading Dynamic Unklnll loader. CIJ()l"la Qf
No
E.xplaln the different types of loaders.
3. 0.3
Dyna mic loading Is Dynamic linking Is
concerned with concerned with linking Differentiate absolute load~r versu
0.4 s re1
load Ing the library routines at run- loader starting who performs which fu . aca~.
of
. nct,on .1-..ig
subroutines of a time Instead partJcular loading scheme. 11 llie
progr am as and when combining them with
required during run- the elCecutable program Explain the relocation and linking conce
code before the 0.5 Pts.
time, Instead of
program execution Describe the working of a Direct linki
loading all the 0.6 ng lo
starts I.e. static linking.
subroutines at once E>cplaln In detail the various data structur ~ -
To achieve this, a small es Ulle(j_
before the program code call 'stub' is
execution starts. 0.7 What Is Bootstrap Loader? Explain.
Inserted in the program
code wherever library
0.8 E>cplain Dynamic Loading and Linking With the h
routine Is required. The
stub contains of example. ~
information about
0.9 At what point in time do each of th8
where to find the follo'tling
routine If it is not loading schemes perform binding ?
already in the main
a. BSS Loader
memory.
b. Direct Linking Loader
4. Efficient use of Efficient use of memory.
memory. c. Absolute Loader
5. It not require
does Dynamic a. 10 Explain the Compile and Go Loader scheme. Also
special support from Linking requires special give Its advantages and disadvantages.
Operating system; it is support from operating
the responsibility of system, the routine a. 11 What Is loader ? Explain the loader functionality am
the programmer to loaded through dynamic Its components in detail.
check whether the linking can be shared
routine that Is to be across various Q.12 Explain the design of direct linking loader
loaded does not exist processes.
in main memory.
a. 13 Write a short note on dynamic loading and dynani:
linking.
Q. 1 What is difference between loader and linker ? Give a. 15 Explain the relocation and linking concepts.
one example of each.
-:□ o