Hardware Reference
In-Depth Information
When the END pseudoinstruction is read, pass one is over. The symbol table
and literal tables can be sorted at this point if needed. The sorted literal table can
be checked for duplicate entries, which can be removed.
7.3.3 Pass Two
The function of pass two is to generate the object program and possibly print
the assembly listing. In addition, pass two must output certain information needed
by the linker for linking up procedures assembled at different times into a single
executable file. Figure 7-10 shows a sketch of a procedure for pass two.
public static void pass two( ) {
// This procedure is an outline of pass two of a simple assembler.
boolean more input = true;
// flag that stops pass two
String line, opcode;
// fields of the instruction
int location counter, length, type;
// misc. variables
final int END STATEMENT =
2;
// signals end of input
final int MAX CODE = 16;
// max bytes of code per instruction
byte code[ ] = new byte[MAX CODE];
// holds generated code per instruction
location counter = 0; // assemble first instruction at 0
while (more input) { // more input set to false by END
type = read type( ); // get type field of next line
opcode = read opcode( ); // get opcode field of next line
length = read length( ); // get length field of next line
line = read line( ); // get the actual line of input
if (type != 0) { // type 0 is for comment lines
switch(type) { // generate the output code
case 1: eval type1(opcode, length, line, code); break;
case 2: eval type2(opcode, length, line, code); break;
// other cases here
}
}
write output(code); // write the binary code
write listing(code, line); // print one line on the listing
location counter = location counter + length;
// update loc ctr
if (type == END STATEMENT) {
// are we done with input?
more input = false;
// if so, perform housekeeping tasks
finish up( );
// odds and ends
}
}
}
Figure 7-10. Pass two of a simple assembler.
The operation of pass two is more-or-less similar to that of pass one: it reads
the lines one at a time and processes them one at a time. Since we have written the
type, opcode, and length at the start of each line (on the temporary file), all of these
 
 
 
Search WWH ::




Custom Search