Raj Kumar
Computer Science And Engineering

Give the algorithm for pass1 of and 2 pass assembler

System Software and Compiler Design

Explanation

969    0

 The Algorithm for Pass 1:


Begin

read first input line

if OPCODE = ‘START’ then begin

save #[Operand] as starting addr

initialize LOCCTR to starting address

write line to intermediate file

read next line

end( if START)



      Else
initialize LOCCTR to 0
While OPCODE != ‘END’ do
Begin
if this is not a comment line then
Begin
if there is a symbol in the LABEL field then
Begin
search SYMTAB for LABEL
if found then
set error flag (duplicate symbol)
Else
(if symbol)
search OPTAB for OPCODE
if found then
add 3 (instr length) to LOCCTR
else if OPCODE = ‘WORD’ then
add 3 to LOCCTR
else if OPCODE = ‘RESW’ then
add 3 * #[OPERAND] to LOCCTR
else if OPCODE = ‘RESB’ then


add #[OPERAND] to LOCCTR

else if OPCODE = ‘BYTE’ then

begin

find length of constant in bytes

add length to LOCCTR

end

else

set error flag (invalid operation code)

end (if not a comment)

write line to intermediate file

read next input line

end { while not END}

write last line to intermediate file

Save (LOCCTR – starting address) as program length

End {pass 1}

 


The Algorithm for Pass 2:


Begin

read 1st input  line

if OPCODE = ‘START’ then

begin

write listing line

read next input line

end

write Header record to object program

initialize 1st Text record

while OPCODE != ‘END’ do

begin

if this is not comment line then

begin





search OPTAB for OPCODE
if found then
Begin
if there is a symbol in OPERAND field then
Begin
search SYMTAB for OPERAND field then
if found then
Begin
store symbol value as operand address
Else
Begin
store 0 as operand address
set error flag (undefined symbol)
End
end (if symbol)
else store 0 as operand address
assemble the object code instruction
else if OPCODE = ‘BYTE’ or ‘WORD” then
convert constant to object code
if object code doesn’t fit into current Text record then
Begin



Write text record to object code

initialize new Text record

end

add object code to Text record

end {if not comment}

write listing line

read next input line

end

write listing line

read next input line

write last listing line

End {Pass 2}


Share:   
   Raj Kumar
Computer Science And Engineering

More Questions from System Software and Compiler Design Module 1