Commodore 64 (C64) Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
LDA ($ADDR), Y vs LDA ($ADDR, X)

 
Post new topic   Reply to topic    Commodore 64 (C64) Forum Index -> Scene
View previous topic :: View next topic  
Author Message
cbmeeks
Master of C64
Master of C64


Joined: 22 Oct 2004
Age: 39
Posts: 1070
Location: Hixson, TN USA

PostPosted: Thu Nov 24, 2011 1:53 am    Post subject: LDA ($ADDR), Y vs LDA ($ADDR, X) Reply with quote

As I get more into my project, I am running across some issues where I am not understanding some of the idiosyncrasies of the 6502.

So, I think I understand how

Code:

LDA ($ADDR), y


works. But when would you use

Code:

LDA ($ADDR, X)


Every time I google it, I get specs on the commands and not real examples of when to use it. Using it in a game situation might be helpful too.

Anyone want to give me a 1 minute explanation? Very Happy

Thanks!
Back to top
View user's profile Send private message Visit poster's website
Fröhn
Grandmaster of C64
Grandmaster of C64


Joined: 13 Jan 2003
Posts: 1950

PostPosted: Thu Nov 24, 2011 2:26 am    Post subject: Reply with quote

There hardly is any use of the 2nd one, that's why you hardly see any examples.
Back to top
View user's profile Send private message
eslapion
Grandmaster of C64
Grandmaster of C64


Joined: 24 Jul 2006
Posts: 2717
Location: Mourrial esti de tabarnak

PostPosted: Thu Nov 24, 2011 5:28 am    Post subject: Reply with quote

AFAIK, the first one takes the value at address $ADDR, then adds Y to that value and loads it into the accumulator.

The second one takes the value at address $ADDR plus X, does not add anything to it then loads it into the accumulator.

Ergo, in the first case, Y is added to the obtained value, in the second case, X is added to the target address.

Also, I believe there is a bug with the second one should the sum of $ADDR plus X result in going to the next 256 bytes page.

Since the bug has apparently been fixed in CMOS versions of the 6502 (not that it matters too much since the 64 uses the 6510 which is NMOS only) then you can get completely different results in a 1541 drive whose CPU has been replaced during repairs.

Its also a complete bugger with VIC-20s that have a 65C02 or with CMD drives.
_________________
I have a crystal ball... it is rectangular in shape, made of beige plastic with a tag on it that says Tektronix TDS 1002.
Back to top
View user's profile Send private message MSN Messenger
groepaz
Immortal Grandmaster of C64
Immortal Grandmaster of C64


Joined: 13 Oct 2004
Posts: 4693

PostPosted: Thu Nov 24, 2011 7:42 am    Post subject: Reply with quote

oh dear...
_________________
Back to top
View user's profile Send private message
Lee73
Newbie


Joined: 15 Mar 2010
Age: 39
Posts: 42
Location: Canada

PostPosted: Thu Nov 24, 2011 8:38 am    Post subject: Reply with quote

Both these commands are 2-byte dealies meant for use with zero page locations. To use a precise example:

LDA ($FB),Y

If location $FB is 0, $FC is 4, and Y is 40, then you'll be loading the first character on the 2nd row of the screen. ($FB) points to a specific address (in this case the start of the screen) and from there, you can use Y to reach anywhere within a page's distance from that pointer. VERY useful. Simply modify $FB and/or $FC when the need arises.

LDA ($FB,X)

If location $FB is 40, $FC is 4, and X is 0, then you'll ALSO be loading the first character on the 2nd row of the screen. ($FB) still points to a specific address (in this case the start of the 2ND screen row) but that can ONLY be accessed when you're using an X index of 0.

If X=2 above, then the value fetched would be wherever ($FD) pointed to. The X is "indexed" BEFORE the pointer is looked up. This relatively useless command exists so a table of pointers can exist in zero page, with the X register used to specify which pointer we want.

