32 lines
1.0 KiB
JavaScript
Executable File
32 lines
1.0 KiB
JavaScript
Executable File
#!/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 { machine } = require('os');
|
|
|
|
const mode = process.argv[2];
|
|
const filename = process.argv[3];
|
|
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
|
|
|
let machineCode;
|
|
|
|
if (mode === "debug") {
|
|
machineCode = assembler.assemble(inputFile_str, true);
|
|
console.group("Machine code output");
|
|
logMemory(machineCode);
|
|
console.groupEnd();
|
|
} else {
|
|
machineCode = assembler.assemble(inputFile_str);
|
|
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);
|
|
} |