This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

indexing a constant (immediate)

indexing a constant (immediate)
by on (#43514)
silly question but...

why can't it be done in 6502
eg.

Code:
lda #$00,x


or can it?

by on (#43515)
Because the immediate isn't an address?

by on (#43516)
well i guess as they say - ask a silly question...

by on (#43517)
Well what would be the difference between lda #$00, x and lda $00, x? None that I can see.

by on (#43518)
kyuusaku wrote:
Well what would be the difference between lda #$00, x and lda $00, x? None that I can see.

"lda #$00, x" simply makes no sense... What does one intent to do with this command? Do you mean it should be treated the same as "lda $00, x"? Allowing the use of "#" when representing addresses could make things really confusing. Addresses don't have a "#", that's a simple rule.

by on (#43521)
ok i didn't state my question correctly.

and i suppose the point's moot because what i'm trying to do can be done easilywith a bit of thought

Code:
lda value,x

=

txa
clc
adc value

by on (#43522)
The argument to an opcode is normally a constant memory address. Even if you use some named identifier for a memory address in your assembly code the assembler will translate it to a constant address.

However, some opcodes take what is called an immediate address, which is not really an address at all but a value. That is usually denoted with a # infront of it.

So, indexing can be done with a constant address, using lda $00,x or lda MyAddress,x where MyAddress is some memory location known to the assembler.

lda #$00,x makes no sense at all, since #$00 implies the value $00 and not the address $00.

EDIT:
You posted while I was typing :). That you wanted it to add was the other possibility I though of other then having confused constant value with constant address. Bah.

by on (#43523)
i'm SORRY

i'll think next time before asking a question that may be met with pedantic remarks about assembly fundamentals

it's just that with all the addressing modes of the 6502, it's hard to remember exactly what's allowed. one might conclude that since
when x=3,
Code:
 LDA $0303,x
means LDA $0306, maybe
Code:
lda #$80,x
when x=7F, could mean lda #$FF?
Re: indexing a constant (immediate)
by on (#43525)
It can almost be done, if you're willing to devote 511 bytes to a table:
Code:
ldx #$04
lda table+$10,x ; loads a with $14
ldx #$FF
lda table+$FF,x ; loads a with $FE

table: .byte $00,$01,$02...$FE,$FF,$00,$01,$02...$FE ; 511 entries total

If you put the table in zero-page, it would have the syntax LDA $n,x to add n and X and put it into A, it'd only need 256 bytes, and the operation would only take two bytes for the opcode and 4 cycles, but then you wouldn't have zero-page available. The equivalent (CLC, TXA, ADC #n) only takes 6 cycles, and carry is often in a definite state, knocking it down to 3 bytes and 4 cycles.

by on (#43526)
tokumaru wrote:
"lda #$00, x" simply makes no sense... What does one intent to do with this command?


Actually, if you loaded A with something other than 0, it would basically serve as:

clc
lda #immediate
adc X

So lda #immediate,x would be really handy.

And I really don't think it's at all odd to figure that lda #immediate,x is a real instruction. But I suppose x is an index and it makes sense seeing it that way.

by on (#43537)
Celius wrote:
tokumaru wrote:
"lda #$00, x" simply makes no sense... What does one intent to do with this command?


Actually, if you loaded A with something other than 0, it would basically serve as:

clc
lda #immediate
adc X

So lda #immediate,x would be really handy.


If I were to speculate I would think it means something more like lda (#imm,PC,x) similar to the 68k. PC being the current PC of the instruction so no need to have it in the syntax. So you'd have pc+#imm, indexed by X. X would wrap, #imm would carry on the PC MSB.

by on (#43538)
i guess i also forgot that x could be an operand of ADC.


Celius knows what it's like to be zonked and still attempting to mentally process code ;)

by on (#43543)
Roni wrote:
i guess i also forgot that x could be an operand of ADC.

You didn't forget, because it can't. =) Celius was just explaining what you wanted to do. Blargg's code is the one that is valid and does what you want (CLC, TXA, ADC #n).

Some time ago we discussed the use of a simple look-up table that would extend the functionality of registers X and Y.

by on (#43544)
tomaitheous wrote:
If I were to speculate I would think it means something more like lda (#imm,PC,x) similar to the 68k.

That's what I thought too at first, actually that could be done as LDA *,x but it would be 4 cycles (instead of presumably 3, if that instruction existed).

by on (#43552)
So ultimately, the 6502 has more addressing modes than just immediate (LDA #imm) and zero-page indirect (LDA (0,x)) because common operations would require too many instructions. You often want to access a fixed 16-bit address, so there's LDA addr, and you often want to do a table lookup, so there's LDA addr,X and LDA addr,Y. It's not that often that you want to load A with X or Y plus some constant. Also, doing so manually isn't nearly as involved as doing indexed addressing manually would be:
Code:
; rough equivalent to LDA addr,X
txa
clc
adc #<addr
sta temp
lda #>addr
adc #0
sta temp+1
ldx #0
lda (0,x)

by on (#43567)
Sorry fot the Off-Topic, but I've seen this operand many times, but never seen what is it for in any tutorial or doc: #> and #<

like in this lines:
Code:
adc #<addr
lda #>addr


I think I also saw it with indirect addressing, something like <(some_addr) but I'm not sure.

What's it for?

by on (#43571)
Petruza wrote:
Sorry fot the Off-Topic, but I've seen this operand many times, but never seen what is it for in any tutorial or doc: #> and #<

like in this lines:
Code:
adc #<addr
lda #>addr


I think I also saw it with indirect addressing, something like <(some_addr) but I'm not sure.

What's it for?


addr in that example would be a 16-bit value, using #> would access the upper 8-bits and #< accesses the lower 8 bits.

Using it for indirect mode, a quick example would be:
Code:
file: .incbin "something.dat"
 lda #<file
 sta addr_lo
 lda #>file
 sta addr_hi
 ldy #0
loop:
 lda (addr_lo),y
 ; sta, then loop and such

by on (#43574)
Great, thanks!