diff --git a/logging.js b/logging.js index 92f551a..7a7edc8 100644 --- a/logging.js +++ b/logging.js @@ -49,9 +49,18 @@ const num2hex = (num) => num.toString(16).toUpperCase().padStart(2, "0"); */ const hex2num = (hex) => parseInt(hex, 16); +/** + * Convert a number to binary + * See here for an explanation: https://stackoverflow.com/questions/9939760/how-do-i-convert-an-integer-to-binary-in-javascript + * @param {number} num + * @returns {string} binary representation of the input + **/ +const num2bin = (num) => (num >>> 0).toString(2).padStart(8, "0"); + module.exports = { "logMemory": logMemory, "logRunningHeader": logRunningHeader, "num2hex": num2hex, "hex2num": hex2num, + "num2bin": num2bin, } \ No newline at end of file diff --git a/run-assembler.js b/run-assembler.js index e48a191..3bf2e3a 100755 --- a/run-assembler.js +++ b/run-assembler.js @@ -1,11 +1,13 @@ #!/usr/bin/env node -// Run: `./run-assembler.js run assembly.asm` -// Debug: `./run-assembler.js debug assembly.asm` +// Run with hex output: `./run-assembler.js run assembly.asm` +// Run with binary output: `./run-assembler.js runbin assembly.asm` +// Debug: `./run-assembler.js debug assembly.asm` const fs = require('fs'); const assembler = require('./assembler.js'); -const { logMemory } = require('./logging.js'); +const { logMemory, num2hex, num2bin } = require('./logging.js'); +const { machine } = require('os'); const mode = process.argv[2]; const filename = process.argv[3]; @@ -20,5 +22,11 @@ if (mode === "debug") { console.groupEnd(); } else { machineCode = assembler.assemble(inputFile_str); - console.log(machineCode); // TODO print just the numbers + let output = ''; + if (mode === 'runbin') { // print binary output + machineCode.forEach((n) => output = `${output} ${num2bin(n)}`); + } else { // print hex output + machineCode.forEach((n) => output = `${output} ${num2hex(n)}`); + } + console.log(output); } \ No newline at end of file