Hardware Reference
In-Depth Information
public static void pass one( ) {
// This procedure is an outline of pass one of a simple assembler.
boolean more input = true;
// flag that stops pass one
String line, symbol, literal, opcode;
// fields of the instruction
int location counter, length, value, type;
// misc. variables
final int END STATEMENT = 2;
// signals end of input
location counter = 0;
// assemble first instruction at 0
initialize tables( );
// general initialization
while (more input) {
// more input set to false by END
line = read next line( );
// get a line of input
length = 0;
// # bytes in the instruction
type = 0;
// which type (format) is the instruction
if (line is not comment(line)) {
symbol = check for symbol(line); // is this line labeled?
if (symbol != null) // if it is, record symbol and value
enter new symbol(symbol, location counter);
literal = check for literal(line);
// does line contain a literal?
if (literal != null)
// if it does, enter it in table
enter new literal(literal);
// Now determine the opcode type.
1 means illegal opcode.
opcode = extract opcode(line); // locate opcode mnemonic
type = search opcode table(opcode); // find format, e.g. OP REG1,REG2
if (type < 0) // if not an opcode, is it a pseudoinstruction?
type = search pseudo table(opcode);
switch(type) { // determine the length of this instruction
case 1: length = get length of type1(line); break;
case 2: length = get length of type2(line); break;
// other cases here
}
}
write temp file(type, opcode, length, line); // useful info for pass two
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
rewind temp for pass two( );
// like rewinding the temp file
sort literal table( );
// and sorting the literal table
remove redundant literals( );
// and removing duplicates from it
}
}
}
Figure 7-9. Pass one of a simple assembler.
occur. Whether it is better to do more I/O to eliminate parsing or less I/O and
more parsing depends on the relative speed of the CPU and disk, the efficiency of
the file system, and other factors. In this example we will write out a temporary
file containing the type, opcode, length, and actual input line. It is this line that
pass two reads instead of the raw input file.
 
Search WWH ::




Custom Search