Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. /****************************************************
  2. VIRTUAL MACHINE
  3. VERSION 1.0
  4. Kevin Taylor
  5.  
  6.  
  7. *****************************************************/
  8. public class VirtualMachine{
  9.  
  10. private int pc = 0;
  11. private int[] memory = {8,5,
  12. 8,2,
  13. 11,
  14. 10, 16,
  15. 9, 16,
  16. 21,
  17. 0,
  18. 0,0,0,0,0,0,0,0,0,0,0,0};
  19. private IntStack stack = new IntStack(20);
  20. private int d, x, y, ir;
  21.  
  22.  
  23. public VirtualMachine(int pc, int[] memory, IntStack stack){
  24. this.pc = pc;
  25. this.memory = memory;
  26. this.stack = stack;
  27. this.x = x;
  28. this.y = y;
  29. this.ir = ir;
  30. this.d = d;
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. //Switch Statement for handling op code instructions in the VM
  43.  
  44. do{
  45. ir = memory[pc];
  46. pc++;
  47.  
  48.  
  49.  
  50.  
  51. switch(ir){
  52.  
  53. case 8: // Load Constant
  54.  
  55. stack.push( memory[pc] );
  56. pc++;
  57. break;
  58.  
  59. case 9: //Load Variable from addess
  60.  
  61. d = memory[pc];
  62. stack.push(d);
  63. pc++;
  64. break;
  65.  
  66.  
  67. case 10: //store variable in address
  68.  
  69. d = stack.pop();
  70. memory[memory[pc]] = d;
  71. pc++;
  72. break;
  73.  
  74. case 11: // add two operands and push the result
  75.  
  76. y = stack.pop();
  77. x = stack.pop();
  78. d = x + y;
  79. stack.push(d);
  80. pc++;
  81. break;
  82.  
  83. case 12: //subtract top two operands and push the result
  84.  
  85. y = stack.pop();
  86. x = stack.pop();
  87. d = x - y;
  88. stack.push(d);
  89. pc++;
  90. break;
  91.  
  92. case 13: //multiply top two operands and push the result
  93.  
  94. y = stack.pop();
  95. x = stack.pop();
  96. d = x * y;
  97. stack.push(d);
  98. pc++;
  99. break;
  100.  
  101. case 14: //Divide top two operands and push the result
  102.  
  103. y = stack.pop();
  104. x = stack.pop();
  105. d = x / y;
  106. stack.push(d);
  107. pc++;
  108. break;
  109.  
  110. case 15: // Check if equal
  111.  
  112. y = stack.pop();
  113. x = stack.pop();
  114.  
  115. if( x==y)
  116. stack.push(1);
  117.  
  118. else
  119. stack.push(0);
  120.  
  121. break;
  122.  
  123. case 16: //Check if less
  124.  
  125. y = stack.pop();
  126. x = stack.pop();
  127.  
  128. if(x<y)
  129. stack.push(1);
  130.  
  131. else
  132. stack.push(0);
  133.  
  134.  
  135. break;
  136.  
  137. case 17: //Check if greater
  138.  
  139. y = stack.pop();
  140. x = stack.pop();
  141.  
  142. if(x>y)
  143. stack.push(1);
  144.  
  145. else
  146. stack.push(0);
  147.  
  148.  
  149. break;
  150.  
  151.  
  152. case 18: //jump to address pc
  153.  
  154. pc = memory[pc];
  155. break;
  156.  
  157. case 19: //jump if false
  158.  
  159. if(stack.pop() == 0)
  160. pc = memory[pc];
  161. break;
  162.  
  163. case 20: //read an int
  164.  
  165. d = memory[pc];
  166. stack.push(d);
  167. pc++;
  168. break;
  169.  
  170. case 21: //write an int
  171.  
  172. System.out.println(stack.pop());
  173. pc++;
  174. break;
  175.  
  176. case 22: //call subprogram
  177.  
  178. stack.push(pc + 1);
  179. pc = memory[pc];
  180. break;
  181.  
  182. case 23: //return from subprogram
  183.  
  184. pc = stack.pop();
  185. break;
  186.  
  187.  
  188.  
  189.  
  190. case 0: //halt program
  191.  
  192. System.exit(0);
  193. break;
  194.  
  195.  
  196. } //end do
  197. }//end Switch
  198. while(true);
  199.  
  200.  
  201.  
  202. }// end vm constructer
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. public static void main(String[] args){
  210.  
  211. int pc = 0;
  212. IntStack stack = new IntStack(20);
  213. int[] memory = {8,5,
  214. 8,2,
  215. 11,
  216. 10, 16,
  217. 9, 16,
  218. 21,
  219. 0,
  220. 0,0,0,0,0,0,0,0,0,0,0,0};
  221.  
  222.  
  223.  
  224. VirtualMachine vm = new VirtualMachine(pc, memory, stack );
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. }// end main
  233.  
  234. }//end Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement