Browse Source

Implement OUTPUT opcode

ApisNecros 1 year ago
parent
commit
f3836fa28c
1 changed files with 14 additions and 0 deletions
  1. 14 0
      IntComp/Computer.js

+ 14 - 0
IntComp/Computer.js

@@ -11,6 +11,7 @@ module.exports = class Computer {
         this.OPCODES = {
         this.OPCODES = {
             ADD: 1,
             ADD: 1,
             MULTIPLY: 2,
             MULTIPLY: 2,
+            OUTPUT: 4,
             HALT: 99,
             HALT: 99,
         };
         };
         this.PARAMETER_MODES = {
         this.PARAMETER_MODES = {
@@ -58,6 +59,10 @@ module.exports = class Computer {
                 this.Operation_Multiply(operandLeft, operandRight, position);
                 this.Operation_Multiply(operandLeft, operandRight, position);
                 break;
                 break;
             }
             }
+            case this.OPCODES.OUTPUT: {
+                const outputPosition = this.stack.Next().Get();
+                this.Operation_Output(outputPosition);
+            }
             case this.OPCODES.HALT:
             case this.OPCODES.HALT:
                 status = false;
                 status = false;
                 break;
                 break;
@@ -102,6 +107,15 @@ module.exports = class Computer {
         this.stack.Put(outputPosition, newValue);
         this.stack.Put(outputPosition, newValue);
     }
     }
 
 
+    /**
+     * Execute the OUTPUT opcode
+     *
+     * @param {number} outputPosition The memory address of the value to output
+     */
+    Operation_Output(outputPosition) {
+        console.log(this.stack.Get(outputPosition));
+    }
+
     /**
     /**
      * Outputs the computer's stack to the console
      * Outputs the computer's stack to the console
      * @returns {void}
      * @returns {void}