From 9a3d9cffdcb4635f3aef9bb3b8b54d1924d2e995 Mon Sep 17 00:00:00 2001 From: n loewen Date: Wed, 2 Aug 2023 13:56:10 +0100 Subject: [PATCH] Turn print-memory.js into a more general-purpose 'printing utils' library -- export num2hex + rename function that prints a table of memory --- assemble.js | 4 ++-- assembler.js | 10 +++++----- print-memory.js | 6 ++++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/assemble.js b/assemble.js index 7f71dab..58dad62 100644 --- a/assemble.js +++ b/assemble.js @@ -3,7 +3,7 @@ const fs = require('fs'); const assembler = require('./assembler.js'); -const printMemory = require('./print-memory.js'); +const { logMemory } = require('./print-memory.js'); const mode = process.argv[2]; const filename = process.argv[3]; @@ -14,7 +14,7 @@ let machineCode; if (mode === "debug") { machineCode = assembler.assemble(inputFile_str, true); console.group("Machine code output"); -printMemory.printTable(machineCode); + logMemory(machineCode); console.groupEnd('Machine code output'); } else { machineCode = assembler.assemble(inputFile_str); diff --git a/assembler.js b/assembler.js index 12bd9fe..44c2654 100644 --- a/assembler.js +++ b/assembler.js @@ -1,4 +1,4 @@ -const printMemory = require('./print-memory.js'); +const { logMemory, num2hex } = require('./print-memory.js'); const { INITIAL_IP_ADDRESS, START_OF_DISPLAY_MEM } = require('./machine.config.js'); // 1 = verbose @@ -36,13 +36,13 @@ function decodeInstructions(str) { let IP = INITIAL_IP_ADDRESS; for (let i = 0; i < lines.length; i++) { dbg(1, ''); - dbgGroup(1, `Input line ${i}, IP ${IP}`); + dbgGroup(1, `Input line ${i}, IP ${num2hex(IP)}`); dbg(3, `> ${lines[i]}`); let line = stripWhitespaceFromEnds(stripComments(lines[i])); // Handle blank lines if (line.length === 0) { - dbg(3, `IP: ${IP}, new code: none`); + dbg(3, `IP: $${num2hex(IP)}, new code: none`); dbg(1, 'blank'); dbgGroupEnd(1, 'Input line'); continue; @@ -64,7 +64,7 @@ function decodeInstructions(str) { } dbg(2, `pointsToByte: ${labels[label].pointsToByte}`); dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`); - dbg(3, `IP: ${IP}, new code: none`); + dbg(3, `IP: $${num2hex(IP)}, new code: none`); dbgGroupEnd(1, 'Input line'); continue; } @@ -123,8 +123,8 @@ function decodeInstructions(str) { machineCode.push(op); machineCode.push(arg_num); + dbg(3, `IP: $${num2hex(IP)}, new code: $${num2hex(op)} $${num2hex(arg_num)}`); IP += 2; - dbg(3, `IP: ${IP}, new code: ${op}, ${arg_num}`); dbgGroupEnd(1, 'Input line'); }; diff --git a/print-memory.js b/print-memory.js index 3533fc7..bfd7259 100644 --- a/print-memory.js +++ b/print-memory.js @@ -1,4 +1,4 @@ -exports.printTable = (x) => { +exports.logMemory = (x) => { console.log(`┌────────┬────────┬────────┐`); console.log(`│ addr │ op │ arg │`); console.log(`├────────┼────────┼────────┤`); @@ -11,4 +11,6 @@ exports.printTable = (x) => { console.log(`└────────┴────────┴────────┘`); } -const num2hex = (num) => num.toString(16).toUpperCase().padStart(2, "0"); \ No newline at end of file +const num2hex = (num) => num.toString(16).toUpperCase().padStart(2, "0"); + +exports.num2hex = num2hex; \ No newline at end of file