0% found this document useful (0 votes)
29 views247 pages

System Programming and Compiler Construction Techknowledge Book PDF

The document provides an overview of assembly language programming, including its definition, advantages, and disadvantages. It explains the role of assemblers in converting assembly language into machine code and outlines the basic structure and functions of an assembler. Additionally, it discusses the components of assembly language programs, including imperative statements, declaration statements, and assembler directives.

Uploaded by

SYA63Raj More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views247 pages

System Programming and Compiler Construction Techknowledge Book PDF

The document provides an overview of assembly language programming, including its definition, advantages, and disadvantages. It explains the role of assemblers in converting assembly language into machine code and outlines the basic structure and functions of an assembler. Additionally, it discusses the components of assembly language programs, including imperative statements, declaration statements, and assembler directives.

Uploaded by

SYA63Raj More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 247

~

• :.:. ~l"'4i illl...


;.M_a
Assemblers

e, pass structure of assembler. Assembler


Elements of Assembly Language programming, Assembly schem
bler, Design for Hypothetical / XB6 family
Design : Two pass assembler design and single pass Assem
processor, data structures used.

Assembly languages are specific to a given micro-


2.1 Introduction
processor/computer and hence are not portable.

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

It helps to get the better performance out of the


processor as posslble.
Linker ,___ _,,
It helps us to gain access to specific characteristics of
the hardware that might not be possible from the
higher level language. Fig. 2.1.2 : Workin g of an Assem bler

2.1.2 Disadvantages of Assembly Language

It always requires the use of an assembler to translate


a source program into an object code.
.t System Prnn & Co
- - - •..-~~;:,~·~:m~m~le~ r~Con=•I~ A
.~(M~U~)_____J2~-2~ --=-======= ===~ss~e~lllb Je~
2 2 18
--·- - E-.:
2' 2
:.::m.::e~n~ta~o~f~an~Aa~•~e~m~bl~y~La~n~g~u~ag~e~P~ro~g~ra~m~m.:::1::."g~-
:. -- - - - -
~
'
•1 Baile Feature of an Alaembler
n An Assembler translates the mnemonic operation codes to their machine language equi~lents and resolves SYlllbo .
ames for memory locations and other entitles. Assigning machine addresses to the symbolic saves tedious calcular lie
and manual address updates after
program modifications has been done. '%
Assemblers provide three basic features which simplify and ease the programming.

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

E.g. MNEMONIC (DESTINATION), (SOURCE)


L General format of mnemonic
MOV EAX, 1
l l l
Mnemonic Destination Source

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

L 3,ELEVEN Load the number eleven Into register 3

LOOP L 2, DATA (6) Load data Into register 2

A 2, FORTYSIX Add 46 to register 2

ST 2, DATA (6) Store updated value of the data

A 4, SIX Add six to register four

BCT 3, LOOP Decrement register 3 by 1, If result non-zero, branch back to loop

BR 14 Branch back to caller

ELEVEN DC F '11' Constant 11

SIX DC F '6' Constant 6

FORTYSIX DC F '46' Constant 46

DATA DC F 'l, 3, 3, 3, 4, S, 8, Words to be processed


9, O'
END
The programmer is not aware where to load the program and from where to start the program. The BALR Instruction
is used for loading the base register.

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

It tells from where the beginning of the program is to be done.

4) END

It tells where the program is to be 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

offset Base Register Value


It means A number is to be added to the contents of register 3.

2.3 Basic Assembler Functions

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.

2.3.1 Essential Assembler Functions

Mnemonic opcodes are converted into equivalent machine instructions.

Symbolic operands are converted into machine addresses.

Machine instructions are building to perform operations.

Data constants are transformed Into Internal representations.

Object program is obtained.


------- ------- ------- ------- --:.:;;- ;;,-;;.. ,..;;;-;
V
;fl pu 11 11r1 111 • '
t System Prog. & Compiler Conat. (MU) 2-5 ~rl

Source • Mnemonloa opoodl OBJECT


Program __...,.. • Symbol __ ...._ ASSEMBI.EA
OODE
• Dela Conllanta

Fig. 2.3,1 : BMlo ....mb..r functlone

2.3.1 (A) Aaaembler Directive,


l,'IJ [i,-• 1 '1

a. Explain different oaembler dlraotlvea With example. (ON. 19, I Mllfb)

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

Reserve space In memory for unlnltlallzed variables

Control the appearance of listings

o Initialize memory

o Assemble conditional blocks

o Define global varlables

o Specify libraries from which the assembler can obtain macros

o Examine symbolic debugging Information

2.4 Assembly Scheme

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 are not responsible for managing their dependencies.

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

Fig. 2.5.1 : Assembler Algorithms and Data Structures

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.

LOCCTR f-- LOCCTR + (instruction length) Enter addresses from LOCCTR.

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.

PASS 2: Translate op-code into machine


language. 2.6 Assembler Design
2. LOCCTR : It includes 2,6, 1 Forward Reference Problem
It initialises the beginning of the "START" address. MU - Dec. 14, May 19

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

, Synthesis phase includes :


(a) Obtain the machine opcode corresponding to
In this there are two reference to myValue before it is the mnemonic from the mnemonics table.
declared. (b) Obtain addresses of a memory operand from
the symbol table.
(c) Synthesize a machine instruction.
Fig. 2.6.1 shows the two phases of an assembler.
MNEMONIC OPCODE Length
MOV . 01 1 FIXED
i--A_D_D_,..__02--+-----t TABLE
I •.

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

2. 7 Two Pass Assembler Design


