diff --git a/assembler.js b/assembler.js index aac84df..4ac7035 100644 --- a/assembler.js +++ b/assembler.js @@ -13,6 +13,8 @@ exports.assemble = (str, debug = false) => { return decodeInstructions(str); } +const POINTER_TO_CURRENT_ADDR_PSEUDO_OPERAND = '*addr'; + const mnemonicsWithOptionalArgs = ['end', 'nop']; const mnemonics2opcodes = { end: { direct: 0, indirect: 0 }, @@ -89,6 +91,9 @@ function decodeInstructions(line) { // FIXME - a quick hack to get around problems caused by another use of lower-casing to sanitize input: let constantName = opName.substring(1).toLowerCase(); // strip '>' let constantValue = arg_str; + if (constantValue.toLowerCase() === POINTER_TO_CURRENT_ADDR_PSEUDO_OPERAND) { + constantValue = IP.toString(); + } constants[constantName] = constantValue; dbg(2, `constants:`); dbg(2, constants); @@ -125,7 +130,7 @@ function decodeInstructions(line) { dbg(2, `bytesToReplace: ${labels[label].bytesToReplace}`); // Handle references to the Instruction Pointer - } else if (arg_str.toLowerCase() === '*addr') { + } else if (arg_str.toLowerCase() === POINTER_TO_CURRENT_ADDR_PSEUDO_OPERAND) { dbg(2, `operand references current address`); arg_num = IP; dbg(2, `arg_num: ${num2hex(arg_num)}`); @@ -135,9 +140,8 @@ function decodeInstructions(line) { // FIXME - a quick hack to get around problems caused by another use of lower-casing to sanitize input: arg_str = arg_str.substring(1).toLowerCase(); // strip '>' dbg(2, `operand references '${arg_str}'`); - // TODO: support *ADDR arg_str = constants[arg_str]; - dbg(2, `arg_str from '${arg_str}`); + dbg(2, `arg_str from '${arg_str}'`); // Handle references to constants in indirect mode } else if (arg_str.startsWith("(=")) { @@ -147,7 +151,6 @@ function decodeInstructions(line) { // FIXME - a quick hack to get around problems caused by another use of lower-casing to sanitize input: arg_str = arg_str.toLowerCase(); dbg(2, `INDY - operand references '${arg_str}'`); - // TODO: support *ADDR arg_str = constants[arg_str]; // Handle indirect expressions diff --git a/test-programs/referencing-addr-during-assembly.asm b/test-programs/referencing-addr-during-assembly.asm index ed7031e..1cf8a5e 100644 --- a/test-programs/referencing-addr-during-assembly.asm +++ b/test-programs/referencing-addr-during-assembly.asm @@ -1,9 +1,13 @@ ;; Test referencing address of line being assembled +NOP ; Push the const below to a later address +=initAddr *ADDR + LDA *ADDR STO $25 FHP 0 ; hop if carry set JMP @setCarry +LDA =initAddr END @setCarry