Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

2 Pass Assembler Algorithm

The two pass assembler algorithm involves two passes over the source code to generate machine code from assembly language. In the first pass, it assigns addresses to labels and directives and writes this information to a symbol table. In the second pass, it uses the symbol table to translate opcodes and operands into machine language object code, which is written to an output file.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

2 Pass Assembler Algorithm

The two pass assembler algorithm involves two passes over the source code to generate machine code from assembly language. In the first pass, it assigns addresses to labels and directives and writes this information to a symbol table. In the second pass, it uses the symbol table to translate opcodes and operands into machine language object code, which is written to an output file.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Two Pass Assembler Algorithm

Data Structures
1. Operation Code Table (OPTAB)
Used to look up mnemonic operation codes and translate them into machine language equivalents Contains the mnemonic operation code and its
machine language equivalent In more complex assemblers, contains information like instruction format and length.

2. Symbol Table
Used to store values (addresses) assigned to labels Includes the name and value for each label Flags to indicate error conditions, e.g. duplicate definition
of labels
May contain other info like type or length about the data area or instruction labeled

3. LOCCTR
Used to help in the assignment of addresses Initialized to the beginning address specified in the START statement After each source statement is
processed, the length of the assembled instruction or data area to be generated is added. Gives the address of a label

In Two pass algorithm:

Pass 1 (Define symbols):


(a)Assign addresses to all statements (LOC)
(b) Save the addresses assigned to all labels in symbol table
(c) Perform some processing for assembler directives

Pass 2 (Generate object code):


a. Translate opcode and operands
b. Generate data values for WORD
c. Write object program

Pass 1: Pass 2:

begin begin
read first input line read first input file {from intermediate file}
if OPCODE = 'START' then if OPCODE = 'START' then
begin begin
save #[OPERAND] as starting address write listing line
initialized LOCCTR to starting address read next input line
write line to intermediate file end {if START}
read next input line write header record to object program
end {if START} initialized first Text record
else while OPCODE != 'END' do
initialized LOCCTR to 0 begin
while OPCODE != 'END' do if this is not a comment line then
begin begin
if this is not a comment line then search OPTAB for OPCODE
begin if found then
if there is a symbol in the LABEL field then begin
begin if there is a symbol in OPERAND field then
search SYMTAB for LABEL begin
if found then search SYMTAB for OPERAND
set error flag (duplicate symbol) if found then
else store symbol value as operand address
insert (LABEL, LOCCTR) into SYMTAB else
end {if symbol} begin
search OPTAB for OPCODE store 0 as operand address
if found then set error flag (undefined symbol)
add 3 {instruction lengh} to LOCCTR end
else if OPCODE = 'WORD' then end {if symbol}
add 3 to LOCCTR else
else if OPCODE = 'RESW' then store 0 as operand address
add 3 * #[OPERAND] to LOCCTR assemble the object code instruction
else if OPCODE = 'RESB' then end {if opcode found}
add #[OPERAND] to LOCCTR else if OPCODE = 'BYTE' or 'WORD' then
else if OPCODE = 'BYTE' then convert constant to object code
begin if object code not fit into the current Text record then
find length of constant in bytes begin
add length to LOCCTR write Text record to object program
end {if BYTE} initialized new Text record
else end
set error flag (invalid operation code) add object code to Text record
end {if not a comment} end {if not comment}
write line to intermediate file write listing line
read next input line read next input line
end {while not END} end {while not END}
write last line to intermediate file write last Text record to object program
save (LOCCTR - starting address) as program length write End record to object program
write last listing line
end

You might also like