MU - May 14, May 15, Dec. 15, May 16, Dec. 16, Dec. 17, May 19, Dec. 19
0. , . What are advah1ages of assemblers with multiple passes,17.1-jqw many: passes should be 1her~ i_ a
n typical ~mpi{er ?
, , . . · · ·\ 1 , , · . . · , ,~J¼. :,~, .(M!Y,~-~,~ fJlarks)

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.

Multi pass assembl


er does the work in two pass
First Pass :
0
Scans the code.
0
Validate the tokens.
0
Create Symbol Table.
Second Pass :

Solves forward reference problem.


0

.
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

Machine Opcode Table (MOT) assembly process.


altered during the
tents of these tables are not filled in or
MOT is a fixed length table I.e. the con
_j System Prog. & Compller Const (MU) 2-9 Assemblers

Table 2.7.1 : Machine-Op Table (MOT) for pus 1 and pan 2


&-bytes per entry .
~

Mnemonic op-code Blnaryop-code Instruction Instruction Not used In


(4-bytes) (l•byte) len,th(2·blls) format(3-bfu) thlsdesl,n
(characters) (hexadecimal) (binary) (binary) (3-bfu)
"Abbb" SA 10 001
"AHbb" 4A 10 001
"Albb" SE 10 001
"ALRbN lE 01 000
"ARbb" lA 01 000
... ... ... ...
"MVCb" D2 11 100
b~represents the character "blank"

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

Pseudo Opcode Table (POT)


POT is fixed length table i.e. the contents of these tables are not filled in or altered during the assembly process.
Table 2.7.2: Pseudo-Op Table (POT) for pass 1 (slmllar table for pass 2)
8-bytes per entry
Pseudo-Opcod,e . Address of routine
(S•bytes) tq process,Pseudo-Opcode
(character) (3-bytes =24 bit iiddress)
"DROPb" PlDROP
"ENDbb" PlEND
"EQUbb" PlEQU
"START" PlSTART
"USING" PlUSING
l These are presumably Pl~PASSl
lables of routines in pass 1;
the table will actually
contains the physical addresses.

'!!!l"e TtcHu11ltllti
V Pubtll-1Jt1ons
lier Const. (MU) 2-10

Symbol Table (ST)


f ••• 1 and pass 2
Table 2.7.3: Symbol Table (ST) or p
~
l4-bytes per entry
Relocation
Symbol Value Length
(1-byte)
(&-bytes) (4-bytes) (1-bytes)
(character)
(characters) (hexadecimal) (hexadecimal)
"R"
"JOHNbbbb" 0000 01
"R"
"FOURbbbb" OC>OC 04
"R"
" FIVEbbbb" 0010 04
"R"
"TEMPbbbb" 0014 04
Symbol Table Includes for each entry :
(i) Name of the symbol

(ii) Symbols assembly t ime value


(iii) length (in bytes) and

(iv) Relative location indicator (R/A)


th
Operand can be either immediate (typically one byte values coded in the in st ruction itself/)or e addresses of the
data located elsewhere
(Relative/Absolute value)
R - Relative value

A - Absolute value

Literal Table (LT)

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.

literal Table includes for each entry

(i) Name of the literal

(ii) literal assembly time value

(iii) Length (in bytes) and

(iv) Relative Location Indicator (R/A)


(The format is same as that of symbol Table - with required changes) based on the definition of the Literal table.
Name of Literal Value of Literal Length of Llteral Relative Location Indicator
(1-byte) (4-byte) (1-byte) (1-byte)

Base Table (BT)


It is used by the assembler to generate the proper base regist er reference in machine instructions and its correil
offsets.

--------- --------- --------- ---- ---~Jae-;:~~~~ . , ,.,~ t t



.:r...
.
System Prog. & Compiler Const. (MU)

____
.,..
2-11

Tablt 2.7.4: 8111 Tablt (BT) for pan 2


Assemblers

4-bytes per entry


Avallablllty Indicator Designated relat ive-address '"

(1-byte) Contents of base re11sttr


(character) (3-bytes :s 24 bit address)
(hexadecimal)
1 "Nu -
2 "N"
:
- 1
15
14 "N" - entries

15 "Y" 00 00 00 l
Availability

Y ~register specified in USING pseudo-opcode

N ~register never specified in USING pseudo-opcode or subsequent made


unavailable by the DROP pseudo-opcode
Location
Counter
LC

Pseudo
Opcode
Table PO
Source
Program

Symbol
Table
ST

Ltteral
Table
LT

Base
Table
BT

Listing

Fig. 2.7.2: Shows the use of databases by two-pass assembler

(IV) Databases Required for Pass 1


1) Input source program.
2) Location Counter (LC) to keep track of each instructions location and to assign address for each symbol
defined.
* T1cHu ■ ld1
..,, P -.b l l l l l I or
i
s
S stem Pr & Co 2-12 . . ~ l..
• mpller Const. (MU) •c for each instruction and its~I
1,olic mnemonr enRth
3) Machine Operation Table (MOT) that indicates the sym d the action to be taken in pass 1 ·
do-Opcode an .
4) Pseudo Operation Table (POT) that Indicates lhe pseu d' g value.
and Its correspon rn
S) Symbol Table (ST) that Is used to store each label d its corresponding assigned locatio
ountered an n.
6) Uteral Table (LT) that Is used to store each literal enc
71 A copy of the Input to be used later by pass 2.

(V) Datab11e1 Required for P111 2

1) Copy of source program Input to pass 1.


2) Location Counter (LC)
3) MOT that Indicates for each instruction
(a) Symbolic mnemonic
(b) Length
(c) Binary machine op-code
(d) Format (RR, RX, ... ) b
. onic and the action to e taken in pass 2
4) POT that indicates for each pseudo-opcode the symbolic mnem ·
d its corresponding value.
S) Symbol Table (ST) prepared by pass 1, containing each labe1an
• I ountered and its assigned location.
6) Uteral Table (LT} prepared by pass 1, containing each htera enc
'fl d base registers by USING pseudo-opcod
7) Base Table (BT} that indicated which registers are speer e as es a~
what the specified contents of these registers are.
INSTRUCTION workspace which is used to hold each instruction as its various parts is being assembled
8)
together.
9) PRINT LINE workspace used to produce printed 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.

(VI) PASS 1 : Define Symbols and Literals


First pass assigns a location to each Instruction and data and it also defines the value for each symbols appearingin
the label fields of the input program.
1) Set location counter to O.
2) Read the input program.
3) Opcode is checked to find whether it is pseudo-opcode or MOT to locate the match for source statements.
a. If the entry found in MOT is matched then the MOT entry gives the length of the instruction.
b. Operan~ field is used to check for the presence of a literal and these literals are stored in literal table fO!
processing.
c. After literals label ~eld Is checked for the presence of a symbol. If found th I in the sym~
table with the existing value of the location counter. ' enter e symbo
d. At last location counter is incremented by the length of th · .
e rnstructron.
e. Copy of input source deck is made available for use by pass 2.
_t system Prog. & Compiler Const. (MU) 2-13

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

DLENGTI"! Assign value


to symbol In
Process any literals, label field
STSTO
enter into literal table
LTSTO On to Pass2

Assign current value


Yes
of LC to symbol
1 sso

Write copy of card


on. file for use by pa
WRITE1

Fig. 2.7.3 : A flowchart for pass 1 of Two Pass Assembler

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~

for later process ing by the loader. code.


nd byte contain s two 4-bit fields
The secO , each
l) Locatio n Counte r Is lnltlallz ed. which encodes a register numbe r. Of
2) Statem ent Is read from the copy file created by passl.
The th .rd and fourth byte contain s a 4-bit .
I regist
3) Opcod e Is checke d to find whethe r It Is pseudo- number and 12-bit displac ement, used to specify t er
dress for the operan d in storage he
opcode or MOT to locate the match for source memorV ad ·
statem ents.. If the entry found In MOT Is matche d Registe r Storage
operand s operand s
then the MOT entry gives the length of the 1 3 2
Instruc tion.

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

[oP f~I ~ 1112 1516


sz I I02
1920 31
0 78
1) System Prog. & Compiler Const. (MU) 2-15 Assemblers

Processing of Pseudo-Opcodes

Pass2

Read card
.--- -from Ille co
READ2

Found . - - - - - - .
Search

EQU USING DROP ENO


START
Indicate
Search Evaluate base reg.
machlne-o table rand field no. unavallable
From constant
MOTGET EVAL BTDROP
and Insert in
assembled
Get op-rode byte program
and format code, DCGEN Enter base
L +-length reg. no
L +-length and value
ss of data field into base
DLENGTH, table
RS BTSTO
SI A
Evaluate both register Evaluate register and
expressions and Insert Index expressions and
Into 2nd byte Insert Into 2nd b te
EVAL EVAL
Generate literal
for entries In
"Punch" assembled Calculate effective Literal Table
Instruction · address E rand LTGEN
PUNCH

"Print" assembly Determine appropriate


A STOP
listing line displacement and ba
register D + C(B) = EA
BTGET
B - Base
D - Displacement
Put B and D Into
byte 3 and 4

Fig. 2.7.4: Flowchart for pass 2 of Two Pass Assembler

i) DS and DC ii) EQU

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,

Ex. 2.7.1 : For following oode what wtll be output


generated by Pass-I and Pass-II for two
FOUR
ST

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

L 1, FIVE counter is increm ented by 4.

A 1, FOUR The next instruc tion is an Add instruc tion. We look up


1, TEMP the opcode but we do not know the offset for tour.
ST
The same thing happens with store (ST) instruction.
DC F '4'
FOUR
The DC instruc tion is a pseud o-opco de directing us to
DC F '5'
FIVE define some data.
TEMP OS IF
Pass2
END
The above mentio ned progra' m was assume For 'fou r' we have to produc e a '4'. This word has
d to be
called by anothe r program that left the address of the first been stored at re1at1ve
• locatio n 12, from the pass 1. So we
instruc tion In register 15. will give the v aIue 1 2 to the Load (L) instruc tion in pass 2·
Same• case is with Ad d and store instruc tion and
their

-----------------------------~=-=:=~~~:_:.~~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

(Ill) The address of the operand is added to the


