Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package interpreter;
- //----------------------------------------------------------------------
- /*
- file : Interpreter.java
- date : 10/24
- author: Robby Manis
- description: Assembler interpreter
- */
- //----------------------------------------------------------------------
- public class Interpreter {
- static int PC; //program counter holds address of next instr
- static int AC; //the accumulator - a register for doing arithmetic
- static int r0; //register 0
- static int r1; //register 1
- static int r2; //register 2
- static int r3; //register 3
- static int r4; //register 4
- static boolean run_bit = true; //a bit that can be turned off to halt the machine
- 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 DEBUG = true;
- static final int CLR = 1;
- static final int ADDI = 2;
- static final int ADDM = 3;
- static final int HALT = 4;
- static final int LDI = 5;//load the AC with the value x
- static final int LDM = 6;//load the AC with the value in memory location y
- static final int ST = 7;//store the value in the AC in memory location y
- //------------------------------------------------------------------
- //This procedure interprets programs for a simple machine. The machine
- //has a register AC (accumulator), used for arithmetic. 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.
- public static void interpret ( int memory[], int starting_address ) {
- PC = starting_address;
- run_bit = true;
- 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, memory ); //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, memory); //execute instruction
- if (DEBUG) System.out.println("AC: " + AC + " PC: " + PC); //print accumulator value if in debug mode
- }
- }
- //------------------------------------------------------------------
- //since our instruction set is so simple, we'll let the opcode and
- // instruction type be the same.
- private static int get_instr_type ( int opcode ) { return opcode; }
- //------------------------------------------------------------------
- private static int find_data ( int opcode, int type, int memory[] ) {
- switch (opcode) {
- case ST:
- case LDI:
- case ADDI:
- return PC++;
- case ADDM:
- case LDM:
- return memory[PC++];
- case CLR:
- case HALT:
- PC++;
- default:
- return -1;
- }
- }
- //------------------------------------------------------------------
- private static void execute ( int type, int data, int memory[] ) {
- switch (type) {
- case ST:
- memory[data]=AC;
- break;
- case LDM:
- case LDI:
- AC = data;
- break;
- case ADDI:
- case ADDM:
- AC += data;
- break;
- case CLR:
- AC = 0;
- break;
- case HALT:
- run_bit = false;
- break;
- default:
- }
- }
- public static void main(String[] args) {
- System.out.println("Test 1");
- int m2[] = { 9,
- -5,
- CLR, 0,
- ADDI, 17,
- ADDI, 2,
- ST, 0,
- ADDM, 0,
- ADDM, 1,
- CLR, 0,
- HALT, 0
- };
- interpret( m2, 2 );
- System.out.println("\nTest 2");
- int m3[] = { 2,
- CLR, 0,
- ADDI, 2,
- ADDM, 0,
- CLR, 0,
- HALT, 0
- };
- interpret( m3, 1 );
- System.out.println("\nTest 3");
- int m4[] = { 2,
- -5,
- CLR, 0,
- ADDM, 6,
- ST, 5,
- LDM, 1,
- CLR, 0,
- HALT , 0
- };
- interpret( m4, 2 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement