From 2901564d28aeec63c371602e49028830e7ad4a67 Mon Sep 17 00:00:00 2001 From: yzamir Date: Thu, 5 Oct 2023 13:43:15 +0300 Subject: [PATCH] always use opcode,data pairs Signed-off-by: yzamir --- src/compiler.mjs | 10 +++++++--- src/vm.mjs | 5 +++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/compiler.mjs b/src/compiler.mjs index a484f5c..449b458 100644 --- a/src/compiler.mjs +++ b/src/compiler.mjs @@ -100,7 +100,7 @@ function processLabel(instruction, operands, labels, address) { address += operands.length; // For each data value } else if (instruction in opcodes) { address += 1; // For the opcode - address += operands.length; // For each operand + address += 1; // We always have one opcode and one operand } return address; // Return the updated address @@ -140,10 +140,14 @@ function processInstruction(instruction, operands, memory, labels, memoryMapping throwFormattedError('Wrong number of operands', instruction, memory.length); } - operands.forEach((operand) => { + // In case of a command with no params, add a placeholder to align with [opcode, data] pairs + if (operands.length === 1) { + const operand = operands[0]; const operandValue = findKeyByValue(labels, operand) || parseValue(operand); addToMemory(operandValue, 'operand', null, operand); - }); + } else { + addToMemory(0, '', undefined); + } } else if (instruction) { // Note: a valid line with a lable and no opCode will have an instruction == undefined. throwFormattedError('Invalid instruction', instruction, memory.length); diff --git a/src/vm.mjs b/src/vm.mjs index 2edb8dd..b87e34a 100644 --- a/src/vm.mjs +++ b/src/vm.mjs @@ -1,4 +1,4 @@ -import { opcodes, opcodesParams } from './opcodes.mjs'; +import { opcodes } from './opcodes.mjs'; import { throwFormattedError } from './debug.mjs'; class VM { @@ -85,7 +85,8 @@ class VM { // Dont incriment the counter on jumps if (!jumpFlag) { - this.pc += (1 + opcodesParams[instruction]); + // 1 opcode + 1 data (we always have paris of 1 opcode and 1 data bytes) + this.pc += 2; } // Memory gurd