Feature: assembler output in binary

This commit is contained in:
n loewen 2023-08-10 10:04:18 +01:00
parent 1f7e87e705
commit a78c1b9d14
2 changed files with 21 additions and 4 deletions

View File

@ -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,
}

View File

@ -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);
}