2.e One Pass Assembler
forward references associated with the
Hi ■ •i4Ms SYMTAB entry.
a. Explain one pass assembler with ffowohart and (Iv) As soon as the definition for the symbol is
respective databases. (Dec. 14, 10 Marki) found, scans the reference list and inserts the
a. Explain with suitable flow chart working of single addresses.
pass assembler. (10 Marks)
(v) At the end reports the error if there are still
Q. Explain the ,working of single pass assembles. Show
SYMTAB entries indicating undefined symbols.
the structure Of Its databases used. (1 OMarks)

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

(d) Forward JUMP to instruction items cannot be easily Word 2 16


eliminated.
Double word 4 32
Insert (label, address-to-be modified) to SYMTAB
Quadra word 8 64
Usually address-to-be-modified is stored in a
linked list.
Tetra word 10 80

(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.

Table 2.8.1 : Declaratlve statements


~
(1)
L ad a segment register with the segrnent b
o ase.
(ii) Let the assembler ~now what address a
Sr. Declarative Description Length
segment register contains.
No. statements
1 DB Reserve a byte 1 DB : This pseudo-opc ode is used to define bytes.
(c)
2 ow Reserve a word 2 (d) ORG : This pseudo-opc ode is used for the

3 DD Reserve a 4 manipulation of the value of the location counter.


double word
(e) EQU : It indicates whether the symbol is defined With
4 DQ Reserve a 8
EQU statements.
Quadra word
5 OT Reserve a Tetra 10 (f) END : It indicates the end of the segment.
word
4) Format of Databases
3) Assembler Directive Statements
(a) Machine Operation Table (MOT) : The MOT ~
Q. What is the use of ORG JORlqlN),7 (3 ~ark~)
organized and contains opcodes for assembly

(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

Table 2.8.2 : Format of MOT

Mnemonic Machine Alignment Routine


opcode (6) opcode (2) Information 1 (Id)

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

----- ----- ----- ----- ----- ----- ----- ----- --=--:-.::~--·•"


ii!~~·••'
System Prog. & Compiler Const. (MU) 2-19 Assemblers

(8) ( 1) (2) (2) (2) (2) (2) (2) (2) (2)

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

Fig. 2.8.1 : Symbol table


The segment in which the symbol Is defined is given by the owner segment field which contains the SYMTAB entry.
EQU symbol type indicates whether the symbol is to be given a numerical value or text value.
(c) SEGMENT REGISTER TABLE : (SRTAB) whenever an assembler encounters ASSUME statement it stores the previous
SRTAB on the stack and new SRTAB is created.

Table 2.8.3 : Segment register table


SEGMENT REG. SEGMENT NAME

(d) STORED SEGMENT REGISTER TABLE (STSRT)


Table 2.8.4 : Stored segment register table
Segment Register Segment Name
00 ES
01 CS (Code segment)
10 SS (Stack segment) } STSRT#I

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)

It contains the address of the instruction making the forward reference.

-:!= 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

Read Next Statement

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

Put Into target


code buffer

Enter SRTAB # · Clieck alignm ent and


lnstm address alignment addres sfbility. Gener ate.
code and stmt # In FAT sag,' tiaae offset tor symbo l

Enter In P roce~ forwar d


SY~TA BCRT refere nces

Yes Report undefined


Produc e Cross-
Symbols from EARTAB
referen ce llstlng

Fig. 2.8.2 : Flowchart of the Sin I8 P


- --- --- --- --- --- --- --- --- ..: 9: :_:..:_,:a:ss:_:A:s~s~
e~m~b~le~r~-------~:-::-:-:::
. -~ ...-;·1 0"'
-=Ior~
V r ub ll l '
y System Prog. & Compiler Const. (MU) 2-21 Assemblers

(VI) Example of One Pass Assembler using varlou ■ Datbue ■

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

Symbol Table Entries for above statement (SRC statement 1)


,,
SYMBOL EQU SEG. DEFINED. :TYPE ., , OFFSET QWNERSEG. LENGTH . SIZE SOURCE PTA. PTA. PTA •
(YIN) REG: (YIN) ' ' , . IN SEG. i;, (2) .. ' ' ( ) SM I TO TO TO
•·
2 -,
(YIN) (2) 1ST LAST · CRT 1ST CRT ENTRY (2)
' L
I, >, _.'· l a
FAT ENTRY (2)
. <
1: ,· I •. ENTRY -.
/ ,, -'
. : ' (2)

,1 XYZ N y y lALIGN-MENT 0000 #1 ... 1 NULL NULL NULL


INFO

Next Statement (SRC Statement 2)


2 : : ASSUME CS : XYZ (# 1),
OS : PQR (# 2),
ES: LMN (# 3)

SRTAB Entries for above statement

SRTAB

SEGMENT REG. SEGMENT NAME '


ES (OO) #3
CS (01) #1
ss (10) --
OS (11) #2

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

Forwar d Refere nce Table : (Mainta ined 88 Stack)

PTRto NXT ENTRY# In INSTRUCTION USAGE CODE SOURCE

FRTENTR Y STSRT ADDRESS (1) STMT#

(2) (1) (2) (2)

STACK TOP #1 0006 XXX 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

90 0020 ASSUME OS : LMN (# 1),


ES : PQR (# 2)

SRTAB Entries for above Statement

SEGMENT REG. SEGMENT NAME


ES (00) ~ #2
cs (01) #1
ss {10) --
DS (11) Jj,i #3

Stored Segment Register Table Entries for Statements

SEGMENT REG. SEGMENT NAME


ES (00) #3

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

Sample program First Pass

Relative Mnemonic
PRC START
Address. Instruction
USING*, 15
0 L 1, -(0,15)
L l,FOUR
A I, FIVE 4 A 1, -(0, 15)

A 1, = F' 2' 8 ST 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 ~
-

~Relative Address What features of assembly language required


Mnemonic Instruction a. 1 build a multi pass assembler? us to
0 L 1, 12 (0,15)
Define the following terms.
Q. 2
4 A 1, 16 (0, 15)
~
a) 1mparat1Ve Statements
8 ST 1, 20 (0,15) b) oeclaratiVe Statements
12 4 c) Assembler DlrectiVes

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.

16 04 R b) Show the literal tables at the end of pass 2.


FIVE
R c) Show the changes in the base register table
TEMP 20 04
during pass 2.

d) Show the generated "machine" code from


Literal Table
pass 2.
Value Length Relocatable
Literal SIMPLE START
04 R BALA 15,0
::: F'2' 28
USING ·, 15

ease Table LOOP L R1, ,WO


Contents Of Base Reg A R1, ,WO
Availability Indicator
ST R1, FOUR
N FOUR +3, 4
0 CL1
N BNE LOOP -
1
BR 14
-
R1 EQU 1
---
TWO DC F '2'
---
FOUR F
OS ----
- y
00
END -

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. 12 Give the analysis and design of two pass assembler DC 10H


with respect of flowchart, data structures and
NUM1 DC 20H
algorithms.
NUM2 OS ?
Q. 13 What is assembly language ? What features of
assembly language required us to build a two pass RESULT END
assembler?

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

. "large" or II w1"de") . A macro is a


