From 12d592c262a75f8a7b5de29fc88f66fbbe767b05 Mon Sep 17 00:00:00 2001 From: n loewen Date: Tue, 29 Aug 2023 19:32:43 -0400 Subject: [PATCH] assembler - WIP - Add a 'return on stdout' mode that turns off all debug logging, and prints machine code output to stdout, so that output can be piped directly to the simulator --- src/assembler.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/assembler.js b/src/assembler.js index 2b20ffc..ae2d503 100755 --- a/src/assembler.js +++ b/src/assembler.js @@ -404,7 +404,9 @@ function hex2num(hex) { return parseInt(hex, 16) }; /** MAIN **/ // Initialize debugger -const dbg = new DBG('nitpick'); +let dbg = new DBG('nitpick'); +let returnOnStdout = true; // FIXME - set this using a CLI arg +if (returnOnStdout) { dbg = new DBG('none'); } // Get input const filename = process.argv[2]; // FIXME - Get filename in a more robust way @@ -414,13 +416,24 @@ assemble(inputFile_str, outputFilename); /** - * @param {string} assemblyCode - * @param {string} outputFile + * Assemble source code into machine code. + * If 'includeMetadata' is true, a JSON object containing + * both machine code and metadata is written to the output file. + * Otherwise, a string of decimal numbers is written. + * @arg {string} sourceCode - Source code to assemble + * @arg {string} [outputFile='out.txt'] - Output file for machine code (and optional metadata) + * @arg {boolean} [includeMetadata=false] - Include metadata for use when debugging using the simulator? **/ -function assemble(assemblyCode, outputFile='out.txt', debuggable='false') { - const out = decodeInstructions(assemblyCode); +function assemble(sourceCode, outputFile='out.txt', includeMetadata=false) { + const out = decodeInstructions(sourceCode); - if (!debuggable) { + if (returnOnStdout) { + const debugJSON = JSON.stringify(out); + console.log(debugJSON); + return; + } + + if (!includeMetadata) { const asciiMachineCode = out.machineCode.toString().replace(/,/g, ' '); fs.writeFileSync(outputFile, asciiMachineCode); } else {