(System programming) Write an algorithm for a two-pass assemble.docx
1. (System programming)
Write an algorithm for a two-pass assembler to translate an
assembly language program to an object program.
(System programming)
Write an algorithm for a two-pass assembler to translate an
assembly language program to an object program.
Write an algorithm for a two-pass assembler to translate an
assembly language program to an object program.
Solution
Algorithm :
Algorithm for a two-pass assembler to translate an assembly
language program to an object program.
Algorithm for Pass 1:
Step1 : Begin
Step2 : read first input line
Step3 : if OPCODE = ‘START’ then begin
Step4 : save #[Operand] as starting addr
Step5 : initialize LOCCTR to starting address
Step6 : write line to intermediate file
Step7 : read next line
Step8 : end( if START)
2. Step9 : else
Step10 : initialize LOCCTR to 0
Step11 : While OPCODE != ‘END’ do
Step12 : begin if this is not a comment line then begin
Step13 : if there is a symbol in the LABEL field then
Step14 : begin
Step15 : search SYMTAB for LABEL
Step16 : if found then
Step17 : set error flag (duplicate symbol)
Step18 : else
Step19 : (if symbol)
Step20 : search OPTAB for OPCODE
Step21 : if found then
Step22 : add 3 (instr length) to LOCCTR
Step23 : else if OPCODE = ‘WORD’ then
Step24 : add 3 to LOCCTR
Step25 : else if OPCODE = ‘RESW’ then
Step26 : add 3 * #[OPERAND] to LOCCTR
Step27 : else if OPCODE = ‘RESB’ then
Step28 : add #[OPERAND] to LOCCTR
Step29 : else if OPCODE = ‘BYTE’ then
Step30 : begin
Step31 : find length of constant in bytes
Step32 : add length to LOCCTR
Step33 : end
3. Step34 : else
Step35 : set error flag (invalid operation code)
Step36 : end (if not a comment)
Step37 : write line to intermediate file
Step38 : read next input line
Step39 : end { while not END}
Step40 : write last line to intermediate file
Step41 : Save (LOCCTR – starting address) as program length
Step42 : End {pass 1}
Algorithm for Pass 2:
Step1 : Begin
Step2 : read 1st input line
Step3 : if OPCODE = ‘START’ then
Step4 : begin
Step5 : write listing line
Step6 : read next input line
Step7 : end
Step8 : write Header record to object program
Step9 : initialize 1st Text record
Step10 : while OPCODE != ‘END’ do
Step11 : begin
Step12 : if this is not comment line then
Step13 : begin
Step14 : search OPTAB for OPCODE
Step15 : if found then
4. Step16 : begin
Step17 : if there is a symbol in OPERAND field then
Step18 : begin
Step19 : search SYMTAB for OPERAND field then
Step20 : if found then
Step21 : begin
Step22 : store symbol value as operand address
Step23 : else
Step24 : begin store 0 as operand address
Step25 : set error flag (undefined symbol)
Step26 : end
Step27 : end (if symbol)
Step28 : else store 0 as operand address
Step29 : assemble the object code instruction
Step30 : else if OPCODE = ‘BYTE’ or ‘WORD” then
Step31 : convert constant to object code
Step32 : if object code doesn’t fit into current Text record then
Step33 : begin
Step34 : Write text record to object code
Step35 : initialize new Text record
Step36 : end
Step37 : add object code to Text record
Step38 : end {if not comment}
Step39 : write listing line
Step40 : read next input line
5. Step41 : end
Step42 : write listing line
Step43 : read next input line
Step44 : write last listing line
Step45 : End {Pass 2}