3.1 Introduction M ro (meaning
ac . h h s been given a name. Whenever the
block of code whic . ~ replaced by the interpreter or
· invoked it ,s .
A macro processor helps us to define and use name is tents of the macro. A single line
macros in an assembly level language. During macro •1 by the con
comp, er f statements is called macro and it is a
definition a group of statements is provided that we want meaning for group ~ for the programmer. Macro
to associate with a macro name. The assembler replaces notational convenience
a
instruction rep 1aces macro with .a group of. statements

the macro name with the statements specified in the macro
whenever t hey are called This activity of rep 1acing the code
definition. Macros provide several advantages when writing ·
assembly programs . is called expansion of macros.
In complied languages, macro-exp~nsion happens at
- The frequent use of macros can reduce the
programmer-induced errors. A macro allows us to comp1•1e t·1me• Thus macro instructi . ons allow the
programmer to write a shorthand version of a program,
define an instruction sequences that are used
and leave the mechanical details to be handled by the
repetitively throughout the program. This reduces the
likelihood of errors introduced in repetitive macro processor.
programming sequences. - The purpose of macros is to either automate
frequently used sequences or enable a more powerful
- The scope of symbols used in macro is limited.
abstraction.
- Macros are well suited for creating simple code tables - Macro processors are viewed as an extension of the
- Macros make an assembly program more readable. basic assembler algorithm.
The assembly language programmer often finds - Macro processors are not directly related with the
necessary to repeat some blocks of code many times. The architecture of the computer on which it is run.
block may consist of series of operation like addition,
- It is used with high level languages, operating system
subtraction etc. again and again. In this situation the
command languages.
programmer will find macro instruction facility very helpful
and useful. - Macro replaces each instruction with the
corresponding group of statements. For e.g.
3.2 Macro Definition and Call - SIC requires a sequence of instructions to save the
MU - May 19, Dec. 19 contents of all registers . This can be done by writing
one macro like save registers etc.
Q. Explain when wlll a macro be used in a program? :.
(5 Marks) 3·2·1 Functions of
MACRO Instructions
Q. Define macro. (2 M~rks)
The macro processor is used to
Q. Explain macro and macro expansion.
(May 19, 5 Marks) {l) Identify the macro definition (2) Save th e
Q. Explain with example conditional macro expansion. macro definition, (3) Identify the macro calls, (4) Expand
the macro calls.
(Dec. 19, 5 Ma~ks)
V Syslem Prog. & Compiler Const. (MU) 3-2 Macros & Macro Proceuor
EXJ'ANOB) PROGAM
A program with
macro definition and MACRO A p,ognim...ihou1
macro Invocation PROCESSOR macrodlir"90rl

__. Copyoode CcncbonalMac,o


__. Pal"llmeter SubetJtutJon ~
ASSEM8LEW
COMPILER

Objeo1
program
Fig. 3.2.1 : Function, of MACRO PrOCHIOrl
Raad maoro dellnlUona lnlo a memory butter

write line In to the output file call Macro Expander routine

Fig. 3.2.2 : A macro processor front end for an assembler


Find 1he appropriate macro definition

Build a parameter table to relate dummy and real parameters

LP ;;: first tine In the template

, - - - - - - - - - - - - - . . Examine the tine

N y

substitute real parameters evaluate the Boolean expression

write it to the output file N y

LP = line number In
the statement

y
LP: line pointer

•Fig. 3.2.3 : Macro expander routine

~ti TtcUN ■ I•~


Y r u 11 11, ,t1o n,
"1 Macros & Macro p
;;;;;
5 st
Y em Prog. & Complier Const. (MU) 3·3 ~
3 2
:· Example of Macro Instruction ~----- - - - - - -(SJ.1
• Explaln macro calls within macro giving example. (5 ._,•'\ti
0
E><plaln the concept of macro dellnlllon within a macro.
• . The macro processor replaces ea L a~l

A f operations. c,, Illa


. macro Instruction Is an abbreviation for a sequence O Ion of macros. tio
t
lnS ruction With a group of source language statements. This Is called expans Start of definition
MACRO
{macro name}

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

F' 40' 3, RECORD2


DATAl OS
DATA2 DC F' SO'
DATA 1 OS F' 40'
DATA2 DC F' 50'
Fig. 3.2.6 : MACRO XYZ has single argument
_! System Prog. & Compiler Const. (MU) 3-4 Macto9 & Macro Proce5so,

3.3 Features of Macro Faclllty


,.w -,., -, 14 ,.1 ,1y 19

a. Whal are the features of macro to be Included In the proooseore? (May 1•. S Marb)

a. Explain different features of macro with e,cample. (May 11, 10 Mam)

1. Slmple Macro Processor

In a simple macro processor each macro processor Is expanded with Its code as shown In the Fig. 3.3.1.

II Macro defined Expanded Source

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

Fig. 3.3.1 : Example of Slmple Macro

2. Parameterlsed Macro/ Posltlonal Macro (Macro definition with one, two or many arguments)

', (Dec. 14, 5 Marks)


a. Explain positional parameter in macro.

Q. Explain the different ways of parameter passing in macros. (5 Marks)

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.

-:!rt T1dluPvbllt~t■ ldp to ,. ~


V
-
Macr05 & Macro p~

~
~

Sy11em p ----,
rog.& Congor Const (MU) 3-S

Sourc,
-r.p,ndtd source
;
I

MACRO 1

& STRG LAB &Al. &A2, &Al l


RECORDl
& STRG A 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!

OATAl DC F' 6'

OATA2 OS F' 12'

OATA3 OS F' 24'

Fig. 3.3.2 : Example of parameterized macro

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.

)2, lpe •··710


A certain macro instruction GENER has 10 possible parameters.

E.g. GENER MAC/1,

GENER , , DIRECT, 3
There are generally three ways of specifying arguments to a macro call.
,Y System Prog. & Compiler Const. (MU) Macros & Macro Processor

(a) Posltlonal 1,auments Call to MACRO


Example 1 Example 1
In this arsuments are match~ with the dummy
arsuments according to the order In which they cauHt execution
appear with posltlonal parameter the prosrammer of Example 2
must be careful enough to specify the arguments In AND we cannot caU ,,.. MACAO
the proper order. E,cample 2 Eumpe2
E.g. STRG, DATAl DATA2, DATA3 as it is
GENER, DIRECT, TYPE etc.
'- MENO
(b) Keyword arpments

They allow reference to the dummy arguments by


name as well as by the position. This Is useful when MENO
a macro has arguments that are not always used.
Fig. 3.3.4 : Example of NESTED MACROS
STRG a3 = DATAl, a2 = DATA2, al= DATA3
Source Expansion
(c) Default arguments

It is an argument to a function that a programmer is


not required to specify. In most programming MACRO Expansion of MIDDLE
languages function may take one or more arguments
E.g. INSIDE &Al

# 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

3. MACRO Calls within MACROS/ NESTED MACROS MEND

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.

Find the appropriate macro definition

Build a parameter table to relate dummy an d real parameters

Push LP onto the stack

LP = first line In the template

-------- ---+! Examine the line

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

LP = first line in the


template

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

4. Condltlonal MACRO Expansion Example


MU - Dec. 15

a. Explaln different pseudo-ops used for conditional MACRO


macro expansion along with an example. START 1 D 1, VALUE 1
(Dec. 15, 10 Marki)
D 2, VALUE 2
In our previous example of macro Instruction, each
Invocation of a particular macro was expanded Into the
same sequence of statements. These statements could be START 2 D 1, VALUE 1
varied by the substitution of parameters, but the form of 2, VALUE 2
the statements, and the order In which they appeared was
unchanged.
VALUE 1 DC F' O'
Most macro processors can modify the sequence of
statements generated from a macro expansion, depending VALUE 2 DC F' 1'
on the arguments supplied in the macro-expansion.
AIF and AGO are two conditional macro expansion
MEND
pseudo opcodes which permit conditional selection of the
sequence of the machine instruction that appear in In this example the labels, the operands and the
expansion of macro call. In conditional macro expansion number of instruction generated change in each sequence.
only part of the macro Is copied out into the code. Which This example can be written as :
part is copied out will be under the control of the
parameters in the macro call.

Source Expanded source

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

Fig. 3.3.7 : Example of CONDITIONAL MACROS

,le Tedlu ■ ltd1i


V Pu b l l c 1t1o n 'i
Macros & Macr
r
3-9 o flr~
· & Compiler Const. (MU)
tor the given example ~
AIF: Arlth explanation .
nest d metlc operations are performed by AJF and if ssion 1s evaluated.
nd1 The expre
Is e co tion Is true then only they are branched. It 1.
conditional opcode. ssion Is initially false, the body of
2. If expre th
nt Is never executed, and co'"'t e ~~.
AGO : It Is 5 I II , stateme · • ro1 •q1e
m ar to 10-to' statement in C/C+ +. It Is the while statement to the ne>ct st P.iss.._
from atell'l ~
an unconditional pseudo-opcode. It specifies a label ent i~
appearing the program.
some other statements In the macro
d e fl nltlon. on If expression is true (nonzero), the bod
3. ment is executed and the process . Y Of t~
The flow f 1 state is rep e
0 program during macro expansion s beginning at step 1. e.iteii
controlled by AIF and AGO.
0th As soon as ENDW statement is found, the rn
er th an AIF and AGO we also use IF-ELSE-ENDIF and 4.
checks the Boolean expre ss,. aer0
processor 011
WHILE-ENOW structure for the conditional branching. rning the call to the WHILE, and actio . ~
ret u n 1st L
Symbol table Is maintained to store the values of all based on the new value of the expression. a~eii
macro variables. This table Is used to look up the conditional macro expansion
ExampIes On
current value of a macro.

Labels With a period ( • ) such as . FINI do not appear in


th
------ ------ ----
Example 3.3.1 :
e output of the macro processor.
The following macro called DOIO has three durr,
AIF (&C EQ l)• FINI is a statement which guides the parameters, namely &DEV, &BUF and &CODE. If the actii~
macro processor not to execute the statement parameter corresponding to . &CODE is R, then the
labelled- FINI if the value corresponding to C = 1. expansion of the macro results m statements that read one
The macro time IF-ELSE-ENDIF structure character from the device spe~ified by &DEV and store the
It provides the mechanism for either generating or character in the memory location (byte) specified by &au
F.
skipping selected statements in the macro body. Whenever If the actual parameter corresponding to &CODE is not ~
an IF statement is found during the expansion of macro the then the expansion of the macro results in statements that
value is coi:nputed in terms of Boolean expression. If the write the character stored in the memory location (byte)
executed value is true IF block is executed otherwise Else is specified by &BUF to the device specified by &DEV.
evaluated.
0010 MACRO &DEV, &BUF, &CODE

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

Nott•: A wrong aoluUon : (Why la thla wrong ?)


1. When the 0010 macro Is Invoked by the statement CHANGET MACRO
0010 AS, LOC, R IF (A EQ O)
The expanded version of the macro Is as follows : CLEAR T
CLEAR A ELSE
AACHECK TD =X' AS' LDT #-1
JEQ AACHECK ENDIF
RD =X'AS' MEND
STCH LOC A correct aolutlon :
2. When the 0010 macro Is Invoked a second time by (Doea not uae condltlonal expanalon)
the statement CHANGET MACRO
0010 A7, LOC, W T
CLEAR
The expanded version of the macro Is as follows : #0
COMP
LOCH LOC
JEQ $EXIT
ABCHECK TD =X' A7'
LDT #-1
JEQ ABCHECK
$EXIT OR #0 #This is a nop.
WO = X' A7'
MEND
3. When the macro is invoked using the statement
5. Recursive MACRO Calls
0010 A7,LOC
It allows the macro invocation statements appearing
The expansion would be similar to that in (2) above.
within the body of a macro instruction. Macros that invoke
(In this case, the SIC/XE assembler treats the value
themselves are called recursive macros. If macros are
of the parameter &CODE as the null character.) calling themselves, one speaks of recursive macro calls. In
Example 3.3.2 this case, there must be some stop criterion, to prevent the
macro of calling itself over and over until the assembler is
The purpose of this example is to point out a
running out of memory. Recursive macros executes until
common mistake in writing macros. Suppose you are asked
some command in its definition command sequences fails.
to write a macro CHANGET that sets register T to o if To write a recursive macro we need to have
register A contains 0; otherwise, it sets register T to -1.
Recursive function.
Termination condition.

For example,

PROGRAM MACRO &PROGl, &INPUT 't'

I * Macro to read· record */


CLEAR X (Clear loop Counter)

&DATA RDCHAR &INPUT Read-characters into' Reg A,

JEQ &QUIT Exit loop if END OF RECORD

JLT &DATA
)
&QUIT MEND

Fig. 3.3.8 (a) : RECURSIVE MACRO


W T1dl ■ l1•i
V Pub l l catlons
Macros & Macro Pro
3-11 ce 8 8<ir
· & Compile r Const. MU)
llOcHAR
MACRO & INPlf l'l . •1
-= X •& I N' ,,.Tet1l Input Device
TD

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

