Page 1 of 1

Noob question: Adressing problem / how to?

Posted: Tue Jul 12, 2016 10:27 pm
by marcelschoen
Hi guys,

I just began learning 6510 programming, and I'm still having problems using the "zeropage indirect indexed adressing" (or however it's called) correctly. I have a very simple program which just prints a string to the screen. The problem is reading the data from the string. When I use it with absolute indexed addressing like

lda line,x

(where "line" is the address-marker of the text data) it works. When I try the same with the above mentioned variation, I get garbagge. Here are the two code examples, complete (for Kick Assembler):

Works (absolute indexed addressing)
:BasicUpstart2(start)
start:
		sei
		jsr $E544
		jsr print_text
		rts

print_text:
		ldx #$00
loop_text:
		lda line,x	// works
		sta $0590,x
		lda #WHITE
		sta $d990,x  
		inx 
		cpx #17         // process 17 columns
		bne loop_text
		rts
				
line:		.text "this is some text"
Fails (zeropage indirect indexed addressing)
:BasicUpstart2(start)
start:
		sei
		jsr $E544
		jsr print_text
		rts

print_text:
		ldx #$00

		lda #<line		// lo-byte in $FB
		sta $fb
		lda #>line		// hi-byte in $FC
		sta $fc

loop_text:
		lda ($fb),x		// does not work
		sta $0590,x
		lda #WHITE
		sta $d990,x  
		inx 
		cpx #17         // process 17 columns
		bne loop_text
		rts
				
line:		.text "this is some text"
Thanks in advance for any help.

Re: Noob question: Adressing problem / how to?

Posted: Wed Jul 13, 2016 8:51 am
by TMR
lda ($fb),x isn't legal, so swapping all the references to X in the loop over to use Y will work correctly.

Code: Select all

print_text:
		ldy #$00

		lda #<line		// lo-byte in $FB
		sta $fb
		lda #>line		// hi-byte in $FC
		sta $fc

loop_text:
		lda ($fb),y
		sta $0590,y
		lda #WHITE
		sta $d990,y  
		iny 
		cpy #17         // process 17 columns
		bne loop_text
lda ($fb,x) is legal but the X will be added to the high byte of the address rather than the low.

Re: Noob question: Adressing problem / how to?

Posted: Wed Jul 13, 2016 9:47 am
by marcelschoen
Thanks for your reply. About this:
TMR wrote:lda ($fb),x isn't legal
Why not? Are you saying that my version only works with the "y"-register? If I understand you right, then these are legal:

lda ($fb),y
lda ($fb,x)

but these aren't:

lda ($fb),x
lda ($fb,y)

correct? I didn't get that from the documentation I read so far. I always thought "x" and "y" could both be used for both cases. That would of course explain everything...

Re: Noob question: Adressing problem / how to?

Posted: Wed Jul 13, 2016 1:13 pm
by tiny tim
marcelschoen wrote:If I understand you right, then these are legal:

lda ($fb),y
lda ($fb,x)

but these aren't:

lda ($fb),x
lda ($fb,y)

correct?
Correct. The 6510 instruction set is anything but orthogonal, there are many more cases where only one of the index registers is allowed. INC and DEC only allow the X-register for example, as do zeropage operations like ADC/SBC zp,x, AND/ORA zp,x etc. In the latter cases most assemblers won't give you an error though but simply use absolute y-indexed addressing instead.

Re: Noob question: Adressing problem / how to?

Posted: Wed Jul 13, 2016 3:28 pm
by marcelschoen
Ahh... that really clears it up. From now on, when I read some 6502 documentation and there's an example using either register, I'll take that as the way it has to be used, as long as the description does not explicitely state otherwise.
In the latter cases most assemblers won't give you an error though but simply use absolute y-indexed addressing instead.
Really? That sounds dangerous, since the "y" register will probably not be in the right state at all in such a case. But good to know, I'll keep an eye open for that kind of mistake if I encounter strange errors. :-)

That was extremely helpful, thanks!