0% found this document useful (0 votes)
149 views8 pages

Two Pass Assembler Algorithm

The two pass assembler first performs symbol table creation and error checking (Pass 1), then generates machine code (Pass 2). Pass 1 analyzes each statement, inserting symbols into the symbol table. It handles labels, mnemonics, operands, and pseudo-ops. Errors are detected and flagged. Pass 2 generates the target file. It analyzes each statement, writing opcodes and addressing symbols. Pseudo-ops are handled to set locations or write data/constants. On completion all tables are reset and files closed.

Uploaded by

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

Two Pass Assembler Algorithm

The two pass assembler first performs symbol table creation and error checking (Pass 1), then generates machine code (Pass 2). Pass 1 analyzes each statement, inserting symbols into the symbol table. It handles labels, mnemonics, operands, and pseudo-ops. Errors are detected and flagged. Pass 2 generates the target file. It analyzes each statement, writing opcodes and addressing symbols. Pseudo-ops are handled to set locations or write data/constants. On completion all tables are reset and files closed.

Uploaded by

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

Algorithm for a two Pass Assembler : [with error handling]

Pass 1:
Step 1:
1) Prepare and initialize all data structures like M!" P!" S! etc#
$) pen input soured file and output intermediate file in respecti%e mode#
&) 'nitialize all pointers(i#e# M!P!)" P!P!) etc#) to point to the first
entr*#
+) 'nitialize the ,- to .#
/) Set error flag 0 11
Step $: )ead a line of source code#
'f 2end of file3 ) 24563 pseudo opcode
'f error flag 0 off
7o to pass $#
4lse disp8message 9:nsuccessful assembl*; and do the re%erse of 1
st

step and <uit#
4lse
7oto step &
Step &: Anal*ze the statement
-ase 1: Statement contains onl* comment#
Start form S- i#e# start of comment and ignore e%er*thing till 4-
i#e# end of comment
7! S!4P $
-ase $: Statement contains onl* label (e#g# loop:)
-heck whether label is present in the S!# (i#e# a s*mbol with attr# ,abel)
'f not present
'nsert label in S! = also mark attribute as 2,3#
address of label is set in ,-#
insert this address against label in S!#
else error 9duplicate label;# And set error flag 0 5
7! S!4P $
-ase &: Statement contains onl* mnemonic (e#g# S!P)
Search M! for Mnemonic
,- 0 ,- > length of instruction
7! S!4P $
-ase +: Statement contains mnemonic and operand# (e#g# ,A6 A)
Search M! for mnemonic
Search operand in S! if present do nothing#
else insert operand in S!
,- 0 ,- > length of instruction
7! S!4P $
-ase /: Statement contains label and mnemonic (e#g# back : S!P)
-heck whether label is present in S!
if not present
insert label in S! = attribute as 2,3
address of label is set to ,-
insert this address against label in S!
else error ;duplicate label ;# And set error flag 0 5
Search M! for mnemonic
,- 0 ,- > length of instruction
7! S!4P $
-ase ? : Statement contains label" mnemonic and operand (e#g#
,P: @mp loop)
-heck whether label is present in S!
if not present
insert label in S! = attribute as 2,3
address of label is set to ,-
insert this address against label in S!
else error ;duplicate label ;# And set error flag 0 5
Search M! for mnemonic
Search operand in S!
'f present
do nothing
else
insert operand in S!
,- 0 ,- > length of instruction
7! S!4P $
-ase A: Statement contains pseudo opcode
-ase A#1: Statement contains pseudo opcode 2S!A)! 3 or 2)73 (e#g#
)7 1...)
Search P! for pseudo opcode
)ead the neBt operand and initialize ,- with the %alue of the operand
7! S!4P $
-ase A#$: Statement contains pseudo code 2456P3 (e#g# 456P)
Search P! for pseudo code
)eset code segment flag and set data segment flag#
7! S!4P $
-ase A#&: Statement contains pseudo code 2dC3 (e#g# dC S:M /)
Search P! for pseudo code
Search operand in the S!
D put the address against operand in S!#
D put neBt operand (e#g# in this case /) against address i#e# put %alue (if
an*) against the address#
D ,- 0 ,- > 1 as 1 is length of db#
7! S!4P $
-ase A#+: Statement contains pseudo code 2dw3 (e#g# dw S:M E)
Search P! for pseudo code
Search operand in the S!
D put the address against operand in S!#
D read neBt operand
D if E then do ,- 0 ,- > $
D else 1) put neBt operand against the address
$) ,- 0 ,- > $
7! S!4P $
-ase A#/ : Statement contains pseudo code 2-5S!3 (e#g# 54 -5S!
1)
Search P! for pseudo code
Search operand in S!
D put the address against operand in S!
D read the neBt operand
1) put %alue of the neBt operand against the address (e#g# in this case 1)
$) ,- 0 ,- >1
7! S!4P $
-ase A#? : Statement contains pseudo code 24563 or 2456 1 1',43
Search P! for pseudo code
'f error flag 0 5 don3t goto pass $" else 7! pass $
-ase F : ,iteral Gandling
'f the literal is found as a part of instruction
then" put it in S! with appropriate attribute
= %alue if such a kind of literal is not
alread* there#
4lse write the literal name" literal %alue and
present ,- where literal is occurring" in the
S! (S*mbol !able)
-ase H : Procedure Gandling
-ase H#1 : Procedure definition start
'1 1')S! !I45 is P)-
[ if definition flag for procedure is alread*
set" then it is an instance of definition inside
definition" which is not allowed" so displa* 8
error 9nested definition procedure not
allowed 9 else go ahead ]
D )ead the neBt token which is the name of
the procedure
D Jalidate with st
D 'f alread* present as a definition "6ispla*8
error : 6uplicate procedure definition; and
set error flag on
4,S4
D put the name and corresponding attributes
along with present ,- %alue as the starting
address of the procedure in St
D set definition flag
D 7! S!4P $
-ase H#$ : Procedure definition ends
D reset the respecti%e procedure definition flag
to
D write total length taken b* procedure" (if
re<d) in S!
D 7! S!4P $
-ase H#& : Procedure call
D 1irst token must be 2-A,,3 or something like" so %alidate
through M!
D )ead the neBt token which must be name procedure
%alidate that name for the correct attributes" if %alid" do
nothing else 6ispla*8error(9problem in the procedure call;)
and set error flag pn
D 7! S!4P $
-ase 1K : 6efault
An assembler comes to this state onl* if the instruction is in%alid i#e it is
not starting with label" mnemonic or pseudo opcode or comment
D ignore the line
D set error flag 5
D 7! S!4P $