#defin e min(a, b) (((a)<(b ))?(a):(b ))


mainQ
{
int w, x, y, z, small;
//Read w, x, y, z.
small= min(w; min(x,m in(y,z))) ;
printf(" Mihiinu m of 4 number s is %cl", small);
}
return address.
If a program ming languag e does not support macro calls then there must be
6. MACRO Instruct ions Defining MACRO s
a group of similar macros. To call the
A single macro instructi on might be used to simplify the process of defining
inner macro it is necessa ry to define the outer macro first.
MACRO
DEFINE &ADD Macro Name DEFINE
MACRO

of &ADD &X Dummy MACRO NAME


Definitio n ~
MACRO DEFINE "I CNOP 0, 5 Alignme nt of boundar y
BAL 3 * + 10 Set register 3 to paramet er
list pointer
,)
Definitio n of DC B(&X) Paramet er list pointer
MACRO ADD L 20, = V (& ADD) Address of subrouti ne ADD
'-- BALR 19,20 call transfer red to
subrouti ne

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

•W ■ Mflt ■ pass. It will save all the macro definitions in a Macro


Definition Table (MDT) and prepares a Macro Name
Q. Explain the working of a macro processor with ,neat
Table (MNT).
flow charts and data bases. (May 14, 10 Marks)
All macro Invocation statements are expanded during
The four basic steps that any macro processor
the second pass. In this pass it will check all the
should perform are as :
mnemonic pseudo-opcodes and substitute each macro.
name with appropriate code from the macro
1. Recognize Macro Definitions
definitions.
Proper match of all the nested and recursive macro
Format of Databases
calls should be done and macro processor should
identify macro definitions given by op-codes MACRO l. Argument List Array (ALA)
and MEND.
ALA is used in both pass 1 and 2.
2. Save the Definitions Dummy arguments are substituted with the positional
indicators in ALA during pass 1. Also the dummy
To expand the macro calls and substituting the arguments on the memory are represented by #.
expanding source code a macro processor should Where # is used by the macros. For example consider
store the macro definitions. the Fig. 3.5.1. The store macro definition would be

LOOPl LAB &Al, &A2, &A3


3. Recognize Calls
&STRG D 3, #3
The processor should recognize all the macro calls
#0 D 2, #2
that appear as mnemonic op-codes.
D 1, #1
In this all the macro calls are identified from various
mnemonic op-codes. MEND

4. Expand Calls and Substitute Arguments


Fig. 3.5.1: MACRO DEFINITION TABLE FOR PASS 1
Macro definition arguments are replaced by the
corresponding macro calls. The index markers # are replaced with macro call
arguments ih PASS2.
In this macro processor has to take a different
LOOPl LAB DATA1,DATA2, DATA3
decision like :
Generated ALA is

'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-~
- ~

Arlument list Array DATABASES Required for PASS 1


Index 1.
Arguments (2 bytes) Macro source code for input.
1.
0 HXYZ bbb" Output of source code is used by pass 2_
2.
1 "RECORDl bbb" Macro definitions are stored in MDT.
3.
2 "RECORD2 bbb" Names of defined macros are store in MNT.
4.
3 "RECORD 3 bbb" The next entry in MDT is indicated by the
s.
b - blank character Macro Definition Table Counter (MDTC).

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.

Index Card 2. Output from PASS2 is used by assembler as


input.
: :
3. PASS2 uses the MDT and MNT made by
10 & STRG LAB &A3, &Al, &A2
PASSl.
11 #0 A 3,#3
4. The next line of data to be used during macro
12 A 1, #1 expansion is given by the Macro Definition

13 A 2,#2 Table Pointer (MDTP.

14 MEND 5. To replace the macro call arguments for the


