Lab 5 -- Instructions; Interpreter

Due WEDNESDAY May 3 before 5:00 PM

5.1 The Machine Language

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 opcodeDecimal opcodeOperation
(X is a machine address)
0000
0
LOAD X
0001
1
STORE X
0010
2
CLEAR X
0011
3
ADD X
0100
4
INCREMENT X
0101
5
SUBTRACT X
0110
6
DECREMENT X
0111
7
COMPARE X
1000
8
JUMP X
1001
9
JUMPGT X
1010
10
JUMPEQ X
1011
11
JUMPLT X
1100
12
JUMPNEQ X
1101
13
IN X
1110
14
OUT X
1111
15
HALT
Table 5.1 Instruction Set

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.

5.2 Interpreter

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).

5.3 Exercise

Write the instruction classes! Turn in all 16 classes and anything else you think is significant.


Visited times since March 23, 1999 (or the last crash).