Noob question: Adressing problem / how to?

JMP $FCE2
marcelschoen
Member
Member
Posts: 3
Joined: Tue Jul 12, 2016 10:14 pm

Noob question: Adressing problem / how to?

Post by marcelschoen » Tue Jul 12, 2016 10:27 pm

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.



TMR
Member
Member
Posts: 29
Joined: Fri May 23, 2014 2:19 pm
Location: Leeds, U.K.
Contact:

Re: Noob question: Adressing problem / how to?

Post by TMR » Wed Jul 13, 2016 8:51 am

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.
Disclaimer: a message board post from this person shouldn't be seen as any kind of indication that a project has been started or is ongoing because as a programmer he has the attention span of... oh look, a squirrel!

marcelschoen
Member
Member
Posts: 3
Joined: Tue Jul 12, 2016 10:14 pm

Re: Noob question: Adressing problem / how to?

Post by marcelschoen » Wed Jul 13, 2016 9:47 am

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

tiny tim
Member
Member
Posts: 4
Joined: Tue Jun 24, 2014 8:52 pm
Location: PAL

Re: Noob question: Adressing problem / how to?

Post by tiny tim » Wed Jul 13, 2016 1:13 pm

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.

marcelschoen
Member
Member
Posts: 3
Joined: Tue Jul 12, 2016 10:14 pm

Re: Noob question: Adressing problem / how to?

Post by marcelschoen » Wed Jul 13, 2016 3:28 pm

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!

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 1 guest