Feature (assembler): Allow assigning `*ADDR` to a constant

This commit is contained in:
n loewen 2023-08-16 09:29:27 +01:00
parent 6a3c70be37
commit 9fefa0916a
2 changed files with 11 additions and 4 deletions

View File

@ -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

View File

@ -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