Hardware machines have a built-in set of instructions that they can decode and execute. We now study how to extend our implementation with representations of the machine instructions.
The instruction set of the machine is in Table 5.1 (see p. 204 of the book).
Binary opcode | Decimal opcode | Operation (X is a machine address) |
---|---|---|
LOAD X | ||
STORE X | ||
CLEAR X | ||
ADD X | ||
INCREMENT X | ||
SUBTRACT X | ||
DECREMENT X | ||
COMPARE X | ||
JUMP X | ||
JUMPGT X | ||
JUMPEQ X | ||
JUMPLT X | ||
JUMPNEQ X | ||
IN X | ||
OUT X | ||
HALT |
To extend our implementation of the machine, the first step is to
write Java definitions for the instructions. Since we will need to
distinguish the instructions from each other, we will have a separate
class for each instruction. Recall that the Instruction
class is a subclass of the Word
class, just as
Data
is a subclass of the Word
class. Your
first job is to design and implement 16 different classes, one for
each machine instruction. Each of these 16 different classes
representing machine instructions should be a subclass of the
Instruction
class.
E.g. A possible hierarchy might look like:
Word | ------------------- | | Data Instruction | ----------------------------------- | | | LoadIns StoreIns ClearIns . . .Alternatively it might be more convenient to treat the
HaltIns
separately since it is the only instruction that has no address component.
Now we can add appropriate methods to the 16 machine instruction
classes. If your class Word
has any abstract methods, you
will have to implement those; even if your class Word
has
no abstract methods, you may want to override some of the existing
methods. You might also want to add a toString
method to
facilitate debugging. For each of the 15 instructions (other than
HaltIns
), it is useful to have a method
getAddress
that returns the address X
on
which the instruction operates (See Table 5.1).
The most important method to add is an execute
method
that actually executes the instruction. Consult the text (Schneider &
Gersting, pp. 194-199) for information on how each instruction
operates. The following 2 examples should get you started:
public class LoadIns extends Instruction { .... public void execute (MemoryI mem, ProcessorI proc, ioUnitI io) throws MachineE { Word w = mem.fetch(getAddress()); proc.storeACC(w); } }
public class AddIns extends Instruction { .... public void execute (MemoryI mem, ProcessorI proc, ioUnitI io) throws MachineE { Word w1 = proc.fetchACC(); Word w2 = mem.fetch(getAddress()); proc.storeACC(proc.getALU().add(w1,w2)); } }
Since the HALT instruction is used to terminate the execution of a program, and this is an exceptional situation, we will use an exception to encapsulate this information:
public class HaltE extends MachineE { public HaltE () {} }The execute method of the HALT instruction should just throw an exception (HaltE).