Warning of LDA const instead of LDA #const
Posted: Sat Jan 19, 2008 1:52 am
65xx assemblers differentiate between immediate and address operands by the # character, rather than the instruction's mnemonic:
Since # is not required in other common computer languages or general writing, it is easy to forget. The value at the unintended address might only rarely cause problems, making it hard to track down. My solution is to have the assembler track whether an expression is an address or numeric value and give a warning when a number is used where an address is expected:
Sometimes a numeric constant must be used as an address, for example a hardware I/O register. To solve this, I added a special address constant named "ADDR":
To allow source compatibility with other assemblers, you must define ADDR as shown. My ca65 extension knows to treat the assignment specially, while other assemblers will just treat it as a normal constant of zero. Suggestions on a better name are welcome, since this is likely to clash.
To reduce unnecessary warnings, I made a few other tweaks:
For normal assembler coding, where it is given the task of putting things in memory and assigning addresses, about the only changes needed to avoid warnings with this extension are where hardware addresses are defined, like I/O locations.
Here are the modified sources, a readme, and test file: cc65-2.11.0-addr-type.tgz
I just coded it today, and wasn't very familiar with ca65 before this, so expect problems. Right now I mainly want to experiment with the implementation to find the best design. Please add your ideas about this feature and how to improve it.
Code: Select all
lda #10 ; A = the number 10
lda 10 ; A = byte at address 10Code: Select all
const = 10
lda #const ; OK
lda 10 ; warning
lda const ; warningCode: Select all
ADDR = 0 ; treated specially by patched ca65, normally by others
ioreg = ADDR+$FE ; ioreg is an address, not just a number
lda ioreg ; OK
lda 10+ADDR ; OKTo reduce unnecessary warnings, I made a few other tweaks:
Code: Select all
const = $12
zpaddr = ADDR+$12
addr = ADDR+$123
label:
lda <12 ; OK, often used for quick nameless temporaries
lda $12,x ; OK, indexed modes always accept numeric expressions
lda const,y ; OK
sta const ; OK, since sta never accepts immediate anyway
sta $1234 ; OK
lda <zpaddr ; OK, since you might want to emphasize it's zero page
lda <addr ; warning, since addr is more than 8 bits
lda addr*2 ; warning, not an addr
lda #<label ; OK, result of < operator is numericHere are the modified sources, a readme, and test file: cc65-2.11.0-addr-type.tgz
I just coded it today, and wasn't very familiar with ca65 before this, so expect problems. Right now I mainly want to experiment with the implementation to find the best design. Please add your ideas about this feature and how to improve it.