assembler / run-assembler - Make assembler get input from stdin! / Remove `run-assembler`
This commit is contained in:
parent
41d7b7ba54
commit
7faf190fe2
|
|
@ -405,14 +405,25 @@ function hex2num(hex) { return parseInt(hex, 16) };
|
|||
const dbg = new DBG('nitpick');
|
||||
|
||||
// Get input
|
||||
const filename = process.argv[2]; // FIXME
|
||||
const filename = process.argv[2]; // FIXME - Get filename in a more robust way
|
||||
const outputFilename = process.argv[3]; // FIXME - Get filename in a more robust way
|
||||
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
||||
assemble(inputFile_str);
|
||||
assemble(inputFile_str, outputFilename);
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} assemblyCode
|
||||
* @param {string} outputFile
|
||||
**/
|
||||
function assemble(assemblyCode) {
|
||||
return decodeInstructions(assemblyCode);
|
||||
function assemble(assemblyCode, outputFile='out.txt', debuggable='false') {
|
||||
const out = decodeInstructions(assemblyCode);
|
||||
|
||||
if (!debuggable) {
|
||||
const asciiMachineCode = out.machineCode.toString().replace(/,/g, ' ');
|
||||
fs.writeFileSync(outputFile, asciiMachineCode);
|
||||
} else {
|
||||
const debugJSON = JSON.stringify(out);
|
||||
dbg.nit(debugJSON);
|
||||
fs.writeFileSync(outputFile, debugJSON);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
// 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, num2hex, num2bin } = require('./logging.js');
|
||||
const machineConfig = require('./machine.config.js');
|
||||
|
||||
const mode = process.argv[2];
|
||||
const filename = process.argv[3];
|
||||
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
||||
|
||||
let assembler_output;
|
||||
|
||||
if (mode === "debug") {
|
||||
assembler_output = assembler.assemble(inputFile_str, true);
|
||||
console.log('');
|
||||
console.group("Machine code output");
|
||||
logMemory(assembler_output.machineCode, machineConfig.INITIAL_IP_ADDRESS);
|
||||
console.groupEnd();
|
||||
} else {
|
||||
assembler_output = assembler.assemble(inputFile_str);
|
||||
let output = '';
|
||||
if (mode === 'runbin') { // print binary output
|
||||
assembler_output.machineCode.forEach((n) => output = `${output} ${num2bin(n)}`);
|
||||
} else { // print hex output
|
||||
assembler_output.machineCode.forEach((n) => output = `${output} ${num2hex(n)}`);
|
||||
}
|
||||
console.log(output);
|
||||
}
|
||||
Loading…
Reference in New Issue