A complete machine has a memory unit, a processor, and an I/O unit. Our machine allows its clients access to these components and has two additional operations: step and run. The machine interface is:
public interface MachineI { MemoryI getMem ( ); ProcessorI getProc ( ); ioUnitI getIO ( ); /** * step fetches the next instruction and executes it * @exception MachineE if an error occurs during the execution * of the instruction **/ void step ( ) throws MachineE; /** * run executes all instructions from the current one * until it encounters a HALT instruction * @exception MachineE if an error occurs during the execution * of one of the instructions **/ void run ( ) throws MachineE; }The methods step and run start executing instructions from the current value of the program counter (PC). The method step executes exactly one instruction. The method run repeatedly executes instructions until it encounters a halt instruction. Since instructions are stored in memory, the first step in executing an instruction is to fetch it into the processor. You will find it useful to have a (private) method
fetchInstruction
that performs the following actions:
Instruction
object. In other words, when we fetch a word from memory we must check if this word is an instruction. If not, we raise an exception. An excerpt of the code you need is:
if (currentWord instanceof Instruction) { Instruction currentIns = (Instruction)currentWord; ... } else throw new MemoryE ("Can only fetch instructions into the IR");
Note:
In order for you Machine program to correctly execute the current instruction, you will need to add an abstract execute method to your Instruction
class.
The help files for this exercise are at the URL
http://www.cs.uoregon.edu/~sabry/duckMachine/machine/
and include:
MachineI.java
which contains the machine interface,
TestMach.java
which contains a small program to test your code.
Machine
that implements
MachineI
and that has four constructors with the following signatures:
public Machine () public Machine (int memorySize) public Machine (InputStream istream, OutputStream ostream) public Machine (int memorySize, InputStream istream, OutputStream ostream)
TestMach.java
.
int factorial (int i) { int result = 1; while (i > 0) { result = result * i; i--; } return result; }Your assembly program should input a value for i, compute factorial(i) and print the result.
TestMach.java
to load your factorial program in memory and test it.
Machine.java
and TestMach.java
(the version with your factorial
program loaded into memory) during class on Monday, May 17th.
Last modified: 05/05/99 18:07:25
yreimer@cs.uoregon.edu