From 3fd862a55dfe6a72ffe2d29d32356edec2e9436b Mon Sep 17 00:00:00 2001 From: n loewen Date: Wed, 16 Aug 2023 09:35:37 +0100 Subject: [PATCH] ! Refactor (assembler): Chance constant prefix from '=' to '#' '=' could be confusing when referencing a constant, since it looks like assignment '#' is hopefully clearer (by analog to #hashtags) Breaking change! --- assembler.js | 10 +++++----- readme.md | 4 ++-- test-programs/constants.asm | 10 +++++----- test-programs/referencing-addr-during-assembly.asm | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/assembler.js b/assembler.js index 4ac7035..f20f4a9 100644 --- a/assembler.js +++ b/assembler.js @@ -14,6 +14,7 @@ exports.assemble = (str, debug = false) => { } const POINTER_TO_CURRENT_ADDR_PSEUDO_OPERAND = '*addr'; +const CONSTANT_PREFIX = '#'; const mnemonicsWithOptionalArgs = ['end', 'nop']; const mnemonics2opcodes = { @@ -86,8 +87,7 @@ function decodeInstructions(line) { let addressingMode = 'direct'; // Must be "direct" or "indirect" // Handle constant definitions - if (opName.startsWith('=')) { - // TODO: validate constant + if (opName.startsWith(CONSTANT_PREFIX)) { // 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; @@ -136,7 +136,7 @@ function decodeInstructions(line) { dbg(2, `arg_num: ${num2hex(arg_num)}`); // Handle references to constants - } else if (arg_str.startsWith('=')) { + } else if (arg_str.startsWith(CONSTANT_PREFIX)) { // 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}'`); @@ -144,9 +144,9 @@ function decodeInstructions(line) { dbg(2, `arg_str from '${arg_str}'`); // Handle references to constants in indirect mode - } else if (arg_str.startsWith("(=")) { + } else if (arg_str.startsWith(`(${CONSTANT_PREFIX}`)) { addressingMode = "indirect"; - arg_str = arg_str.replace("(=", ""); + arg_str = arg_str.replace(`(${CONSTANT_PREFIX}`, ""); arg_str = arg_str.replace(")", ""); // FIXME - a quick hack to get around problems caused by another use of lower-casing to sanitize input: arg_str = arg_str.toLowerCase(); diff --git a/readme.md b/readme.md index 81f49b2..e85b2f1 100644 --- a/readme.md +++ b/readme.md @@ -88,10 +88,10 @@ With verbose debugging output: ; the label will be replaced with ; the address of the label - =foo $FF ; define a constant + #foo $FF ; define a constant ; (must be defined before it is referenced) - ADD =foo ; use a constant as an operand + ADD #foo ; use a constant as an operand LDA *ADDR ; `*ADDR` is a magic value referencing the memory address ; that the current line will store at after assembly diff --git a/test-programs/constants.asm b/test-programs/constants.asm index c6c501e..3fb4978 100644 --- a/test-programs/constants.asm +++ b/test-programs/constants.asm @@ -1,6 +1,6 @@ ;; Test constants -=foo $01 -=bar $02 -ADD =foo -STO =bar -STO (=bar) \ No newline at end of file +#foo $01 +#bar $02 +ADD #foo +STO #bar +STO (#bar) \ No newline at end of file diff --git a/test-programs/referencing-addr-during-assembly.asm b/test-programs/referencing-addr-during-assembly.asm index 1cf8a5e..77d5f37 100644 --- a/test-programs/referencing-addr-during-assembly.asm +++ b/test-programs/referencing-addr-during-assembly.asm @@ -1,13 +1,13 @@ ;; Test referencing address of line being assembled NOP ; Push the const below to a later address -=initAddr *ADDR +#initAddr *ADDR LDA *ADDR STO $25 FHP 0 ; hop if carry set JMP @setCarry -LDA =initAddr +LDA #initAddr END @setCarry