Skip to content

Commit

Permalink
always use opcode,data pairs
Browse files Browse the repository at this point in the history
Signed-off-by: yzamir <[email protected]>
  • Loading branch information
yaacov committed Oct 5, 2023
1 parent 20a15b8 commit 2901564
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/compiler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/vm.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { opcodes, opcodesParams } from './opcodes.mjs';
import { opcodes } from './opcodes.mjs';
import { throwFormattedError } from './debug.mjs';

class VM {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2901564

Please sign in to comment.