assembler - Fix test for argument-less operations / Remove unnecessary variable

This commit is contained in:
n loewen 2023-08-24 15:16:12 +01:00
parent 51bc13fe5b
commit dc78518b73
1 changed files with 24 additions and 26 deletions

View File

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