Need help to Add numbers to X and Y registures
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Need help to Add numbers to X and Y registures
I am hopeless with 6502... I come from the Apple II world but for the most part I think 6502 is universal, at least to my understanding. I have bought and read several books, but nothing sinks in.... but i'm crackerjack at Basic.. (whiptydo?!?)
Here is an example...the numbers are expressed in DEC although I know they need to be in hex when compiling...
LDA 950
CLC
ADC 44
STA 44
This adds the location of 950 to location 44... and the result is stored at 44
Exp 950: 10 44:12 after code 44:22
The next thing I need to be able to do, is add a memory location value to Xreg, say from the same address 951.
Xreg is 10.... 951: 20 I need the Xreg to = 30 (or)
Xreg is 20.... 951: 25 I need the Xreg to = 45 (something like that)
The last thing I need to be able to do, is add a memory location value to Yreg, say from the same address 952.
Yreg is 11.... 952: 26 I need the Yreg to = 36 (or)
Yreg is 53... 952: 35 I need the Yreg to = 88
I hope this makes sense... Thanks for any replies.... Tom
Here is an example...the numbers are expressed in DEC although I know they need to be in hex when compiling...
LDA 950
CLC
ADC 44
STA 44
This adds the location of 950 to location 44... and the result is stored at 44
Exp 950: 10 44:12 after code 44:22
The next thing I need to be able to do, is add a memory location value to Xreg, say from the same address 951.
Xreg is 10.... 951: 20 I need the Xreg to = 30 (or)
Xreg is 20.... 951: 25 I need the Xreg to = 45 (something like that)
The last thing I need to be able to do, is add a memory location value to Yreg, say from the same address 952.
Yreg is 11.... 952: 26 I need the Yreg to = 36 (or)
Yreg is 53... 952: 35 I need the Yreg to = 88
I hope this makes sense... Thanks for any replies.... Tom
Re: Need help to Add numbers to X and Y registures
Welcome!
May I suggest you use the Code text style? The mono-spaced font makes a tidier presentation. Like this:
Alright. Y' reckon the TAX and TXA instructions might be helpful for that? 
Code: Select all
LDA 950
CLC
ADC 44
STA 44Quote:
The next thing I need to be able to do, is add a memory location value to Xreg
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
https://laughtonelectronics.com/Arcana/ ... mmary.html
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: Need help to Add numbers to X and Y registures
TAX, TXA, TAY, and TYA are your friends here, because the A register is the most efficient place to do arithmetic on the 6502. If you don't want to trash what's in A, you can start with a PHA and end with a PLA, like so:
Happy programming!
Mike B.
Code: Select all
pha
txa
clc
adc 951
tax
tya
clc
adc 952
tay
pla
Mike B.
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Re: Need help to Add numbers to X and Y registures
Alright, I'm flipping thru my 6502 machine book to find out what these codes mean... writing lots of notes...
Thank you for the quick replies! Anybody use Apple II emulators? I have a game you can try out if you do!
Tom
Thank you for the quick replies! Anybody use Apple II emulators? I have a game you can try out if you do!
Tom
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Re: Need help to Add numbers to X and Y registures
Ok... so let me get this straight.... if a number is in the accumulator, and then I add
another number to it, its an automatic addition (or subtraction), so ADC is not just
put a number there, its a Add a number there....
another number to it, its an automatic addition (or subtraction), so ADC is not just
put a number there, its a Add a number there....
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: Need help to Add numbers to X and Y registures
LASERACTIVEGUY wrote:
Ok... so let me get this straight.... if a number is in the accumulator, and then I add
another number to it, its an automatic addition (or subtraction), so ADC is not just
put a number there, its a Add a number there....
another number to it, its an automatic addition (or subtraction), so ADC is not just
put a number there, its a Add a number there....
Speaking of Apple ][s:
I have a couple of examples of the real hardware in my attic, and they still work ... or at least they did the last time I checked, a few years ago. For hacking around, I prefer the convenience of AppleWin. It seems to mostly satisfy my 8-bit needs, and it looks pretty convincing in full-screen mode. It doesn't smell the same, though.
Mike B.
P.S. The PDP-8 didn't have a unique instruction to copy an arbitrary value into the accumulator, but it did have TAD (twos-complement add to accumulator). There was an instruction to clear the accumulator first (CLA), but it wasn't always necessary, because a preceding DCA (deposit and clear accumulator) automatically cleared it for you! Very baroque!
-
LASERACTIVEGUY
- Posts: 18
- Joined: 29 Dec 2015
Re: Need help to Add numbers to X and Y registures
If you have nothing to do, This is the Music Master RPG...
https://www.dropbox.com/s/tuecdmul18csm ... 9.zip?dl=0
My very first 'game'. There is a sequel actually 1/2 done that has 3 times the people, 3 times the music and a huge world/map/story. The first one is 'not an official finish' but good for 35-45 minutes of gameplay. Apple II disk images!
Ps. I got the PLOT code working!!! Now I have both the VLIN and HLIN codes to go, and then when its done I will be able to have BRUN-able sprites that have instant loading time. I even have ideas how to make an easy collision detection possible with two states (background or enemy attack)...creating different outcomes for the sprites. You made my day... but a tired one at that! I'll be up all night!
https://www.dropbox.com/s/tuecdmul18csm ... 9.zip?dl=0
My very first 'game'. There is a sequel actually 1/2 done that has 3 times the people, 3 times the music and a huge world/map/story. The first one is 'not an official finish' but good for 35-45 minutes of gameplay. Apple II disk images!
Ps. I got the PLOT code working!!! Now I have both the VLIN and HLIN codes to go, and then when its done I will be able to have BRUN-able sprites that have instant loading time. I even have ideas how to make an easy collision detection possible with two states (background or enemy attack)...creating different outcomes for the sprites. You made my day... but a tired one at that! I'll be up all night!
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Need help to Add numbers to X and Y registures
Welcome.
An assembler can take in decimal values too (or binary, and usually octal as well, although I've never seen any practical use in octal in this equipment). BASICs usually take decimal only; but hex and binary are much more practical for some situations. The assembler will convert when appropriate. For example, if you write LDA #10, the assembler will lay down A9 0A; but if you write LDA #$10 or LDA #%00010000, it will lay down A9 10. You could also have a named constant (or EQUate) with that value. For an example, we'll use the name LCD_WIDTH to tell how many characters are on a line in an intelligent character LCD, and assign it a value of 16 in the source code, with the line LCD_WIDTH EQU 16. Then LDX #LCD_WIDTH will lay down A2 10. Now if you ever go to a wider LCD, for example one that's 24 characters across, you don't have to change the number in every single display routine that loads the initial width—only change the line where LCD_WIDTH is defined. Another use would be to count up and do a CPX #LCD_WIDTH to determine whether to go for another loop iteration or not. (These aren't really the best examples, but they should make the point.)
The normal use for adding X or Y is for indexing, ie, adding their content to an address. If X contains 7 and the instruction LDA 950,X is encountered, the processor will load the accumulator with what's in address 957. X's contents and the 950 got added together, but X does not change. It is common to have a loop where the index register gets incremented or decremented once or twice per loop iteration, using INX or DEX or INY or DEY. Each of these is a single byte, and takes two cycles to complete. If you find yourself wanting arithmetic or logical operations on X or Y, you have probably approached the problem the wrong way. BigEd here observed, "With 6502, I suspect more than one beginner has wondered why they can't do arithmetic or logic operations on X or Y, or struggled to remember which addressing modes use which of the two. And then the intermediate 6502 programmer will be loading and saving X and Y while the expert always seems to have the right values already in place."
LASERACTIVEGUY wrote:
Here is an example...the numbers are expressed in DEC although I know they need to be in hex when compiling...
The normal use for adding X or Y is for indexing, ie, adding their content to an address. If X contains 7 and the instruction LDA 950,X is encountered, the processor will load the accumulator with what's in address 957. X's contents and the 950 got added together, but X does not change. It is common to have a loop where the index register gets incremented or decremented once or twice per loop iteration, using INX or DEX or INY or DEY. Each of these is a single byte, and takes two cycles to complete. If you find yourself wanting arithmetic or logical operations on X or Y, you have probably approached the problem the wrong way. BigEd here observed, "With 6502, I suspect more than one beginner has wondered why they can't do arithmetic or logic operations on X or Y, or struggled to remember which addressing modes use which of the two. And then the intermediate 6502 programmer will be loading and saving X and Y while the expert always seems to have the right values already in place."
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
- BigDumbDinosaur
- Posts: 9425
- Joined: 28 May 2009
- Location: Midwestern USA (JB Pritzker’s dystopia)
- Contact:
Re: Need help to Add numbers to X and Y registures
LASERACTIVEGUY wrote:
I am hopeless with 6502... I come from the Apple II world but for the most part I think 6502 is universal, at least to my understanding.
Quote:
Here is an example...the numbers are expressed in DEC although I know they need to be in hex when compiling...
One of the first steps in becoming a good programmer is understanding and using the correct terminology. Doing so, especially in print, will assist in getting across your thoughts when discussing programming topics in an on-line forum.
- Assembly language is called assembly language, not assembly, assembler or machine language.
- Your program as written in a text editor is the source code.
- You assemble your source code, not compile it.
- Assembly is the process of translating the human-readable instructions in your source code into the binary equivalents that are specific to the particular microprocessor being programmed.
- The program that assembles your source code is referred to as an assembler, not a compiler (they are two very different animals).
- The output from the assembler is (usually) written into an object file, which contains object code. Object code may be raw binary that can be executed if loaded into memory, or may be some sort of textual representation of the raw binary. If the object code is the latter, it must be processed by a loader to produce an executable binary.
GARTHWILSON wrote:
...although I've never seen any practical use in octal in this equipment.
As you noted, octal is very seldom used in 65xx programs. If writing in assembly language it's just as easy to express masks and such in binary.
x86? We ain't got no x86. We don't NEED no stinking x86!
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: Need help to Add numbers to X and Y registures
BigDumbDinosaur wrote:
...
- You assemble your source code, not compile it.
... - The program that assembles your source code is referred to as an assembler, not a compiler (they are two very different animals).
Are they really that different, BDD?
Mike B.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Need help to Add numbers to X and Y registures
Your linked page, Mike, says, "All assemblers are (simple) compilers, since they transform one language to another. Not all compilers are assemblers." I might be able to accept that, with difficulty. Assembly language and machine language could be considered to be the the same language in two different writing systems, one being the machine-readable version and the other being the human-readable version, with basically a 1:1 relationship.
The Wikipedia article for "compiler" says, "The name 'compiler' is primarily used for programs that translate source code from a high-level programming language to a lower-level language." There's not a 1:1 relationship there, as different compilers starting with the same source code could produce vastly different machine-language outputs.
Although there may not always be as clear of a line between the two terms as we would like, I think it will be helpful for the beginning programmer to understand these differences.
My (mis?)understanding is that octal was common on early computers that had 12-bit words, so they could have four digits of three bits each, and that four bits per digit did not seem to make sense before the idea of going to letters A-F to get beyond 9 caught on.
In any case, I, too, would strongly recommend the programming manual BDD references above. It's free, and very thorough. The tutorial should get the beginner up to speed on the basics and how the various features of the instruction set are used. A large section near the end devotes an entire page (and sometimes more) to explaining every instruction. Another section devotes an entire page to diagramming every address mode.
The Wikipedia article for "compiler" says, "The name 'compiler' is primarily used for programs that translate source code from a high-level programming language to a lower-level language." There's not a 1:1 relationship there, as different compilers starting with the same source code could produce vastly different machine-language outputs.
Although there may not always be as clear of a line between the two terms as we would like, I think it will be helpful for the beginning programmer to understand these differences.
My (mis?)understanding is that octal was common on early computers that had 12-bit words, so they could have four digits of three bits each, and that four bits per digit did not seem to make sense before the idea of going to letters A-F to get beyond 9 caught on.
In any case, I, too, would strongly recommend the programming manual BDD references above. It's free, and very thorough. The tutorial should get the beginner up to speed on the basics and how the various features of the instruction set are used. A large section near the end devotes an entire page (and sometimes more) to explaining every instruction. Another section devotes an entire page to diagramming every address mode.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: Need help to Add numbers to X and Y registures
GARTHWILSON wrote:
... Assembly language and machine language could be considered to be the the same language in two different writing systems, one being the machine-readable version and the other being the human-readable version, with basically a 1:1 relationship.
Quote:
6. A86 takes advantage of situations in which more than one set of opcodes can be generated for the same instruction. (For example, MOV AX,BX can be generated using either an 89 or 8B opcode, by reversing fields in the following effective address byte. Both forms are absolutely identical in functionality and execution speed.) A86 adopts an unusual mix of choices in such situations. This creates a code-generation "footprint" that occupies no space in your program file, but will enable me to tell, and to demonstrate in a court of law, if a non-trivial object file has been produced by A86. The specification for this "footprint" is sufficiently obscure and complicated that it would be impossible to duplicate by accident. I claim exclusive rights to the particular "footprint" I have chosen, and prohibit anyone from duplicating it.
GARTHWILSON wrote:
The Wikipedia article for "compiler" says, "The name 'compiler' is primarily used for programs that translate source code from a high-level programming language to a lower-level language." There's not a 1:1 relationship there, as different compilers starting with the same source code could produce vastly different machine-language outputs.
Although there may not always be as clear of a line between the two terms as we would like, I think it will be helpful for the beginning programmer to understand these differences.
My (mis?)understanding is that octal was common on early computers that had 12-bit words, so they could have four digits of three bits each, and that four bits per digit did not seem to make sense before the idea of going to letters A-F to get beyond 9 caught on.
Although there may not always be as clear of a line between the two terms as we would like, I think it will be helpful for the beginning programmer to understand these differences.
My (mis?)understanding is that octal was common on early computers that had 12-bit words, so they could have four digits of three bits each, and that four bits per digit did not seem to make sense before the idea of going to letters A-F to get beyond 9 caught on.
GARTHWILSON wrote:
In any case, I, too, would strongly recommend the programming manual BDD references above. It's free, and very thorough. The tutorial should get the beginner up to speed on the basics and how the various features of the instruction set are used. A large section near the end devotes an entire page (and sometimes more) to explaining every instruction. Another section devotes an entire page to diagramming every address mode.
Mike B.
Re: Need help to Add numbers to X and Y registures
(We'll always get a big detour when we indulge in pedantry... resist the temptation!)
Back to 6502 programming - there's a great diagram of the programmer's model for the 6502 by Bob Sander-Cederlof. Here it is as an image (from here) and as text (got to love ascii diagrams):

Back to 6502 programming - there's a great diagram of the programmer's model for the 6502 by Bob Sander-Cederlof. Here it is as an image (from here) and as text (got to love ascii diagrams):

Code: Select all
TRANSFER OPERATIONS
-------------------
+-----------------------------------------------------------------+
| MEMORY |
| $0000-$FFFF |
+-----------------------------------------------------------------+
^ | ^ | ^ |
| | | | | |
STX LDX STA LDA STY LDY
| | | | | |
| V | V | V
+--------------+ +--------------+ +---------------+
| |---TXA-->| |<--TYA---| |
| X-REGISTER | | A-REGISTER | | Y-REGISTER |
| |<--TAX---| |---TAY-->| + |
+--------------+ +--------------+ +---------------+
| ^ ^ |
| | | |
TXS TSX PLA PHA
| | | |
V | | V
+--------------+ +--------------+ +---------------+
| | | |---PLP-->| |
| S-REGISTER | | STACK | | P-REGISTER |
| | | $0100-$01FF |<--PHP---| NV*BDIZC |
+--------------+ +--------------+ +---------------+
OTHER OPERATIONS
----------------
+-----------------------------------------------------------------+
| A-REGISTER X-REGISTER Y-REGISTER MEMORY |
|-----------------------------------------------------------------|
| Arithmetic: ADC INX INY INC |
| SBC DEX DEY DEC |
| |
| Logical: AND --- --- BIT |
| ORA --- --- --- |
| EOR --- --- --- |
| |
| Shift: ASL --- --- ASL |
| LSR --- --- LSR |
| ROL --- --- ROL |
| ROR --- --- ROR |
| |
| Compare: CMP CPX CPY --- |
+-----------------------------------------------------------------+
+----------------------------------------------------+
| Status: SET CLEAR BRANCH |
|----------------------------------------------------|
| CARRY SEC CLC BCC, BCS |
| OVERFLOW --- CLV BVC, BVS |
| DECIMAL SED CLD -------- |
| INTERRUPT SEI CLI -------- |
| ZERO --- --- BEQ, BNE |
| MINUS --- --- BPL, BMI |
+----------------------------------------------------+
Jump: JMP, JSR -------------------------------------------
6502 Programming Model, Bob Sander-Cederlof
Return: RTS, RTI Apple Assembly Line, May 1981
http://txbobsc.com/aal/1981/aal8105.html#a4
Other: NOP, BRK -------------------------------------------
Last edited by BigEd on Wed Mar 27, 2019 10:20 pm, edited 2 times in total.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Need help to Add numbers to X and Y registures
That was a good way to visualize it. Ed, would you like to post an updated version, perhaps under "Tutorials, References, and Primers" at http://6502.org/tutorials/, to reflect the added CMOS instructions and addressing modes. It would be a good addition. Glaring are the lack of PHX, PLX, PHY, and PLY. Also lacking are TSB, TRB, INA, DEA, STZ, and BRA, and to a lesser extent, BBS, BBR, SMB, and RMB.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: Need help to Add numbers to X and Y registures
BTW I posted that diagram previously, in Best way to start learning 6502 Assembler which might perhaps be a good sticky thread in Newbies? If not, it might contain some material for such a post.
(I'm pretty sure I also straightened up the image but I can't find that anywhere.)
(Edit: found it, and updated the post upthread.)
(I'm pretty sure I also straightened up the image but I can't find that anywhere.)
(Edit: found it, and updated the post upthread.)