From dab111b6ad07ac7ff4d5f35ca61967a6eeeacb57 Mon Sep 17 00:00:00 2001 From: n loewen Date: Tue, 1 Aug 2023 15:38:31 +0100 Subject: [PATCH] Rename `byteCursor` to `IP` + update debugging setup --- sketches/assembler.js | 55 ++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/sketches/assembler.js b/sketches/assembler.js index aee580b..0c1ac3d 100644 --- a/sketches/assembler.js +++ b/sketches/assembler.js @@ -10,11 +10,11 @@ const printMemory = require('./print-memory.js'); -// 0 = silent // 1 = verbose // 2 = what i'm currently focusing on // 3 = always print -const DEBUG = 2; +// 4 = silent +const DEBUG = 4; const INITIAL_IP_ADDRESS = 32; const mnemonicsWithOptionalArgs = ['end', 'cfc', 'chp']; @@ -34,18 +34,18 @@ function decodeInstructions(str) { let lines = str.split(/\n/); // returns an array of lines let machineCode = new Array(INITIAL_IP_ADDRESS).fill(0); let labels = {}; - let byteCursor = INITIAL_IP_ADDRESS; + let IP = INITIAL_IP_ADDRESS; for (let i = 0; i < lines.length; i++) { - console.log(); - console.group(`Input line ${i}, cursor ${byteCursor}`); + dbg(1, ''); + dbgGroup(1, `Input line ${i}, IP ${IP}`); dbg(3, `> ${lines[i]}`); let line = stripWhitespaceFromEnds(stripComments(lines[i])); // Handle blank lines if (line.length === 0) { - dbg(3, `cursor: ${byteCursor}, new code: none`); + dbg(3, `IP: ${IP}, new code: none`); dbg(1, 'blank'); - console.groupEnd('Input line'); + dbgGroupEnd(1, 'Input line'); continue; } @@ -56,17 +56,17 @@ function decodeInstructions(str) { label = line.substring(1); // strip '@' if (label in labels) { - labels[label].pointsToByte = byteCursor; + labels[label].pointsToByte = IP; } else { labels[label] = { - pointsToByte: byteCursor, + pointsToByte: IP, bytesToReplace: [], }; } dbg(2, `pointsToByte: ${labels[label].pointsToByte}`); dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`); - dbg(3, `cursor: ${byteCursor}, new code: none`); - console.groupEnd('Input line'); + dbg(3, `IP: ${IP}, new code: none`); + dbgGroupEnd(1, 'Input line'); continue; } @@ -91,28 +91,35 @@ function decodeInstructions(str) { if (label in labels) { dbg(1, `'${label}' already in labels object`); - labels[label].bytesToReplace.push(byteCursor + 1); + labels[label].bytesToReplace.push(IP + 1); } else { dbg(1, `'${label}' NOT in labels object`); labels[label] = { - bytesToReplace: [byteCursor + 1], + bytesToReplace: [IP + 1], }; } dbg(2, `pointsToByte: ${labels[label].pointsToByte}`); dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`); } else if (arg_str.startsWith("(")) { // Handle indirect expressions + dbg(2, "INDIRECT"); addressingMode = "indirect"; arg_str = arg_str.replace("(", ""); arg_str = arg_str.replace(")", ""); - arg_num = parseInt(arg_str); - } else if (arg_str.startsWith("$")) { - // Handle direct expressions - arg_str = arg_str.replace("$", ""); - arg_num = hex2num(arg_str); - } else { - // Accept decimal i guess - arg_num = parseInt(arg_str); + } + + // Handle numeric operands + dbg(2, `ARG NUM ${arg_num}`); + if (arg_num === null) { + dbg(2, `ARG STR ${arg_str}`); + if (arg_str.startsWith("$")) { + // Handle hex + arg_str = arg_str.replace("$", ""); + arg_num = hex2num(arg_str); + } else { + // Accept decimal i guess + arg_num = parseInt(arg_str); + } } // Decode! @@ -120,9 +127,9 @@ function decodeInstructions(str) { machineCode.push(op); machineCode.push(arg_num); - byteCursor += 2; - dbg(3, `cursor: ${byteCursor}, new code: ${op}, ${arg_num}`); - console.groupEnd('Input line'); + IP += 2; + dbg(3, `IP: ${IP}, new code: ${op}, ${arg_num}`); + dbgGroupEnd(1, 'Input line'); }; dbg(1, '');