! 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!
This commit is contained in:
parent
9fefa0916a
commit
3fd862a55d
10
assembler.js
10
assembler.js
|
|
@ -14,6 +14,7 @@ exports.assemble = (str, debug = false) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const POINTER_TO_CURRENT_ADDR_PSEUDO_OPERAND = '*addr';
|
const POINTER_TO_CURRENT_ADDR_PSEUDO_OPERAND = '*addr';
|
||||||
|
const CONSTANT_PREFIX = '#';
|
||||||
|
|
||||||
const mnemonicsWithOptionalArgs = ['end', 'nop'];
|
const mnemonicsWithOptionalArgs = ['end', 'nop'];
|
||||||
const mnemonics2opcodes = {
|
const mnemonics2opcodes = {
|
||||||
|
|
@ -86,8 +87,7 @@ function decodeInstructions(line) {
|
||||||
let addressingMode = 'direct'; // Must be "direct" or "indirect"
|
let addressingMode = 'direct'; // Must be "direct" or "indirect"
|
||||||
|
|
||||||
// Handle constant definitions
|
// Handle constant definitions
|
||||||
if (opName.startsWith('=')) {
|
if (opName.startsWith(CONSTANT_PREFIX)) {
|
||||||
// TODO: validate constant
|
|
||||||
// FIXME - a quick hack to get around problems caused by another use of lower-casing to sanitize input:
|
// 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 constantName = opName.substring(1).toLowerCase(); // strip '>'
|
||||||
let constantValue = arg_str;
|
let constantValue = arg_str;
|
||||||
|
|
@ -136,7 +136,7 @@ function decodeInstructions(line) {
|
||||||
dbg(2, `arg_num: ${num2hex(arg_num)}`);
|
dbg(2, `arg_num: ${num2hex(arg_num)}`);
|
||||||
|
|
||||||
// Handle references to constants
|
// 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:
|
// 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 '>'
|
arg_str = arg_str.substring(1).toLowerCase(); // strip '>'
|
||||||
dbg(2, `operand references '${arg_str}'`);
|
dbg(2, `operand references '${arg_str}'`);
|
||||||
|
|
@ -144,9 +144,9 @@ function decodeInstructions(line) {
|
||||||
dbg(2, `arg_str from '${arg_str}'`);
|
dbg(2, `arg_str from '${arg_str}'`);
|
||||||
|
|
||||||
// Handle references to constants in indirect mode
|
// Handle references to constants in indirect mode
|
||||||
} else if (arg_str.startsWith("(=")) {
|
} else if (arg_str.startsWith(`(${CONSTANT_PREFIX}`)) {
|
||||||
addressingMode = "indirect";
|
addressingMode = "indirect";
|
||||||
arg_str = arg_str.replace("(=", "");
|
arg_str = arg_str.replace(`(${CONSTANT_PREFIX}`, "");
|
||||||
arg_str = arg_str.replace(")", "");
|
arg_str = arg_str.replace(")", "");
|
||||||
// FIXME - a quick hack to get around problems caused by another use of lower-casing to sanitize input:
|
// FIXME - a quick hack to get around problems caused by another use of lower-casing to sanitize input:
|
||||||
arg_str = arg_str.toLowerCase();
|
arg_str = arg_str.toLowerCase();
|
||||||
|
|
|
||||||
|
|
@ -88,10 +88,10 @@ With verbose debugging output:
|
||||||
; the label will be replaced with
|
; the label will be replaced with
|
||||||
; the address of the label
|
; the address of the label
|
||||||
|
|
||||||
=foo $FF ; define a constant
|
#foo $FF ; define a constant
|
||||||
; (must be defined before it is referenced)
|
; (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
|
LDA *ADDR ; `*ADDR` is a magic value referencing the memory address
|
||||||
; that the current line will store at after assembly
|
; that the current line will store at after assembly
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
;; Test constants
|
;; Test constants
|
||||||
=foo $01
|
#foo $01
|
||||||
=bar $02
|
#bar $02
|
||||||
ADD =foo
|
ADD #foo
|
||||||
STO =bar
|
STO #bar
|
||||||
STO (=bar)
|
STO (#bar)
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
;; Test referencing address of line being assembled
|
;; Test referencing address of line being assembled
|
||||||
|
|
||||||
NOP ; Push the const below to a later address
|
NOP ; Push the const below to a later address
|
||||||
=initAddr *ADDR
|
#initAddr *ADDR
|
||||||
|
|
||||||
LDA *ADDR
|
LDA *ADDR
|
||||||
STO $25
|
STO $25
|
||||||
FHP 0 ; hop if carry set
|
FHP 0 ; hop if carry set
|
||||||
JMP @setCarry
|
JMP @setCarry
|
||||||
LDA =initAddr
|
LDA #initAddr
|
||||||
END
|
END
|
||||||
|
|
||||||
@setCarry
|
@setCarry
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue