From d6b55db381d718f242721b5868450143c8a74dc3 Mon Sep 17 00:00:00 2001 From: n loewen Date: Thu, 24 Aug 2023 15:26:37 +0100 Subject: [PATCH] assembler - Add feature: allow use of `*` with a numeric offset --- assembler.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/assembler.js b/assembler.js index 378d3c0..44b52b1 100644 --- a/assembler.js +++ b/assembler.js @@ -53,6 +53,7 @@ const mnemonics2opcodes = { * @property {SourceLineType} type - line type * @property {string} [operation] - For code: the first non-whitespace chunk * @property {string} [argument] - For code: the second non-whitespace chunk, if there is one + * @property {string} [extraArgument] - For code: the third non-whitespace chunk, if there is one **/ /** @@ -87,13 +88,20 @@ function preparseSourceCode(source) { dbg(1, ``); if (info.type === 'code') { - const op_arg_array = info.sanitized.split(/\s+/); // split line into an array of [op, arg] + const op_arg_array = info.sanitized.split(/\s+/); // split line into an array of [op, arg, extra_arg] if (op_arg_array[0] !== 'undefined') { info.operation = op_arg_array[0]; } if (op_arg_array.length === 2) { info.argument = op_arg_array[1]; } + if (op_arg_array.length === 3) { + info.argument = op_arg_array[1]; + info.extraArgument = op_arg_array[2]; + } + if (op_arg_array.length > 3) { + console.error('Error on line ${info.number}: Too many arguments'); + } } return info; }); @@ -285,7 +293,11 @@ function decodeInstructions(source) { // Operands - Handle references to the Instruction Pointer if (line.argument === ASM_IP_LABEL) { dbg(1, ` References current IP - ${IP}`); - decodedArg = IP; + if (typeof line.extraArgument === 'undefined') { + decodedArg = IP; + } else { + decodedArg = IP + decodeNumericOp(line.extraArgument); + } } // Operands - Handle references to constants