Intro to Registers

Probably the most important thing to know about when starting to learn assembly is the registers.

The CPU is always needs somewhere to temporarily store the data it is working on. These storage spaces are called registers. For the z80 processor there are two sizes of registers: 8-bit (eight 0's or 1's) and 16-bit (sixteen 0's or 1's). A lot of the time, we can use any of these registers interchangeably, but there are certain tasks where the CPU can only use certain ones. Here is an overview of each of the registers:

General-Purpose 8-bit Registers
These can usually be used in any place where you need to temporarily hold 8 bits of data.


 * A is also called the "Accumulator". It is the most important 8-bit register and the one you will be using the most. When the z80 chip tries to do arithmetic or access memory, this is the register it has to use.


 * B is commonly used as an 8-bit counter.


 * C is often used when you want to access hardware ports.


 * D is used for temporary storage.


 * E is used for temporary storage.


 * H is used for temporary storage.


 * L is used for temporary storage.

Specialized 8-bit Registers
These 8-bit registers have specific uses which discourage general data storage.


 * F is known as the "Flags". The 8 bits of this register will turn on or off (that is to say they "flag") when certain events have occurred. The uses of the flags will be explained later once we have gotten a little deeper. We cannot actually use F to store data since it is busy keeping track of other things.


 * I is also called the "Interrupt Vector". Sometimes we want to "interrupt" our program and jump to a certain piece of code. This register keeps track of where we want to jump.


 * R is also called the "Refresh Counter". This one increases every time the CPU does an instruction. This means we can't use this one to store data either, but if we need a random number this is usually a pretty good place to find one.

List of 16-bit Registers (8-bit Register Pairs):
Of course, having only 8 bits means we can only store the numbers 0-255. What if we need more? Well, we can always just pair up two of our 8-bit registers to get 16 bits. This lets us hold from 0 to 65535. Unfortunately, we can't just combine any two registers; they only pair up in special ways. Again, most of these can be used as general 16-bit data storage, but certain CPU operations will require certain registers:

General-Purpose 16-bit Registers
These 16-bit registers can mostly be used in any place where you need to temporarily store 16 bits of data.


 * HL has two purposes. One, it is like the 16-bit equivalent of the Accumulator, meaning it is used for 16-bit arithmetic. Two, it can store locations in memory when we want to access them.


 * BC is used as a Byte Counter in certain functions.


 * DE holds the address of a memory location that is a destination.


 * IX is called an index register. Almost everywhere HL is acceptable, so too is IX. Important to note that using IX results in slower and more inflated code than HL would (approximately double the size and time), so call on his services only when necessary. It can actually be split into 8-bit registers IXH and IXL, but some compilers will not recognize this because they are not defined in the compiler.
 * Sometimes it's better to use IX though, because it has a little trick up its sleeve. It makes use of "offsets", meaning that you can use it as a baseaddress. Just keep this in mind, as it will be explained later.


 * IY is almost exactly the same as IX. It can be split into 8-bit registers IYH and IYL, but these are also "undocumented".
 * IY can also be used as index register, but the only reason you should use IX first, is that IY holds the baseaddress of the system flags already. Keep this in mind aswell.

Specialized 16-bit Registers
These 16-bit registers have specific uses which discourage general data storage.


 * AF is actually not often used since A and F do not work together but sometimes they are paired for special reasons.


 * PC is the abbreviation for Program Counter, and it always contains the current position in the program. It cannot be accessed directly by any of the instructions. It cannot be split into 8-bit pieces.


 * SP is the Stack Pointer. The stack is a place where the values of 16-bit register pairs can be saved for later retrieval and will be explained later. SP can be used just like HL or BC, but this is usually discouraged because changing SP loses the location of the stack..

Shadow (Complimentary) Registers:
There is an additional set of registers that you can’t directly access, but sometimes they can be useful. At any one time, you can select either set of registers to work with by swapping the two sets very quickly with a single "exchange" command. These are normally only seen in their 16-bit pairs:

AF', BC', DE', HL'

These will receive more attention later.