What Are CPU Registers?

cpumemoryregisterterminology

This question has been bothering me for some time now and today I figured I would Google it. I've read some stuff about it and it seemed very similar to what I've always known as processor cache.

Is there a difference between the two or am I right when I think they are the same?
Is a register actually required to be inside a CPU for it to work?

According to Wikipedia a register is a place in the CPU where memory can be quickly accessed and modified before being sent back to the RAM. Did I understand this wrong or are the cache and register actually the same?

Best Answer

They're not quite the same. The registers are the places where the values that the CPU is actually working on are located. The CPU design is such that it is only able to actually modify or otherwise act on a value when it is in a register. So registers can work logic, whereas memory (including cache) can only hold values the CPU reads from and writes to.

Imagine a carpenter at work. He has a few items in his hands (registers) and then, very close by on his workbench (cache) things he is frequently working on, but not using right this moment, and then in the workshop (main memory) things that pertain to the project at hand but that are not immediately important enough to be on the workbench.

EDIT: Here's a simple explanation for how register logic works.

Let's imagine we have four registers named R1..R4. If you compile a statement that looks like this:

x = y + z * 3;

the compiler would output machine code that (when disassembled) looks something like this:

LOAD  R1, ADDRESS_Z //move the value of Z into register 1
MUL   R1, 3         //multiply the value of register 1 by 3
LOAD  R2, ADDRESS_Y //move the value of Y into register 2
ADD   R1, R2        //adds the value in R2 to the value in R1
STORE R1, ADDRESS_X //move the value of register 1 into X

Since most modern CPUs have registers that are either 32 or 64 bits wide, they can do math on any value up to the size they can hold. They don't need special registers for smaller values; they just use special ASM instructions that tell it to only use part of the register. And, much like the carpenter with only two hands, registers can only hold a small amount of data at once, but they can be reused, passing active data in and out of them, which means that "a lot of registers" don't end up being needed. (Having a lot available does allow compilers to generate faster code, of course, but it's not strictly necessary.)

Related Topic