6502 assembly A, X and Y. What are they?

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
Baka94
Posts: 12
Joined: 23 Feb 2014

6502 assembly A, X and Y. What are they?

Post by Baka94 »

Every guide about 6502 assembly talks about things called A, X and Y, but none of them explain what they are, where they are used, etc. It's odd that in the very beginning parts these are introduced but never explained.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 assembly A, X and Y. What are they?

Post by BigEd »

That's a very fundamental question! They are registers, byte-sized storage locations on-chip. To operate on data in memory, you typically have to load it into one of the registers then perform your work and eventually write out the result to memory. Or if not memory, then I/O devices. There are some operations which operate directly on memory, so you can increment a memory location without using any registers, but if you want to add two numbers, you'll find you need to use A, the accumulator.

See this diagram:

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   -------------------------------------------
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: 6502 assembly A, X and Y. What are they?

Post by GARTHWILSON »

The top section of http://6502.org/tutorials/ should help. The book "Programming the 65816, including the 6502, 65C02 and 65802" there is excellent, and I'm sure other ones listed there are too, although I'm not familiar with all of them.

"A" is short for "accumulator" and is kind of the central register. X and Y have indexing functions. Other registers in the 6502 that the programmer deals with directly are P (processor status), PC (program counter), and S (stack pointer).
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?
Baka94
Posts: 12
Joined: 23 Feb 2014

Re: 6502 assembly A, X and Y. What are they?

Post by Baka94 »

Few more questions:
1. What's the difference between the accumulator (A) register and the indexing function (X and Y) registers (in what kind of situations they are used)?
2. What are the S and P registers and the stack used?
3. How is the Memory are divided for specific actions (like drawing sprites)?
4. Is there a complete colour chart made for the available colours for 6502 and their data values?
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: 6502 assembly A, X and Y. What are they?

Post by barrym95838 »

Most of those questions can only be answered in the context of a particular implementation, like C64, Apple, Atari, Atom, KIM, NES, VIC20 etc. All of those systems use a variant of the 6502, but have a diverse collection of support hardware, and that support hardware determines how the memory, display, keyboard and other I/O are to be utilized.

Mike
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: 6502 assembly A, X and Y. What are they?

Post by GARTHWILSON »

A is where the logic and arithmetic functions start and end. X & Y are for indexing; for example, the instruction LDA $14F3,X will load A (the accumulator) with the value read from the address that is the sum of $14F3 and the contents of X; so for example if X had 5 in it, then A would be loaded with the contents of address $14F3+5, or $14F8. Indexing can be used with indirects also.

S (the Stack pointer) is like an index register to stack operations which are in page 1, ie, from addresses $0100 to $01FF. Stack operations automatically increment and decrement S.

P (Processor status) register has status bits (or flags) telling if an arithmetic or logic function resulted in a negative result (N flag), result of zero (Z flag), or that oVerflow caused the sign bit to be wrong (V flag), or resulted in a Carry or a borrow (C flag), or affect the processor's operation like the Decimal (D) flag, or Interrupt-disable (I) flag, or tell if an interrupt was caused by a software Break instruction (B flag).

Most of the memory range can be used in many ways, although there are a few requirements, given in the memory map requirements page of the 6502 primer, at http://wilsonminesco.com/6502primer/MemMapReqs.html .

It's far too much to explain in detail in a forum post, but hopefully this will get your imagination going. The books at the link above will explain it all. I'm in the process of writing a treatise on stacks, but the basics will be covered just fine in the books. [Edit: It's up, at http://wilsonminesco.com/stacks/index.html .]

As for the number of colors and so on, that does not really depend on the processor, but the attached video hardware.
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?
User avatar
BigDumbDinosaur
Posts: 9428
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: 6502 assembly A, X and Y. What are they?

Post by BigDumbDinosaur »

Baka94 wrote:
Few more questions:
1. What's the difference between the accumulator (A) register and the indexing function (X and Y) registers (in what kind of situations they are used)?
2. What are the S and P registers and the stack used?
3. How is the Memory are divided for specific actions (like drawing sprites)?
4. Is there a complete colour chart made for the available colours for 6502 and their data values?
I (and no doubt others) highly recommend that you read the available literature before posing questions of this type. We are more than happy to help someone starting out with the 6502 assembly language, but like to see that that individual has made a modicum of effort to seek relevant material.

Garth Wilson already suggested reading the 6502/65C02/65C816 programming manual, which covers the 6502 family from top to bottom, and goes from the most basic levels (What is a byte?) to fairly advanced programming concepts. I also recommend that you read Richard Mansfield's Machine language for Beginners, which starts from scratch, making only small assumptions about the reader's knowledge, meaning it doesn't explain basic computer concepts. Mansfield's book was the jumping off point for countless 6502 programmers in the 1980s (many of whom went on to become very fluent in 6502 assembly language) and remains valid to this day.

If you get the urge to build a 6502-powered device you should look at the resources here and also check out Garth's website for lots of helpful hardware info.

Incidentally, questions 3 and 4 are machine-specific. The 6502 knows nothing about colors, sprites, sound, etc. Such features are implemented in other devices.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
Aaendi
Posts: 56
Joined: 26 Jun 2013

Re: 6502 assembly A, X and Y. What are they?

Post by Aaendi »

Baka94 wrote:
Few more questions:
1. What's the difference between the accumulator (A) register and the indexing function (X and Y) registers (in what kind of situations they are used)?
2. What are the S and P registers and the stack used?
3. How is the Memory are divided for specific actions (like drawing sprites)?
4. Is there a complete colour chart made for the available colours for 6502 and their data values?
When you mentioned about the color chart it made me thinking, was there ever a computer system with the CPU having its own graphics output? I'm now imagining a CPU with a scan line buffer built in, and blitting instructions.
User avatar
BigDumbDinosaur
Posts: 9428
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: 6502 assembly A, X and Y. What are they?

Post by BigDumbDinosaur »

Aaendi wrote:
Baka94 wrote:
4. Is there a complete colour chart made for the available colours for 6502 and their data values?
When you mentioned about the color chart it made me thinking, was there ever a computer system with the CPU having its own graphics output? I'm now imagining a CPU with a scan line buffer built in, and blitting instructions.
Well, the graphics processors (GPU) used in modern video cards are capable of "blitting" and other tasks that offload a lot of the grunt work of managing the video display from the microprocessor (MPU). In such scenarios, the MPU issues relatively terse commands to the GPU and the latter works independently of the MPU to generate and manipulate the the image.

The 8563 and 8568 video display controllers, which were used in the Commodore 128, were 6502 bus-compatible devices with some primitive intelligence, such as block fill and copy functions.

However, to answer your question, I'm not aware of any system in which the MPU itself drives the video display. Someone else here might know differently.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 assembly A, X and Y. What are they?

Post by BigEd »

Parallax' propeller chip has (minimal) video hardware on-chip. See for example http://www.rayslogic.com/propeller/propeller.htm and http://learn.parallax.com/propeller-c-s ... xt-display

Cheers
Ed
Post Reply