This command is best left forgotten, but does have one good use: If the Y register is in use, yet you need to directly check something like ($ZP),Y assuming Y=0 (but isn't) you can use ($ZP,X) when X=0 instead.
Back to top
View user's profile Send private message
Mogwai
Über Groupie
Über Groupie


Joined: 20 Oct 2011
Age: 48
Posts: 342
Location: Netherlands

PostPosted: Thu Nov 24, 2011 8:42 am    Post subject: Re: LDA ($ADDR), Y vs LDA ($ADDR, X) Reply with quote

cbmeeks wrote:

Code:

LDA ($ADDR, X)


Every time I google it, I get specs on the commands and not real examples of when to use it. Using it in a game situation might be helpful too.

Anyone want to give me a 1 minute explanation? Very Happy

Thanks!


ZeroPage Indexed Indirect is supported by quite a few instructions, but has only a few real-life applications. It allows you to setup a series of zero-page pointers to memory addresses and manipulate them using X as the index to that table.
For example you could, when switching/setting memory for the screen location, create a table of 8 addresses for 8 sprite pointers. Then you could write a generic routine to manipulate a sprite, by passing it the sprite number and desired sprite-offset.
The routine could then acces the sprite pointer through the indextable in zero-page, without knowing which bank was in use.

EDIT: Removed stuff about Absolute Indexed indirect babble.


Last edited by Mogwai on Thu Nov 24, 2011 9:05 am; edited 2 times in total
Back to top
View user's profile Send private message
groepaz
Immortal Grandmaster of C64
Immortal Grandmaster of C64


Joined: 13 Oct 2004
Posts: 4693

PostPosted: Thu Nov 24, 2011 8:46 am    Post subject: Reply with quote

Quote:
LDA ($ADDR,X) is NOT supported!
Absolute Indexed Indirect addressing only 'knows' JMP($ADDR,X) where ADDR is a 16 bit (2 byte) operand. That one is great for using Jump-tables.

wtf
_________________
Back to top
View user's profile Send private message
Mogwai
Über Groupie
Über Groupie


Joined: 20 Oct 2011
Age: 48
Posts: 342
Location: Netherlands

PostPosted: Thu Nov 24, 2011 8:47 am    Post subject: Reply with quote

groepaz wrote:
oh dear...

Laughing
Back to top
View user's profile Send private message
Mogwai
Über Groupie
Über Groupie


Joined: 20 Oct 2011
Age: 48
Posts: 342
Location: Netherlands

PostPosted: Thu Nov 24, 2011 8:49 am    Post subject: Reply with quote

groepaz wrote:
Quote:
LDA ($ADDR,X) is NOT supported!
Absolute Indexed Indirect addressing only 'knows' JMP($ADDR,X) where ADDR is a 16 bit (2 byte) operand. That one is great for using Jump-tables.

wtf


I put some explanation there about zeropage/none zeropage... Probably while you wrote the reply, but:

*double checking as we speak and will edit ASAP*

I wrote the stuff about JMP($1234,X) from memory and memory can fail.
Though I just found this reference: Absolute Indexed Indirect
I can't find it in another document.
I stand corrected!

(and we should report this error to Wikipedia Exclamation )


Last edited by Mogwai on Thu Nov 24, 2011 9:08 am; edited 1 time in total
Back to top
View user's profile Send private message
groepaz
Immortal Grandmaster of C64
Immortal Grandmaster of C64


Joined: 13 Oct 2004
Posts: 4693

PostPosted: Thu Nov 24, 2011 9:04 am    Post subject: Reply with quote

hint:
Quote:
I am running across some issues where I am not understanding some of the idiosyncrasies of the 6502.


... the 65816 has JMP(abs,x), the 6502 doesnt.
_________________
Back to top
View user's profile Send private message
Mogwai
Über Groupie
Über Groupie


Joined: 20 Oct 2011
Age: 48
Posts: 342
Location: Netherlands

PostPosted: Thu Nov 24, 2011 9:07 am    Post subject: Reply with quote

groepaz wrote:
hint:
Quote:
I am running across some issues where I am not understanding some of the idiosyncrasies of the 6502.


... the 65816 has JMP(abs,x), the 6502 doesnt.


Yes! That's where it must have come from. I'm getting old....
Back to top
View user's profile Send private message
THEWIZ
Forum Junkie
Forum Junkie


Joined: 17 Jun 2006
Posts: 527

PostPosted: Thu Nov 24, 2011 9:08 am    Post subject: Re: LDA ($ADDR), Y vs LDA ($ADDR, X) Reply with quote

Mogwai wrote:

LDA ($ADDR,X) where $ADDR is not zeropage, is NOT supported!
Absolute Indexed Indirect addressing only 'knows' JMP($ADDR,X) where ADDR is a 16 bit (2 byte) operand. That one is great for using Jump-tables.


Actually LDA($ZP,X) IS supported, there is no JMP($ZP,X). There are only TWO JMP addressing modes, absolute and indirect. Absolute is JMP $ADDR and indirect is JMP ($ADDR) (note there is NO X in this addressing mode) BOTH of these are 3 byte commands.

Only JMP and JSR can use the indirect addressing mode. LDA on the other hand uses (Indirect,X) and (Indirect),Y also known as Index Indirect and Indirect Indexed (yeah confusing) Both of these can ONLY use Zero Page addresses.

(Indirect, X) adds the X value FIRST then indirects.
(Indirect), Y indirects FIRST then indexes

(Indirect, X) is useful if you have a table of addresses.
(Indirect), y is useful if you have an address to a table.
_________________
Back to top
View user's profile Send private message
THEWIZ
Forum Junkie
Forum Junkie


Joined: 17 Jun 2006
Posts: 527

PostPosted: Thu Nov 24, 2011 9:10 am    Post subject: Reply with quote

Mogwai wrote:

(and we should report this error to Wikipedia Exclamation )


Now, if only there was SOME way to do that. Or even better if there was some way to just fix it.
Darn... I wish there was a way...
_________________
Back to top
View user's profile Send private message
Mogwai
Über Groupie
Über Groupie


Joined: 20 Oct 2011
Age: 48
Posts: 342
Location: Netherlands

PostPosted: Thu Nov 24, 2011 9:17 am    Post subject: Reply with quote

THEWIZ wrote:
Mogwai wrote:

(and we should report this error to Wikipedia Exclamation )


Now, if only there was SOME way to do that. Or even better if there was some way to just fix it.
Darn... I wish there was a way...

Laughing
Just edited the entry...
Back to top
View user's profile Send private message
eslapion
Grandmaster of C64
Grandmaster of C64


Joined: 24 Jul 2006
Posts: 2717
Location: Mourrial esti de tabarnak

PostPosted: Thu Nov 24, 2011 11:25 pm    Post subject: Reply with quote

groepaz wrote:
oh dear...

This says it all about you...

I know I was wrong but instead of explaining you just make fun of my lack of knowledge.

Now...

AFAIK:
LDA ($ADDR), y
CPU looks at address $ADDR and uses the content as a 2 byte vector, adds Y to that vector, loads the content of the value stored at address (vector + Y) into the accumulator.

LDA ($ADDR, X)
CPU looks at address $ADDR + X and uses the content as a 2 byte vector, loads the content of the value stored at addres (vector) into the accumulator.
_________________
I have a crystal ball... it is rectangular in shape, made of beige plastic with a tag on it that says Tektronix TDS 1002.
Back to top
View user's profile Send private message MSN Messenger
groepaz
Immortal Grandmaster of C64
Immortal Grandmaster of C64


Joined: 13 Oct 2004
Posts: 4693

PostPosted: Thu Nov 24, 2011 11:35 pm    Post subject: Reply with quote

congrats for rephrasing Lee73s post =P
_________________
Back to top
View user's profile Send private message
eslapion
Grandmaster of C64
Grandmaster of C64


Joined: 24 Jul 2006
Posts: 2717
Location: Mourrial esti de tabarnak

PostPosted: Fri Nov 25, 2011 12:06 am    Post subject: Reply with quote

groepaz wrote:
congrats for rephrasing Lee73s post =P

Thank you for confirming I got it right this time.

So kind and courteous of you!

People have such good manners here.
_________________
I have a crystal ball... it is rectangular in shape, made of beige plastic with a tag on it that says Tektronix TDS 1002.
Back to top
View user's profile Send private message MSN Messenger
cbmeeks
Master of C64
Master of C64


Joined: 22 Oct 2004
Age: 39
Posts: 1070
Location: Hixson, TN USA

PostPosted: Sun Nov 27, 2011 3:36 am    Post subject: Reply with quote

Thanks to everyone for posting information. Reading posts here on Lemon for the last few days has helped me reevaluate how I am coding my project. Which means back to the drawing board for some things but at least I am understanding it more.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Commodore 64 (C64) Forum Index -> Scene All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Tip: Get C64 Forever for super-comfy C64 emulation with pre-installed games, demos and other goodies!


Powered by phpBB © 2001, 2005 phpBB Group