diff --git a/assembler.js b/assembler.js index def5161..378d3c0 100644 --- a/assembler.js +++ b/assembler.js @@ -212,21 +212,19 @@ function decodeInstructions(source) { if (line.type === 'code') { const op = line.operation; - let sourceArgument; // TODO remove null? - if (typeof line.argument != 'undefined') { - sourceArgument = line.argument; - } else { + if (typeof line.argument === 'undefined') { // If this isn't a label definition, // or one of the ops with optional arguments, // then it's an error if (!line.operation.startsWith('@')) { - if (mnemonicsWithOptionalArgs.indexOf(line.operation) < 0) { - console.error(`Missing opcode for line ${line.number}: ${line.source}`); - throw new Error("Missing opcode"); + if (mnemonicsWithOptionalArgs.indexOf(line.operation.toLowerCase()) < 0) { + console.error(`Missing operand for line ${line.number}: ${line.source}`); + console.error(''); + throw new Error("Missing operand"); } else { // It *is* one of the special optional-arg ops // So let's fill in the implicit operand with $00 - sourceArgument = '0'; + line.argument = '0'; } } } @@ -242,13 +240,13 @@ function decodeInstructions(source) { // Opcodes - Handle constant definitions if (op.startsWith(ASM_CONSTANT_PREFIX)) { - constants = handleConstantDefinitions(op, sourceArgument, IP, constants); + constants = handleConstantDefinitions(op, line.argument, IP, constants); continue; } // Opcodes - Handle setting value of IP if (op.startsWith(ASM_IP_LABEL)) { - IP = parseInt(sourceArgument); + IP = parseInt(line.argument); continue; } @@ -267,7 +265,7 @@ function decodeInstructions(source) { line.operation = line.operation.toLowerCase(); // Operands - Handle references to labels - if (sourceArgument.startsWith(ASM_LABEL_PREFIX)) { + if (line.argument.startsWith(ASM_LABEL_PREFIX)) { let label = line.argument.substring(1); // strip label prefix if (label in labels) { dbg(1, `'${label}' already in labels object`); @@ -285,30 +283,30 @@ function decodeInstructions(source) { } // Operands - Handle references to the Instruction Pointer - if (sourceArgument === ASM_IP_LABEL) { + if (line.argument === ASM_IP_LABEL) { dbg(1, ` References current IP - ${IP}`); decodedArg = IP; } // Operands - Handle references to constants - if (sourceArgument.startsWith(ASM_CONSTANT_PREFIX)) { - dbg(1, `References '${sourceArgument}'`); - decodedArg = decodeNumericOp(constants[sourceArgument.substring(1)]); // substring(1) strips '>' + if (line.argument.startsWith(ASM_CONSTANT_PREFIX)) { + dbg(1, `References '${line.argument}'`); + decodedArg = decodeNumericOp(constants[line.argument.substring(1)]); // substring(1) strips '>' } // Operands - Handle references to constants in indirect mode - if (sourceArgument.startsWith(`(${ASM_CONSTANT_PREFIX}`)) { + if (line.argument.startsWith(`(${ASM_CONSTANT_PREFIX}`)) { addressingMode = "indirect"; - dbg(1, `(Indirectly) References '${sourceArgument}'`); - let constName = sourceArgument.replace(`(${ASM_CONSTANT_PREFIX}`, ""); + dbg(1, `(Indirectly) References '${line.argument}'`); + let constName = line.argument.replace(`(${ASM_CONSTANT_PREFIX}`, ""); constName = constName.replace(")", ""); decodedArg = decodeNumericOp(constants[constName]); } // Operands - Handle indirect expressions - if (decodedArg === null && sourceArgument.startsWith("(")) { + if (decodedArg === null && line.argument.startsWith("(")) { addressingMode = "indirect"; - let indyTemp = sourceArgument.replace("(", "").replace(")", ""); + let indyTemp = line.argument.replace("(", "").replace(")", ""); decodedArg = decodeNumericOp(indyTemp); } @@ -325,12 +323,12 @@ function decodeInstructions(source) { machineCode[IP] = decodedOp; machineCode[IP + 1] = decodedArg; - debugInfo[IP] = { - lineNumber: line.number, - source: line.source, - address: IP, - machine: [decodedOp, decodedArg] - }; + debugInfo[IP] = { + lineNumber: line.number, + source: line.source, + address: IP, + machine: [decodedOp, decodedArg] + }; dbg(3, ``);