Hardware Reference
In-Depth Information
public class Interp {
static int PC;
// program counter holds address of next instr
static int AC;
// the accumulator, a register for doing arithmetic
static int instr;
// a holding register for the current instruction
static int instr type;
// the instruction type (opcode)
static int data loc;
// the address of the data, or 1 if none
static int data;
// holds the current operand
static boolean run bit = true;
// a bit that can be turned off to halt the machine
public static void interpret(int memory[ ], int starting address) {
// This procedure interprets programs for a simple machine with instructions having
// one memory operand. The machine has a register AC (accumulator), used for
// arithmetic. The ADD instruction adds an integer in memory to the AC, for example.
// The interpreter keeps running until the run bit is turned off by the HALT instruction.
// The state of a process running on this machine consists of the memory, the
// program counter, the run bit, and the AC. The input parameters consist of
// the memory image and the starting address.
PC = starting address;
while (run bit) {
instr = memory[PC];
// fetch next instruction into instr
PC = PC + 1;
// increment program counter
instr type = get instr type(instr);
// determine instruction type
data loc = find data(instr, instr type);
// locate data (
1 if none)
if (data loc >= 0)
// if data loc is 1, there is no operand
data = memory[data loc];
// fetch the data
execute(instr type, data);
// execute instruction
}
}
private static int get instr type(int addr) { ... }
private static int find data(int instr, int type) { ... }
private static void execute(int type, int data) { ... }
}
Figure 2-3. An interpreter for a simple computer (written in Java).
This equivalence between hardware processors and interpreters has important
implications for computer organization and the design of computer systems. After
having specified the machine language, L , for a new computer, the design team can
decide whether they want to build a hardware processor to execute programs in L
directly or whether they want to write an interpreter to interpret programs in L in-
stead. If they choose to write an interpreter, they must also provide some hardware
machine to run the interpreter. Certain hybrid constructions are also possible, with
some hardware execution as well as some software interpretation.
An interpreter breaks the instructions of its target machine into small steps. As
a consequence, the machine on which the interpreter runs can be much simpler and
less expensive than a hardware processor for the target machine would be. This
 
 
Search WWH ::




Custom Search