index markers in the stored macro definitions
are given by ALA.
Fig. 3.5.3 : Macro Definition Table
Algorithm for Two Pass MACRO Processors
3. Macro Name Table (MNT) PASS 1 : It recognizes all macro definitions defined in the
MDT table indicates the beginning of the macro program.
definition consists of a character string and a pointer 1. Each input is checked line by line.
to the entry In the MDT. MNT is almost similar to
2. MDT is used to store the entire definition of macro
MOT and POT
8 bytes 4 bytes in next available location as soon as macro pseudo-
opcode is encountered.
Index Name MDT Index
3. The maero name is entered into the MNT with

pointer which Indicates the first entry of the macro


4 "LAB bbb" 10 definition.

MDTC and MNTC are incremented when th eY


4.

Fig. 3.5.4 : Macro Name Table encounter next statement.


-- -
_j System Prog. & Compiler Const. (MU) 3-14 Macros & Macro Prooessot

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

Read Next Source Card !+-----------,

No

Write a copy Enter Macro name


forPASS 2 and Current Value
ol MOTC In MNT
entry No.

MNTC = MNTC + 1

IGoto PASS 2 j
Construct Argument
List Array

Entry Macro Name MOTC = MDTC + 1


Into MOT

MOTC = MOTC + 1

Read Next Source Code 14--+------"

• Substitute Positional
Indicators for the
formal parameters

Fig. 3.5.5 : Flowchart - PASS1 - Processing Macro Definitions


t SY61em Prag. & Compiler Const- !MU)

PASS2

.]

Write lnlo expanded ..,___No_-......


eource 01rd Ille

MDTI'+-MD'T
lndeX lrom MNT entrY

~ply expanded ~
"._. to ASSEMBLER

olfference t,etwe
3,7

Slbstitute arguments
t·,from Macro cal

Fig. 3.5.6 : FLOWCHART PASS 2- Recognize Macro Call & Expansion

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

4. 1ubrout1ne II that -function ,.iume a value but


Macros check the number of arguments; they do not
1Ubroutl119 doet not. A fl#ICIIOn lhould oot change
check argument types. Functions checks both
thf ~ues of 1he actual • ~ WhefMI •
number of arguments and arguments type.
■ ubrotlne changee the YaluN of 1tle actual
s. The macro replaces the shorthand text, with the argument ■ .

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.

7. Macros can only be used In the program they are


Debugging In macro Is difficult as compared to
defined In and only after the definition.
function as size of the code grows.
8. Macros can take max 9 parameters.
Macro should be used for small tasks and functions
for the complex tasks. Macros are expanded at compilation / generation.

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.

Subroutines are 'expanded' at runtime

3.7 Difference between Procedure Calls and Macro Calls

'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 .

In procedure the passage


possible.
of parameters Is not

Procedure is an extension of a determined program.


In macro the passage of parameters Is possible.

Macro is a module with specific functions which


can be used by different programs.
___,/ The call of macros Is done as If it were an
use of a directive is required.
1or1J 4. To call a proce d ure the assembler Instruction.
to II ,~
:ne d fuo"'
14- ,I
1e 3· w~at ~
·orrf1S
V

3.17 . ' Macro Call~


