Make assembler a javascript module, remove code for working over stdio

This commit is contained in:
n loewen 2023-07-26 15:02:21 +01:00
parent 9797b2e25f
commit ba35470c3c
1 changed files with 3 additions and 12 deletions

View File

@ -1,5 +1,3 @@
// Usage: `node assembler_sketch.js assembly.asm`
// Syntax: // Syntax:
// ADD $01 ; comments follow a `;` // ADD $01 ; comments follow a `;`
// ADD $FF ; this is direct addressing // ADD $FF ; this is direct addressing
@ -7,9 +5,6 @@
// END ; END, CFC, and CHP don't require arguments // END ; END, CFC, and CHP don't require arguments
// ; (a default value of 0 will be used as their operand) // ; (a default value of 0 will be used as their operand)
const fs = require('fs');
const filename = process.argv[2];
const mnemonicsWithOptionalArgs = ['end', 'cfc', 'chp']; const mnemonicsWithOptionalArgs = ['end', 'cfc', 'chp'];
const mnemonics2opcodes = { const mnemonics2opcodes = {
end: { direct: 0, indirect: 0 }, end: { direct: 0, indirect: 0 },
@ -32,7 +27,7 @@ function decodeMultipleInstructions(str) {
output.push(decoded.op); output.push(decoded.op);
output.push(decoded.arg); output.push(decoded.arg);
}); });
return output; return new Uint8Array(output);
} }
function decodeInstruction(line) { function decodeInstruction(line) {
@ -80,10 +75,6 @@ function hex2num(hex) { return parseInt(hex, 16) };
// RUN IT // RUN IT
try { exports.assemble = (str) => {
// console.log(`Reading ${filename}`); return decodeMultipleInstructions(str);
const inputFile_str = fs.readFileSync(filename, 'utf8');
console.log(decodeMultipleInstructions(inputFile_str));
} catch (err) {
console.error(err);
} }