Guest User

Untitled

a guest
Oct 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. //----------------------------------------------------------------------
  2. /*
  3. *file : Interpreter.java
  4. *date : 10/22/17
  5. *author: @epomp447
  6. *description: Simple program that mimics a basic machine with a PC, AC and
  7. *simple instructions.
  8. */
  9. //----------------------------------------------------------------------
  10. public class Interpreter {
  11. static int PC; // program counter holds address of next instr
  12. static int AC; // the accumulator - a register for doing arithmetic
  13. static boolean run_bit = true; // a bit that can be turned off to halt the machine
  14. static int instr; // a holding register for the current instruction
  15. static int instr_type; // the instruction type (opcode)
  16. static int data_loc; // the address of the data, or -1 if none
  17. static int data; // holds the current operand
  18. static final int CLR = 90;// <-- set the value in the AC to 0
  19. static final int ADDI = 95;// <-- add the value x to the AC
  20. static final int ADDM = 93;// <-- add the value in memory location y to AC
  21. static final int HALT = 100;// <-- instruction which halts the processor
  22. // ------------------------------------------------------------------
  23. // This procedure interprets programs for a simple machine. The machine
  24. // has a register AC (accumulator), used for arithmetic. The interpreter
  25. // keeps running until the run bit is turned off by the HALT instruction.
  26. // The state of a process running on this machine consists of the memory,
  27. // the program counter, the run bit, and the AC. The input parameters
  28. // consist of the memory image and the starting address.
  29. public static void interpret(int memory[], int starting_address) {
  30. PC = starting_address;
  31. run_bit = true;
  32. while (run_bit) {
  33. instr = memory[PC]; // fetch next instruction into instr
  34. PC = PC + 1; // increment program counter
  35. instr_type = get_instr_type(instr); // determine instruction type
  36. data_loc = find_data(instr, instr_type, memory); // locate data (-1 if none)
  37. if (data_loc >= 0) { // if data_loc is -1, there is no operand
  38. data = memory[data_loc]; // fetch the data
  39. }
  40. execute(instr_type, data); // execute instruction
  41. }
  42. }
  43. // ------------------------------------------------------------------
  44. // since our instruction set is so simple, we'll let the opcode and
  45. // instruction type be the same.
  46. private static int get_instr_type(int opcode) {
  47. return opcode;
  48. }
  49. // ------------------------------------------------------------------
  50. private static int find_data(int opcode, int type, int[] memory) {
  51. if (opcode == ADDI) {
  52. return PC;
  53. }
  54. if (opcode == ADDM) {
  55. return memory[PC];
  56. } else
  57. return -1;
  58. }
  59. // ------------------------------------------------------------------
  60. private static void execute(int instr_type, int data) {
  61. if (instr_type == CLR) {
  62. AC = 0;
  63. System.out.println(AC);
  64. }
  65. if (instr_type == ADDI) {
  66. AC = AC + data;
  67. System.out.println(AC);
  68. }
  69. if (instr_type == ADDM) {
  70. AC = AC + data;
  71. System.out.println(AC);
  72. }
  73. if (instr_type == HALT) {
  74. run_bit = false;
  75. }
  76. }
  77. // ----------------------------------------------------------------------
  78. public static void main(String[] args) {
  79. int m2[] = { 2, -5, 15, CLR, // "program" starts here
  80. ADDI, 12, ADDI, 7, ADDM, 0, ADDM, 1, CLR, HALT };
  81. System.out.println("Memory image 1: ");
  82. interpret(m2, 3);// start at CLR
  83.  
  84. int m3[] = { 1, 3, 5, CLR, // "program" starts here
  85. ADDI, 7, ADDM, 2, CLR, ADDM, 0, ADDM, 1, CLR, HALT };
  86. System.out.println("Memory image 2: ");
  87. interpret(m3, 3); // start at CLR
  88.  
  89. int m4[] = { 13, -5, 7, 8, CLR,//"program" starts here
  90. ADDM, 1, 3, ADDM, 1, CLR, HALT };
  91. System.out.println("Memory image 3: ");
  92. interpret(m4, 4);
  93. }
  94. }
Add Comment
Please, Sign In to add comment