. & Compiler Const. (MU

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

calls with the help of an example.


Q. 1 Explain the recursive macro expansion and nested macro
tine/function and when will be a macro used.
Q, 2 What are the advantages of using macro in place of subrou
.
Q. 3 Explain the difference between functions and macros
Give example.
Q.4 Describe the different features offered by the macro facility.

Explain the function of the macro processor.


I

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.

a.a Explain the design of two pass macro processor.


~
::1:odule •! [ Loaders and Linkers

Introduction, functions of loaders, Relocation and Linking concept, Different loading schemes : Relocating
loader, Direct Linking Loader, Dynamic linking and loading.

o These two basic linker functions-relocation and


4.1 Introduction library search-appear to predate even assemblers.

o The relocating loader will load the program


4.1.1 Evolution of Linker and Loader anywhere in memory, altering the various
addresses as required to ensure correct
- Initially the programs in the computers were
referencing.
programmed using the machine language.
- Before this there was operating systems, each program
o Programmers would write out the symbolic had the machir;ie's entire memory at its disposal, so the
programs on sheets of paper, hands assemble program could be assembled and linked for fixed
them into the machine code, and then toggle the
memory addresses.
machine code into the computer.
- With the advancement in operating system, relo~ting
o The symbols were bound to addresses as the
loaders became necessary. The actual addresses at
programmer did the translation from his hands.
which the programs would be running were not known
o The names are bound to addresses too early in until the operating system loads the program into the
the process. memory.

- 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

• Static linking and Dynamic linking


• Shared Library
• Object codes with multiple sections
• Shared code section
• The advent of hardware
relocation and virtual memory
• Linkers and loaders became less
complex
• Overlap
• Linkers did more and more complex
name management and address
binding
• The advent of operating system
• Separation of linkers and loaders
• Relocation and library search

• Address binding by hand translation


Fig. 4.1.2 : Basic Structure for Storing of Program Into Memory
y System Prog. & Complier Const. (MU) 4-3 Loadn and Lilkets

help of Unker and finally step 4 Is done with the help of


4.2 Linking vs. Loading Loaders.

hJIM'11M0
Q. Loading and !Inking scheme. (5 Marke)

Q. Define Loader. . (Dec. 19, 2 Marki)

Execution of program written in a language L performs OBJECT


UNl<EA RtedylO ~
the following steps MODULES blnltyp,ogram

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

For performing these steps we need different


Fig. 4.2.1 : General Language Proceuor Scheme
language processors. Step 1 is performed using
Language Translators, step 2 and 3 is done with the

Loader

I Q, Define Loader. (2 Markll) I


by
A loader is a program which accepts the various object program decks, and prepares these decks for the execution
loading them into the main memory for execution.

PROG 1

PROG2
LOADER
PROG2
PROG . ___. . ,
31-- ----:- ----.;i
PROG3

DATA
BASES
PROGN
PROGN
t
PROGRAMS

Programs loaded into the


memory ready for execution
Fig. 4.2.2 : Basic Loading Scheme
'rl,; TtcHu•l•~lt
V Publlt• t l on,
~ d .'
4~ === === =L= o== a== d=:e ~rs~ an~
~
t• ~~~ ~~~ ~~~ ~~~ ~~~ === ==~ ~== === ===
Y
~ Syste m Prog. & Comp iler Const. (MU)
Linke r dules (creat ed by comp ilers or ass
t mo . . m ertibler
II ction of objec 11
Is a progra m that takes a co e the linked progr am rn main emory .
A HUnker" or llnk editor
m. A loader places
and combines them Into a single executable progra

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

and at setting prote ction bits, and


large progr am into small, more manageable pieces 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

Svmbol Resolution : The main task of a


symbol I
I
resolution Is to transl ate name d symb ols into memory
Execut able ges: I
Runtlme
--+ Linker --+ program locatlons. The process typica lly involv es a few sta
Library
and :
identifying the symbo ls, segm enting them,
determ ining the actua l addre ss.
I
Fig 4.2.4 : Basic Struc ture of Linker
Al th ough there is consi derab le overlap between
linkers
linking and loadin g, one impo rtant featu re that
nd code. This
~ loaders share is that they both patch objec t
is a uniquely powe rful featu re.
S stem Pro . & Compiler Const. (MU) 4-5 Loaders and Unkers

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 :

4.3 Functions of Loader (1) Publicly defined symbols


MU - Dec.14, May 15, Dec. 19 (2) Externally defined symbols
(3) Local symbols
•Q. What are the: different Junctions,of loader explain-in
brief? ', , (Dec. 14, Dec. 19, 5 Marks) 1. Publicly defined symbols : These symbols are

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 ?

0012 SAMPLE SEGMENT

0013 ASSUME cs: SAMPLE

OS: OAMl

MOV AX, DAMl 0000


0014
MOV OS, AX 0003
0015
JMP A 0005
0016
A MOV AL, B 0008
0017

00 27 s MOV AX, BX 0196

0043 SAMPLE ENDS

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

Linking Is required by all subprogram calls. program.

_ Programs written In PASCAL are typically nested Inside Advantages


the main program, hence procedure references do not
- Simple to Implement.
require llnklng.
- No extra procedures are involved.
Object module
Disadvantages
Object module is a sequence of object records, each
object record describing specific aspects of the program. - Wastage of memory.
- It retranslates the user's program code every tJme It is
The object module contains the following basic
executed.
information.
- When the source code Is in different program is in
(1) Binary image different languages it is hard to handle multiple
External references segments.
(2)

(3) Public definitions


(4) Debugging information Source
,Program
(5) Miscellaneous information 'Deck
Each object record contains variable length
information.

4.5 Different Types of Loaders


MEMORY
Fig. 4.5.1 : Assemble and Go Loader
Q, Explain In brief various types 6f loader with their
adva~tages and disadv~ntaqes. (10 Marks) 4.5.1(A) General Loading Scheme
Q. Explain different types of lo~ders in detail,
(May 19, 1'0 Marks) Loader is a utility program that accepts the object
code and load them into the main memory for the
The various type of loaders are as follows execution. Loader involves the storage management
1. Compile and Go Assembler/ Compile and Go Loader function of operating systems and is usually performed
2. Absolute Loader later than compilation. In loader scheme, the source
3. Relocating Loader program is converted to object program by some translator
4. Direct Linking Loader (assembler). The loader accepts these object modules and
puts machine Instruction and data in an executable form at
5. Dynamic Loading Loader
their assigned memory. The loader occupies some portion
G. Dynamic Linking Loader
of main memory. Load time refers to the period of loading
These loaders primarily differ in the manner in which
the program in memory and preparing object program for
the four basic functions are accomplished.
execution.
_._TtcHu11ld19
V rua11,1t 1oris
Object
programs
Source ready for
Program 1 execution

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

Simple and efficient to Implement.

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

Fig. 4.5.3 : Absolute Loader

from the old phrase "Pull yourself up by your bootstraps." It


The four basic functions of an absolute loader are as :
is also known as bootstrapping or boot loader. The boot
i. Allocation - done by programmer loader is now part of the EFI BIOS.
ii. Linking - done by programmer
The basic functions of the bootstrap loader are as
iii. Relocation - loaded where assembler assigns.
following:
iv. Loading- By loader
- It helps user to select an operating system for starting.
4.5.2(A) Bootstrap Loader - It loads the operating system files from the boot
partition.
Bootstrap Loader is a type of an absolute loader.
- It controls the operating system selection and
When the computer is first powered on, it does not have an
hardware detection prior to the kernel initialization.
operating system in Memory. The computer alone cannot
perform complex actions such as loading of a program from In computing, bootstrapping is a technique by which

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.

For each Source program the assembler outputs


4.5.4 Direct Linking Loader
MU - Dec. 14. Dec. 17
The machine translation code of the program.

Transfer vector are used to store the addresses


a. Explain working of a dfrect llnkinp loader with 8
proper example. Clearly show the entries In different
containing names of the subroutines referenced by the
databases built by the direct linking loader.
source program.
-. (Dec. ,4, -10 Merk•)

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.

(Oto. 17, 10 M1ru) Q. Expia,lhlclolig flal_fnldno_ln __ I


1·~ •
~

a. Explain the working of Direct Linking Loader with


,neat flow charts. · · (10 Markt)
I
_ (10MIJb) _

Direct finking loader uses four types of records In the


object file they are as ESO, m, RLD, END.
A Direct Linking loader Is the most popular loading
scheme used. It allows the programmer to use the multiple
1. External Symbol Dnctory (ESD)
procedure and data segments giving user a complete choice It combines information about all symbols that are
In referencing data or Instruction enclosed In other parts of mentioned in the pr011am but that may be
segments. It provides flexlble lntersegment referencing referenced elsewhere.
accessing ability. Assembler provides followlng Information
2. Tfft Record (TXT)
to loader with respect to each procedure or data segment :
It contains the Information about the actual object
_ Segment length.
code translated version of the source pr01f3m.
_ Records of all the symbols that may be referenced by
other symbols and their relative location. 3. Relocation and Linkage Directory (RLD)

- 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

- Explanation about the address constants, their location dependent.


in the segment and information about their revise 4. END Record
values.
It specifies the end of the object file and starting
- Information about the source codes machine code
address for execution if the assembled routine is in
translation and the relative addresses assigned.
the main program.

4.5.4(8) Example Program for Direct Linking Loader


..Source
.,, .,,
•~; ~. { '
,Refatlve
, , . . -~· ·~ ,, , S,ample Program ' ,.

1 0 LOOPl START

2 ENTRY L0START0, L0STARTl

3 EXTRN LlSTART0, LlSTARTl

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

EXTRN LOSTART0, LOSTARTl


14

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

Source Card Variable ESD Relative


Length
Reference Name address
Type ID

0 36 (STARTING FROM 0 AND


1 LOOPl SD 01
ENDING AT 36)

08 -
2 L0START0 LD 02

2 L0STARTl LD 02 12 -

3 LlSTARTl ER 03 - -
3 LlSTART0 ER 03 - -

Where,

SD - Segment Definition (Hexadecimal code= 01)


LO - Local Definition (Hexadecimal code = 02)
ER - External Reference (Hexadecimal code= 03)

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

Object Deck Program for L1START1

ESD Records

Source Card Variable Type ESDID Relallve Length


Reference Name :
t Addrell

12 LlSTARTl SD 01 0 26 (STARTING FROM 0 AND


ENDING AT 26)

13 LlSTART0 LD 02 04 N/A

14 L0START0 ER 03 Unknown N/A

14 L0STARTl ER 03 Unknown N/A

TXTRecords
·.•
Source Card Relative Content ,. What the assembler did?
, I
Reference Address
'
16 14 0 0 LlSTART0 Not known

17 18 10 10 L0STARTl Not known

18 22 -5 - 5 L0START0, L0STARTl Not known

RLD Records

Variable ~ource Card ESDID r


;
Length ' Arithmetic Relative
'
Name Reference Operator Address

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
- ,. " ~

(4 -bytes) decimal .,.; RLD, END or LDT/EOF. ' ,


1 100 As soon as TXT or RLD record is encountered
2 120 processing is done during PASS 1 so the next record
no
3 130 is read.
Indexed 4 - ESD record is processed depending upon SD, LD or
by : ER.
identifier
SD (Segment Definition) : During SD length is
: :
temporarily stored in SLENGTH and the assigned
255 :
value to this symbol is set to the current value of the
4.5.4{C) Databas es Required for Direct Linking PLA.
Loader After that value is stored in GEST to check whether
the symbol exist or not.
(I) PASS 1
If the symbol exists in the GEST error duplicate entry
(1) Object files used for input. Is given otherwise symbol and its value are printed
(2) Initial Program Load Address (IPLA) gives the as part of the load map.
address to load the first segment given by
LO (Local Definition) : During LO the value assigned
operating system or programme r. PLA plus the relative ADDR given by the ESD record.
(3) Program Load Address (PLA) it keeps the track
ER (External reference) : No processing is done for
of each location for the segments assigned to
ER.
it.
(4) To store external symbol and its correspondin g, END : During END record the PLA is incremented bY
th
assigned core address GEST is used. adding the SLENGTH, in it and giving the PLA for e
(5) Output of PASSl Is used as input for PASS2. next segment.
(6) Load map Is used to specify external symbols
and their assigned output values.
LDT/EOF : When LDT or EOF ESD card is
1 r~:
control transfers to the PASS 2 after cornple
- - - - - - - - - - - - - - - - - - - - - L - - ~ P ~ A S ~ S ! ; l · : . . __ _ _ _ _ _ _ _ _
_,--::c-;:;
,:/~f•dl"'~!~
V p u11 t1 < '
-
.i System Prog. & Compiler Const. (MU) ..15 l.Oldltlandlilkett

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

Print Symbol name


arict,'Y!llue for load map

Fig. 4.5.4 : Flowchart for PASS 1

PASS 2 : Load the Actual Program Text.

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.

(2) Each of the ESD records is processed in different manner.


- SD : In this length is temporarily saved to the variable SLENGTH and LESA(ID) is set to the current value of the
PLA.
- LD: No processing is done during PASS 2.
- ER: GEST obtained form PASS1 is searched for match with an ER symbol if found its value is extracted LESA (ID)
is set to one otherwise error message is displayed .
J., Tea..._.Hfi
. . PtUlltltlOflS
Pl.A+ - IPLA

Tn,ns fer to
1.c,oellon EXADOR

ESO Card type 1


TXT
MoveTXTt,y1.ee
from oard column
17-72 to locatio n
PLA+ ADDA
RLD

Sl.EN GTH +- LENG lli Search GEST tor Symbol


PLA+ - PL.A+ SlENGTH t - - - --1
LESA (ID) PLA

No

(-)
Flag

.Add value to;.


conten ts of locatio n
(PLA+ ADDR )

Fig. 4.5.5 : Flowchart FOR PASS 2


process
in order to execute the progr am. So loading
TXT : In this data is copie d to the appropriate
reloc ated core locat ion (PLA+ADDR). can be extremely time consuming.
l
RLD : RLD recor d is used to calculate the actua Even though the loade r progr am may be smalle
r than
and
reloc ated addre ss as the sum of the Pl.A amount
the assembler, It does abso rb a considerable
the ADDR field.
of space.
END : In END recor d the PLA is incre ment ed by
Pl.A
addin g the SLENGTH, in it and giving the The ment ioned probl em can be solved by dividin
g the
for the next segm ent. It is used to store
loading process Into two sepa rate progr ams :
exec ution start addre ss EXADDR after being
reloc ated by the PLA. 1. Binder : A binde r is a progr am that perfor
ms the
d
LDT/ EOF : As soon as LDT/EOF is encou ntere same funct ion as a direc t-link ing loade r In "bindin
(
curre nt
the cont rol is trans ferre d to the The binde r performs
by subroutines toget her.
addre ss of the loade d progr am given
allocation, relocation and linking.
E.XADDR.
rather
Disa dvan tage s 2. A Modu le Loader ,;_ It outpu ts the text in a file
than mem ory. The modu le loade r merely has to
to allocate,
In direc t-link ing loade r, it is necessary
s each time physically load the modu le into the memorv-
reloc ate, link and load all of the subro utine
_! System Prog. & Compiler Const. (MU) 4-17 Loaders and Linkers

4.5.5 Dynamic Loading Loader PAOOt


~
_ If all the subroutines are loaded slmultaneouslv Into PA002
the core, then there mav be a chance, that the core (2011)
avallable mav be Insufficient for the subroutines, this PAOOa
11011
mav lead to further complications Execution of such (30k)
(TOIII Mlfflofy
program can be possible If all the segments are not PFI004 ~
required simultaneously to be present In the main (101()
memory. PR00 5
(201()
_ In such situations only those segments are resident In
the memories that are actually needed at the time of PROGe
(10k)
execution. But the question arises what wlll happen If
the required segment Is not present In the memory? Fig. 4.5.8 : Subroutine carta t>Mw•n Dtffef'9nt
Naturally the execution process will be delayed until Procedurn
the required segment gets loaded In the memory. Subroutine call between the procedures

_ overlay structure is used to specify the inter PROGi


dependency between all the segments. It consists of {
nodes and edges and segment is represented by the PROG2
nodes. If the two are on the same path they can lie in
{
the main memory.
PROG3
- For solving such problems techniques like {
segmentation and paging is used. In these the
subroutines are loaded into core at a different time
because the subroutines in a program are needed at
different times i.e. they may be mutually e~clusive, by ..
}
identifying which subroutine can calls other
subroutines to produce an Overlay Structure. PROGS
{ }
Overlay Structure is used to :

1) Keep . in memory only those instructions and PROG4


data that are needf!d at any given time. {
. PROG6
'
2) Overlay structure needed when process is {-
larger than amount of memory allocated to it.
}
3) Implemented by user, no special support
}
needed from operating system, programming
PROGS
design of overlay structure is complex.
{
- The flipper or overlay supervisor is the portion of the
loader that actually intercepts the "calls" and loads the }
necessary proc,edure. }
- Let us take an example to understand the concept.
PROG.1-. Calls (PROG 2, PROG 4, PROG 5)
Various · segments· ·along with their memory
PROG 2 - Calls (PROG 3, PROG 5)
requirements are shown further. The Fig. 4.5.6
PROG 4 - Calls (PROG 6)
illustrates a program consisting of six subprograms.
PROG 3 and PROG 5 do not call any other routines.
(PROGl, PROG2, PROG3, PROG4, PROG5, PROG6)
PROO 1 (20 k)

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)

It helps In not loadlng the entire library for


~ 19 Loaderl and lJnkeq

execution. In Unix-like optmJnt systems u1'nl XCOFF, such as


(4)
AIX, dynamlcalty-toaded shared libnrles use the
(4)
In dynamic llnklng loader Is used to load the main
fllen1me wfflx .1.
program.
Dynamic llnkJna from A.lsembler llnsu,,e prosr..m
SttPI to accomplllh th• actual loadlng and !Inking (SI
In IBM OS/360 and tts successo,s k done typk:aDy
of I called procedure•
u5'n1 a LINK m1ao Instruction contutlnc ,
(l) n,e symbolic name of the routine In the program Is Supervisor Call lnstructJon that KtJvites the
used to make the load and call service request to the operating system routines that makes the librJry
operating system. module to be linked avalla~e to the P,Ol'iffl-
(2) The operating system checks Its Internal tables to
Advantage
determine whether or not the routine Is already
loaded. (1) No over head Is Incurred unless the procedure to be •
(3) Control Is then being passed from the operating called or referenced Is actually used.
system to the routine being called. When the called
subroutine completes Its processing, the operating (2) A further advantage is that the system can be
system then returns the control of the program that dynamically configured.
Issued this request.
Disadvantage
Implementation of Dynamic Linking Loader
(1) Considerable overhead and complexity lncurved,
(1) Dynamic-link library, or DLL, Is Microsoft's due to the fact that we have postponed most of the
implementation of the shared library concept in the binding process until execution time.
Microsoft Windows and 05/2 operating systems.
4.5.7 Difference between Dynamic Loading
These libraries usually have the file extension DLL,
and Dynamic Linking
OCX (for libraries containing ActiveX controls), or
ORV (for legacy system drivers). r.1u - r.lay 16
.,
(2) In Unix operating systems, the dynamic linker is
.a. What Is the difference between Dynamic Loadng
k.J' and Dynamic Linking explain with an example?
actually an external executable that the operating
_ ., ~~'i·-·- __ _ (May 16, 10 Marb)
system kernel loads and executes first in a process
address space newly constructed as a result of l>yo.amlc Loading '" Dynamic Unklng
calling exec or posix_spawn functions. When an
executable file is loaded, the operating system
1. It refers to mapping Dynamic linking refers
kernel reads the path of the dynamic linker from it
(or less often copying) to resolving symbols •
and then attempts to load and execute this other
an executable or associating their names
executable binary. library into a process's with addresses or
memory after is has offsets - after compile
(3) In Apple Darwin operating system, OS X and iOS
started. time.
operating systems the dynamically loaded shared
libraries can be identified either by the filename 2. It is similar to plugins The linker while creating
suffix .dylib or by their placement inside the bundle that is an exe can the exe does minimal
actually execute work. For the dynamic
for a framework.

.
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.

ft_ReyieY!,,q~~..stlons I a. 14 Explain the design of Absolute Loader.

Q. 1 What is difference between loader and linker ? Give a. 15 Explain the relocation and linking concepts.
one example of each.

-:□ o

You might also like