Homework 5Hypothetical Computer Implementation |
Modified: |
As discussed in class, a hypothetical computer architecture can be implemented in software to model the architecture and interpret each hypothetical computer machine instruction. The purpose of this exercise is to gain understanding of how simple interpreters work and some experience using Java as the implementation language for the interpreter.
The interpreter program has two main parts, 1) a loader for both data values and algorithm machine code, and 2) a machine language instruction interpreter. Recall that all details of the hypothetical machine architecture are to be simulated in software including the organization of memory into areas separate for data and algorithm storage.
Load - The loader must store the initial data (everything up to the first +9999999) into the data area. Then store machine instructions (everything up to the next +9999999) into the algorithm area. After both the data and instructions have been loaded execution of the instructions can start by simulating the fetch/execute cycle of the hypothetical machine.
The structure of a machine language program is:
Interpret - After the program data and algorithm (machine instructions) are loaded into the Hypothetical Machines memory, the program can be executed.
Execution is performed by fetching the next machine instruction of the algorithm and executing. This process is commonly called interpreting the program when performed by software rather than directly by hardware. This is explained in the notes but briefly the steps required are:
An incomplete interpreter is listed below.
// Hypothetical Machine Interpreter
public class Interpreter {
static float Data[] = new float[100];
static int Algorithm[] = new int[100];
public static void main(String args[]) throws Exception
{
Load();
Interpret();
}
static void Interpret()
{
int IP;
boolean TraceOn = false;
int Instruction;
int Op;
int Opn1;
int Opn2;
int Dest;
System.out.print("\tIP\tOp\tOpr1\tOpr2\tDest\n");
IP = 0;
while (true) {
// Fetch
Instruction = Algorithm[IP];
// Decode
Op = Instruction / 1000000;
Dest = Math.abs(Instruction % 100);
Opn2 = 0;
Opn1 = 0;
if (TraceOn)
System.out.println("\t"+IP+"\t"+Op+"\t"+Opn1+"\t"+Opn2+"\t"+Dest);
// Update IP
IP = IP + 1;
// Execute
switch (Op) {
case 0 : Data[Dest] = Data[Opn1]; break;
case 1 : Data[Dest] = Data[Opn1] + Data[Opn2]; break;
case 2 : Data[Dest] = Data[Opn1] * Data[Opn2]; break;
case 3 : Data[Dest] = Data[Opn1] * Data[Opn1]; break;
case 4 : if (Data[Opn1] == Data[Opn2]) IP = Dest; break;
case 5 : if (Data[Opn1] >= Data[Opn2]) IP = Dest; break;
case 6 : Data[Dest] = Data[Opn1 + (int)Data[Opn2]]; break;
case 7 : Data[Opn1] = Data[Opn1] + 1;
if (Data[Opn1] < Data[Opn2]) IP = Dest; break;
case 8 : Data[Dest] = C311.readFloat(); break;
case 9 : return;
case -1 :
case -2 :
case -3 :
case -4 :
case -5 :
case -6 :
case -7 :
case -8 :
case -9 : TraceOn = !TraceOn;
}
}
}
static void Load()
{
int DataMemoryLocation;
float DataValue;
DataMemoryLocation = 0;
while ((DataValue = C311.readFloat()) != 9999999.0){
Data[ DataMemoryLocation ] = DataValue;
DataMemoryLocation = DataMemoryLocation + 1;
}
int AlgorithmMemoryLocation;
int Instruction;
AlgorithmMemoryLocation = 0;
while ((Instruction = C311.readInt()) != 9999999){
Algorithm[ AlgorithmMemoryLocation ] = Instruction;
AlgorithmMemoryLocation = AlgorithmMemoryLocation + 1;
}
}
}
|
| +0000000 (00)
Constant 00 +0000000 (01) i +0000000 (02) j +0000000 (03) min +0000000 (04) n +0000000 (05) temp1 +0000000 (06) temp2 +0000000 (07) A +9999999 End of Initial Data -9000000 (00) (-9000000 turns on trace ) +8000004 (01) Read n +8000005 (02) Read temp1 -6050701 (03) A[i] = temp1 +7010402 (04) increment i, if i<n goto 2 +0000001 (05) i=0 +0010003 (06) min=i +0010002 (07) j=i +6070205 (08) temp1=A[j] +6070306 (09) temp2=A[min] -5060512 (10) if A[min] < A[j] goto 12 +0020003 (11) min=j +7020408 (12) increment j, if j<n goto 8 +6070305 (13) temp1=A[min] +6070106 (14) temp2=A[i] -6050701 (15) A[i]=temp1 -6060703 (16) A[min]=temp2 +7010406 (17) increment i, if i<n goto 6 +0000001 (18) i=0 +6070105 (19) temp1=A[i] -8050000 (20) print temp1 +7010419 (21) increment i, if i<n goto 19 +9000000 (22) Stop +9999999 End of program +0000005 n +0000028 Data Values to sort +0000020 +0000021 +0000025 +0000024 |
A[min] <-> A[i]
exchanges the min and i variables of array A.
Note the two new instructions exceed the number of unused codes. Consider
replacing some operation not used by the sort (e.g. sqrt).
| +6070305 (13) temp1=A[min] +6070106 (14) temp2=A[i] -6050701 (15) A[i]=temp1 -6060703 (16) A[min]=temp2 |
Your exchange instruction should only occupy one of the four instruction words. Replace the remaining three words with your no operation.
java -cp . Interpreter < pseudo.pgm > output
then print the file named output.
java -cp . Interpreter < sort.pgm > output
then print the file named output.
File links Sort program sort.pgm Class for int and float C311.class Completed Interpreter for verification Interpreter.class Starter Interpreter for your assignment Copy Interpreter.java above.
Compiling and executing at IUS
javac -classpath . Interpreter.java
java -cp . Interpreter < sort.pgm
Compiling and executing at Home - Java is freely available from the SUN Java website. Download the software and follow the instructions for installing the Java SDK. With the SDK installed, the Interpreter can be executed by (note that Java is case-sensitive everywhere, including the DOS command prompt line):
javac -classpath . Interpreter.java
java -cp . Interpreter < sort.pgm
x = Math.abs(x);
System.out.print("Hello World");
in C++ it would be:
cout << "Hello World";
The simplest is to use:
System.out.print( expr );
where expr is a string, int, float, etc. expression. To output several expressions in one output statement use concatenation (i.e. the + operator), for example to output the value of IP and op variables seperated by a tab and terminated by a new line use:
System.out.print(IP + "\t" + op + "\n");
System.out.println();
or by outputting a "\n".
Document last modified: