Registers
Registers are the key to understanding how a CPU goes about its business. No CPU instruction can happen without them.
Registers
A CPU has a number of registers, which are, like all the other components, just assembled from large numbers of transistors. What do they do? Well, there are different types of registers in any chip, and these registers perform various roles. Essentially, however, registers simply store data. Recall from the Hardware section that the CPU reads data stored in memory by specifying a memory address and then reading the data that appears at the data pins from the memory. This data flows into the CPU as bits along the data bus. Once in the CPU, they are stored in registers. Why is this done? Well, it's because once the data is stored in a register, that data can be manipulated very, very quickly, since no more memory accessing is required. At this level data is held internally by the CPU and can benefit from its own huge operational speed.
In conclusion, the registers are simply storage areas for holding instructions and data that have been fetched from memory.
The N-Bit CPU
It is a subset of these registers that defines whether a chip is N-bit. We often hear a chip referred to as a 16 bit chip, a 32 bit chip, or lately, a 64 bit chip. A little while ago, when I knew a lot less about this stuff than I do now and before I started delving into assembly, I found this term highly confusing and set out to find out exactly what it meant. Did it refer to the bit width of the address bus? Plainly, no, since early 16 bit chips had a 20 bit address bus (allowing 1MB memory access). So is it the data bus? This seemed to tie in a little closer, but the answer was still no, since the early Intel 8088 had an 8 bit data bus but was a 16 bit chip. Looking at newer chips, 64 bit address buses have been around since the Pentium, but the Pentium and its successors have all been 32 bit chips. (To see comparisons of various chips, make sure you look at the CPU History section.)
"Well, it must be something to do with the registers," said I. Which registers? Modern 32 bit chips have 64 bit or even 128 bit floating point registers (more on this later), so that's not it. Well, after playing around with some assembly language I learned that there are a set of registers for the purpose of performing integer (whole number) calculations. These are referred to as general purpose registers (GP) and it is the number of bits that these can hold that determines the value of N for an N bit chip. To put it more simply, a 32-bit CPU has 32-bit GP registers, meaning that they can store 32 bit binary numbers.
This is all so plainly obvious to me now that it seems unbelievable that I was confused for so long. This is the kind of knowledge an assembly coder takes for granted. Yet, as a beginner, this stuff can be really confusing. This is why it's useful to have a handy place to start learning.
A deeper look at Registers
Your typical x86 compatible CPU has many registers, and different registers have different roles. CPUs before the 386 were 16 bit and had four 16 bit segment registers (called CS - code, DS - data, SS - stack, ES - extra) whose role was to hold segment addresses. (32 bit x86 chips also have segment registers FS and GS; they are still 16 bit registers however.) For those of you that are interested, a segment is a region of memory spanning up to 64kB that begins on a paragraph boundary. Such paragraph boundaries occur every 16kB from the beginning of memory. It's fairly complicated, but the use of 16 bit segment addresses and 16 bit offsets from these addresses allows any 20 bit memory location within the first 1MB of real mode memory to be expressed using two 16 bit registers! This was and still is an essential conversion that must take place when operating in the real mode memory model (which was the only mode available to the early CPUs such as the 8086 and 8088 which had a 20 bit address bus).
General purpose registers (or GP registers), as their name suggests, perform a variety of tasks, including integer arithmetic (such as addition), bit shifting, and storing memory offset addresses. The older 16 bit CPUs had eight 16 bit GP registers, and, as already stated above, it is the bit width of these registers that defines the bit-ness of the CPU. In x86-compatible CPUs (more on this later), these registers are designated AX, BX, CX, DX, BP, SI, DI and SP.
While these are all general purpose registers, some are more general than others. For example, SP is the stack pointer, and it is the job of this register to hold the memory address of the top of the stack. The stack, as it happens, is a region of memory that acts very much like a large pile of dishes... When you clean a dish, you put it on the pile. When you need a dish, you pull it off. Thus the last dish on the pile is the first one off. This kind of approach is called LIFO - Last In, First Out, and this approach proves very useful for programmers, especially when they need somewhere to temporarily store data.
Why would they want somewhere to store data? Isn't that what the GP registers are for? Indeed this is true, but the number of registers available is very limited. In high level programming languages, for example, you can define as many variables to work with as you want. (Note that a variable is merely a name for a piece of data.) But the CPU has only a limited number of GP registers. Since all data manipulation must occur in registers, it is the job of the compiler to make efficient use of these registers. The less reading and writing to main memory (which is slow in CPU terms), the faster a program will run.
In fact, to increase the number of registers available to assembly coders (and software compilers alike, since a compiler is really a kind of automated assembly programmer!), the first four of these GP registers can each be split into two 8 bit half-registers. For example, AX can be referred to as AL (the Lower 8 bits) and AH (the High 8 bits).
By the way, many of these terms may be unclear to you. However, they will soon start to make sense if you persevere.
In more modern 32 bit CPUs (i.e. from the 386 onwards), these eight registers are 32 bit, not 16. However, they can still be used as 16 bit registers, in which case the lower 16 bits only are used. When used as 32 bit registers, the letter E is prefixed to the register name to distinguish it from the 16 bit equivalent. For example, in a 386 you could use register AX if you only wanted 16 bits, or instead use register EAX (though it is in fact the same register!) if you wanted to use 32 bits.
Now it's time to look at the purpose of the CPU.
| Just Too Good Last updated: June, 2006 (DJL) |
Drop me a line