Pass $ of an assembler:
Step 1:
1) Assuming all tables are initialized in pass 1" initialize table pointers#
$) pen input source file and output target file in respecti%e mode#
&) 'nitialize all pointers (i#e# M!P!)" P!P!) etc#) to point to the first
entr*#
+) 'nitialize the ,- to .#
Step $: )ead a line of source code
'f 2end of file3 or 24563 pseudo opcode
Assembl* complete# )eset all tables" %ariables and close all files#
else goto step &#
Step &: Anal*ze the statement
-ase 1: Statement contains onl* comment#
Start form S- i#e# start of comment and ignore e%er*thing till 4-
i#e# end of comment
7! S!4P $
-ase $: Statement contains onl* label (e#g# loop:)
'gnore this as pass 1 has taken care of label definition handling
7! S!4P $
-ase &: Statement contains onl* mnemonic (e#g# S!P)
Search M! for mnemonic
Lrite opcode for that mnemonic in output file
,- 0 ,- > length of instruction
7! S!4P $
-ase +: Statement contains mnemonic and operand (e#g# ,A6 A)
Search M! for mnemonic
Lrite opcode for that mnemonic in output file
Search operand in S!
if present " get address of s*mbol from S! and write in output file
,- 0 ,- > length of instruction
7! S!4P $
-ase /: Statement contains label and mnemonic (e#g# back: S!P)
'gnore label as pass 1 has tackled it#
Search M! for mnemonic
Lrite opcode for that mnemonic in output file
,- 0 ,- > length of instruction
7! S!4P $
-ase ?: Statement contains label" mnemonic and operand (e#g# ,P : @mp
loop)
'gnore label as pass 1 has taken care of it#
Search M! for mnemonic
Lrite opcode for that mnemonic in output file
Search operand in S!
if present " write address of s*mbol in output file
,- 0 ,- > length of instruction
7! S!4P $
-ase A: Statement contains pseudo opcode
-ase A#1: Statement contains pseudo opcode 2S!A)!3 or 2)73 (e#g# )7 1...)
Search P! for pseudo opcode
)ead the neBt operand and initialize ,- with the %alue of the operand
7! S!4P $
-ase A#$ : Statement contains pseudo code 2456P3 (e#g# 456P)
Search P! for pseudo code
)eset code segment flag and set data segment flag#
7! S!4P $
-ase A#&: Statement contains pseudo code 26C3 (e#g# 6C S:M /)
Search P! for pseudo code
Search operand in the S!
D put neBt operand %alue (if an*) against the present ,-
D write this addr# 'n output file
D ,- 0 ,- > 1
7! S!4P $
-ase A#+: Statement contains pseudo code 26L3 (e#g# 6L S:M E)
Search P! for pseudo code
)ead the neBt operand and search it in S!
if E then write some garbage %alue or some predefined %alue against
the present ,- and also in output file
D ,- 0 ,- > $
7! S!4P $
-ase A#/ : Statement contains pseudo code 2-5S!3 (e#g# 54 -5S! 1)
Search P! for pseudo code
Search operand in S!
1) put %alue of the neBt operand against the present ,- (e#g# in this case
1)
$) ,- 0 ,- >1
&) write this constant %alue in output file
7! S!4P $
-ase A#? : Statement contains pseudo code 24563
Search P! for pseudo code
4nd of assembl*

You might also like