Warning of LDA const instead of LDA #const

Programming the 6502 microprocessor and its relatives in assembly and other languages.
kc5tja
Posts: 1706
Joined: 04 Jan 2003

Post by kc5tja »

debounce wrote:
kc5tja wrote:
debounce wrote:
FWIW I prefer the old syntax, and not just for inertia's sake. Addressing modes are an 'intellectual convenience' I'd like to keep, as they are a mnemonic in themselves.
That is their syntactic point -- the idea is to make assembly language a bit more like a high-level language.
Not sure what you're saying here. You mean "the idea [of bracket-and-comma] is..." or "The idea [of all-alphabetic] is..."? If the latter, then reducing mnemonics to a simple label for an opcode byte (and a width specifier for the following operand) pushes the assembly language closer to machine code. As evidenced by the Forth examples quoted earlier.
Therefore, I must be talking about the former.
Quote:
INC abs,Y is an illegal instruction (the only one that's bitten me.) It's just as easy to assume INCAY abs exists.
But this particular problem is not what is being discussed. Look at the subject of the message: LDA 5 versus LDA #5. We're specifically addressing the issue where you have ambiguity.
Quote:
And the mnemonics aren't such great mnemonics any more. Some people work better with brackets and commas.
This is a "what I learned first stays with me" issue. You only think this because you learned the current syntax first.

Compare: Folks who program the 68000 series of CPUs find Intel's native syntax to be retarded. Also, folks who grew up coding on Zilog or Intel machines first find Motorola syntax to be gobsmackingly stupid. You find religious wars over whether Unix assemblers should continue to use such "AT&T-syntax" (which is basically Motorola syntax) to this day.
Quote:
The @ prefixes an otherwise bare operand of an instruction to indicate it is intended as an absolute address. Any addressing mode notation # () [] , A: Z: is enough to show the intention (or non-intention) and @ is forbidden. Where @ is allowed and the assembler does not generate a warning (depending on the sophistication of its heuristics), @ is optional. @ has nothing to do with ADDR notation, but there's no reason why the two couldn't be used in conjunction (though I'd rather there was a shorter and less shouty symbol than "ADDR+".)
Wait, what? I'm thoroughly confused by your proposition. Why bother with @ at all, if you already have A: and Z:?

Code: Select all

        LDY     const   ; give warning
        LDY     @const  ; opcode AC or A4
You mean I have to actually work to decipher these opcodes? Uugh. I've offered a correction to the table, with published interpretations of various prefixes (published by WDC, I might add): :)

Code: Select all

LDY const   ; give warning
LDY @const  ; LDY abs or LDY dp??
LDA <addr   ; ditto here; LDA abs or LDA dp??
LDA @<addr  ; LDA abs ; <N is NOT (N & 0xFF) on 65816.  See LDA # below.
LDA A:addr  ; LDA abs (for ca65 only)
LDA #<addr  ; LDA #nn(nn) -- note that LDA #>01020304 will load $0203 into A!!
STA const   ; AMBIGUOUS; STA A:$000F and STA Z:$0F are *NOT* the same thing on 65816!!
STA @const  ; STA abs
Even this table is pretty doggone confusing to me. What's needed is a distinguishing between types of access, and mathematical operators:

Code: Select all

; new operators
HIBYTE x = (x & 0xFF00) >> 8
LOBYTE x = (x & 0x00FF)
HIWORD x = (x & 0xFFFF0000) >> 16
LOWORD x = (x & 0x0000FFFF)

; for WDC compatibility:
^ x = HIWORD x
> x = LOWORD (x >> 8)
< x = LOWORD x

; typecasts:
@ x = (make x an absolute address)
# x = (make x an immediate value)
& x = (make x a long-absolute address)
* x = (make x a direct-page address)
So, with this, we can be far more precise:

Code: Select all

LDY const   ; give warning
LDY *const   ; LDA dp
LDY @const  ; LDY abs
LDA <addr   ; warning: LDA abs or LDA dp??
LDA @<addr  ; LDA abs
LDA *<addr   ; LDA dp
LDA #<addr  ; LDA #nn(nn) -- note that LDA #>01020304 will load $0203 into A!!
STA const   ; for sake of typing consistency, this would be a warning.
STA *const   ; STA dp
STA @const  ; STA abs
Quote:
@ notation is appropriate when the problem is framed as "# being left off instructions." ADDR notation is an appropriate response to "constants being used as addresses" (in which case STA const should give a warning.) I prefer @ notation as ADDR doesn't cover the case of an address being used as an address when it should have been used as a constant.
I have to giggle at this, because if we assume future assembler syntax takes this route, and to eliminate further ambiguities between (long-)absolute and direct page modes, then you end up with a system of prefixes that fundamentally aren't any different than one opcode, one mnemonic.
Quote:
We'll see, I suppose, which syntax wins out.
Nothing will change of course; WDC's syntax has defined the assembler syntax for decades. :)

So, with that in mind, this will be my final post on the topic. I think my opinion on this matter is already very well known, and we're flogging a dead horse. :)
blargg
Posts: 42
Joined: 30 Dec 2003
Contact:

Post by blargg »

My only aim was to catch the error with minimal changes to source, and preferably in a way that doesn't yield one's source incompatible with the standard ca65. I've succeeded in all these respects and have a simple, practical approach, so doubt I'll do much more work on it. Out of curiosity, has anyone actually tried my implementation? Using it would dispel a lot of the misunderstandings about how much work it requires to use.
kc5tja
Posts: 1706
Joined: 04 Jan 2003

Post by kc5tja »

I have to admit that I regularly use constants as both addresses and immediate values freely in my code, so I likely won't profit in using it until it becomes a problem for me (which is unlikely). However, that's no reason not to make it available to others. Have you considered submitting a patch to the ca65 maintainers?
mdpenny
Posts: 50
Joined: 24 Sep 2002
Location: Essex, UK

Post by mdpenny »

Just as a not - I'm sure that this is how 6502 assmbler was *originally* written, "way back when"; for example, have a look at the code towards the end of this article:

http://users.telenet.be/kim1-6502/micro ... chess.html

I'm not quite sure why the switch was made to the current way of writing code, but I *think* it may have been to generate "neater, tidier" code with "simple" 3-character opcodes...

Just my £0.02's-worth...

--Martin
Martin Penny

.sig in beta - full release to follow.
debounce
Posts: 27
Joined: 23 Nov 2004
Location: London, UK

Post by debounce »

Sorry everyone, I take it all back. Strange as I'm usually more flexible. Perhaps I had believed it was The One True Syntax.

--G
Post Reply