Browse Source

Add LESS_THAN and EQUALS opcodes

Added opcodes for the two currently available equality checks. Added
them such that they should be extendable in the future if need be.
ApisNecros 1 year ago
parent
commit
340dce1ade
1 changed files with 44 additions and 0 deletions
  1. 44 0
      IntComp/Computer.js

+ 44 - 0
IntComp/Computer.js

@@ -31,6 +31,11 @@ module.exports = class Computer {
             HALT: 99,
         };
 
+        this.EQUALITY = {
+            EQUALS: 0,
+            LESS_THAN: 1,
+        };
+
         this.parameterMode = ComputerParameterMode.POSITION_MODE;
 
         /**
@@ -106,6 +111,14 @@ module.exports = class Computer {
                 this.Operation_JumpIf(rawOpcode, false);
                 break;
             }
+            case this.OPCODES.LESS_THAN: {
+                this.Operation_Equality(rawOpcode, this.EQUALITY.LESS_THAN);
+                break;
+            }
+            case this.OPCODES.EQUALS: {
+                this.Operation_Equality(rawOpcode, this.EQUALITY.EQUALS);
+                break;
+            }
             case this.OPCODES.HALT:
                 status = false;
                 break;
@@ -237,6 +250,37 @@ module.exports = class Computer {
         }
     }
 
+    /**
+     * Execute the various equality checking opcodes
+     *
+     * @param {number} rawOpcode The opcode in memory used to make this call
+     * @param {number} testCondition The type of equality check to perform as defined in the computer's constructor
+     * @returns {void}
+     */
+    Operation_Equality(rawOpcode, testCondition) {
+        const operandLeftMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 1);
+        const operandRightMode = ComputerParameterMode.ParseParameterMode(rawOpcode, 2);
+
+        const operandLeft = this.stack.Next().Get(operandLeftMode);
+        const operandRight = this.stack.Next().Get(operandRightMode);
+        const outputPosition = this.stack.Next().Get(ComputerParameterMode.IMMEDIATE_MODE);
+
+        let testPassed = false;
+
+        switch (testCondition) {
+            case this.EQUALITY.EQUALS:
+                testPassed = operandLeft == operandRight;
+                break;
+            case this.EQUALITY.LESS_THAN:
+                testPassed = operandLeft < operandRight;
+                break;
+            default:
+                break;
+        }
+
+        this.stack.Put(outputPosition, Number(testPassed));
+    }
+
     /**
      * Outputs the computer's stack to the console
      *