Commodore Free Magazine, Issue 78 - Part 8
From
Stephen Walsh@39:901/280 to
All on Sun Mar 23 17:59:20 2014
e architectures are radically
different. The 6502 and 65816 belong to the same family, and it can be
said that the 65816 is a superset of the 6502, which means that the 65816
is compatible to and builds and expands upon all that is contained within
the 6502.
The most obvious difference between the 6502 and 65816 is the size of data their registers are able to process. The registers in the 6502 work with
8-bit data, while registers in the 65816 are capable of operating on 8 or 16-bit data. Bits are the parts that make up a byte, a familiar term that
is the basic unit of data used in computers. There are 8 bits in a byte,
and each bit is a significant sub-part of the byte, so when we refer to a processor as having 16-bit registers, all we are really saying is that its registers are capable of operating on two bytes of data at a time (two
bytes is commonly called a word). A byte may contain a value 0-255
(decimal) and a word may contain a value 0-65535 (decimal), and its value
is determined by adding each bit together. Each bit (starting from the
lowest bit, or bit 0) has a value that is an increasing power of two. We
will discuss with greater detail the different numbering systems used by assembly language programmers in the next article. So then the next
question becomes, what is a register?
THE 6502/65816 REGISTERS
A register is a special memory location within the processor itself. It is
a place where intermediate results, addresses, and other information which
must be accessed quickly are stored. Since registers are built into the processor itself, they can be accessed and manipulated much faster than external memory. Registers are the "middle-man" when moving data around
the computer; you need to first move data from memory into a register
before moving it to another memory location. Some instructions perform operations on only a single bit within a register; others on two registers
at once; and still others move data between a register and external memory.
The 6502/65816 are not considered register-oriented machines when compared
to modern processors, and have a comparatively small set of registers, each dedicated to a special purpose. The 6502/65816 instead rely on per-cycle efficiency and a large number of addressing modes, particularly the
direct-page indirect addressing modes, to give them their power. If you compare the two processors we look at in this series of articles - the
8-bit 6502 and the 16-bit 65816 - you will find they both have a basic set
of registers in common.
The key difference between the 65816 and the earlier processors in the 65x family is that the 65816's three primary user registers - the A, X and Y registers - can be toggled between eight and sixteen bits. With its
ability to change register size, the 65816 functions equally well with
eight or sixteen bits.
THE A REGISTER
The A Register, commonly called the Accumulator, is the primary user
register and generally holds one of the operands, as well as the result, of
any of the basic data-manipulation instructions. An operand is the part of
a computer instruction which specifies which data is to be referenced, manipulated or operated on. The operand may be a processor register, a
memory address, a literal constant, or a label. Almost all arithmetic is performed on the data in the A Register, with the result of the operation
being stored in the A Register. Because the A Register is the primary user register, there are more addressing modes for A Register operations than
for any other register. Both the 6502 and 65816 contain one A Register.
Talking history again - another way Mensch was able to lower the cost of
the 6502 (relative to the 6800) was by eliminating a second accumulator, or
B Register. In the 6800 the A and B Registers were two distinct 8-bit accumulators. The A/B naming scheme was revived in the 65816 but this time
A and B refers to the lower and upper 8-bit halves of the 16-bit C
Register.
THE 6502 8-BIT A REGISTER
Only one byte is ever fetched from memory when the A Register is loaded, or
for operations which use two values - one from memory and the other in the
A Register. In order to perform an operation on a multi-byte (16-bit or greater) value the operation must be broken up into steps that work on each byte separately.
THE 65816 8/16-BIT A REGISTER
The 65816 the accumulator is called the C Register, and is made up of the A Register (lowest 8 bits) and B Register (highest 8 bits). In assembly
language it is permissible to refer to the 16-bit accumulator as "A" but it
is considered somewhat more correct to use "C."
You can select which size (eight or sixteen bits) you wish to use by
executing special control instructions (REP and SEP) that modify the m flag
in the P Register. 16-bit capability enhances processing power
tremendously while significantly lowering the number of instructions needed
to perform basic operations. In 8-bit accumulator mode (m = 1) the upper 8 bits in Register B are "hidden"; that is, they are still there but not
directly accessible. There is one instruction (XBA) which exchanges the
8-bit A and B registers, allowing you to access the hidden upper eight
bits.
THE X AND Y INDEX REGISTERS
The X and Y index registers are generally used as components in generating effective addresses when any of the indexed addressing modes are used, or
as loop counters. They can be easily incremented or decremented; which
means the value in the index registers can be increased or decreased by 1, using just a single instruction. They are useful in accessing tables (as
we will see below), moving memory, and counting loop iterations. The index registers differ from the A Register in that no logical or arithmetic operations (other than incrementing, decrementing, and comparing) may be performed upon them.
The use of indexing allows easy access to continuous series of memory locations. Indexing is performed by adding one of several forms of base addresses, specified in the operand field of an instruction, to the
contents of an index register. Although the X and Y index registers are basically similar, their capabilities are not identical. Certain
instructions and addressing modes work only with one or the other of these registers, so it starts to make sense the more you use them and become comfortable with the different addressing modes available to them. You can select which size (eight or sixteen bits) you wish to use by executing
special control instructions (REP and SEP) that modify the x flag in the P Register.
Indexing is an extremely important part of assembly language (you will use indexing a lot), and can be loosely compared to array addressing in higher level languages. Without indexing it would be impossible to refer to a
block of data without performing what could become a very long series of instructions (higher level languages would be affected by this in the same way). An array of 100 items would require 100 instructions to address
every item. Clearly this would be a very inefficient use of memory
(although somewhat faster in many cases. With indexing you can refer to
any of the items in the block with just one instruction (and the help of a
few others to manipulate the index register). Table 1 looks at a simple example which demonstrates why indexing is so important.
- - - - - - - - - - - - - - - - - - -
TABLE 1. THE NECESSITY AND ADVANTAGE OF INDEXING
We have a data table containing 200 screen codes located starting at
address 49152 (decimal). If indexing did not exist we would have to do something similar to the following to move those screen codes to screen RAM
for display.
LDA 49152 ;load the A Register
with the contents of
location 49152
STA SCREEN ;copy to screen RAM
LDA 49153 ;load the next screen
code
STA SCREEN+1 ;copy to the next
screen location
LDA 49154 ;load the next screen
c
--- MBSE BBS v1.0.01 (GNU/Linux-i386)
* Origin: Dragon's Lair ---:- bbs.vk3heg.net -:--- (39:901/280)