32

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?

10
  • 13
    A CPU register is an organization that keeps track of your CPU for you. The NSA is an example of a CPU register. Aug 28, 2013 at 17:25
  • 3
    CPU level-1 cache is often made of the same type of silicon stuff as registers. It is not quite as fast because it needs extra lookups to associate cached addresses with cache locations. But registers and cache are definitely different places on the chips.
    – Zan Lynx
    Aug 28, 2013 at 18:49
  • 6
    @JimmyHoffa: I'm afraid that your comment will be very confusing to OP, to the detriment of computer science knowledge.
    – rwong
    Aug 28, 2013 at 19:56
  • 3
    @rwong Nah I got it, I thought it was pretty funny. :P
    – Jeroen
    Aug 28, 2013 at 20:05
  • 1
    And another difference is size: a register file is rarely larger than a couple of hundreds of words, while L1 cache can easily be something as large as 64k.
    – SK-logic
    Aug 29, 2013 at 8:15

4 Answers 4

66

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.)

15
  • 27
    And more stuff in the warehouse across town (disk) and other stuff he can have Fedex'd in (network) :-) Aug 28, 2013 at 17:00
  • 2
    Depends very heavily on the system architecture in question.
    – user28988
    Aug 28, 2013 at 17:18
  • 5
    @Binero in a 16 bit x86 processor you have registers named Ax, Bx and so on. When they moved to 32 bit processors they Extended the registers to 32 bit EAx, EBx, if you access Ax you get the low 16 bits of EAx. In 64 bit mode they named the registers RAx with the lower 32 bits being EAx and the lower 16 bits still being Ax. check out these diagrams to see how you get to other parts of bytes like AH to get the high byte of the 16 bit Ax register: en.wikipedia.org/wiki/X86#Structure
    – stonemetal
    Aug 28, 2013 at 19:14
  • 2
    And in modern CPUs, this kinda breaks down, as well, due to the registers being merely temporary mappings to an in-core bank of Really Fast Memory, changing depending on the instruction stream and how closely it is following the predicted instruction stream.
    – Vatine
    Aug 29, 2013 at 7:50
  • 2
    @Vatine: Yes, there's a very deep rabbit hole when you start peeling away the layers of abstraction and looking at real implementation. But when dealing with someone asking a beginner-level question, I prefer to give more basic answers that will enlighten, rather than confuse further. Aug 29, 2013 at 12:38
3

Actually A Register in CPU terminology is small Named chunk of memory Available inside a microprocessor(CPU), Registers have specific names, sizes and functions varies from processor to processor, for example, if take 8085 microprocessor is an 8-bit processor which has 8-bit Registers(A:Accumulator, B,C,D,E, H, and L Registers and one flag register all are 8-bit). Two 16-bit registers PC and SP all are having special function and functions comes into the picture during assembly programming. Few registers control is beyond the programmer.

If you take another processor Registers will vary let say 8086 is 16-bit processor and it has AX,BX,CX and DX all are 16-bit, PC, SP and Flag registers.

as you quoted in the question they are to speed up the program execution and acts as processor cache but now a days processor architecture changed and they(Intel) add lot of memory calling processor cache

but there is slight difference between processor(CPU) cache and Processor(CPU)Registers, registers actually needed for some special activities like memory pointer, program status etc.. Ex:PC:Program Counter which acts a memory pointer in the program memory, SP:Stack pointer which acts a memory pointer in the stack memory. and Accumulator is buffer and main register to access ALU for Arithmetic Ops...

You can see Mason Wheeler explanation for exaples

1

I think it helps to think that Registers are not memory and should not be thought of as such.

Think more like OO - Register is a class, not derived from Memory and Memory is a class not derived from register, but the Register class has methods (Machine Op Codes) to convert its data to and from Memory. Memory on the other hand knows nothing about Registers and cannot invoke actions on them. As a result, all CPU operations are performed by registers, which often access memory.

It is not uncommon to see write only registers - hardly an attribute of memory. Its also possible for a register value to change without writing to it - again, not the behavior you expect of memory.

2
  • In the memory-mapped I/O, write-only regions of "memory" are not that uncommon.
    – SK-logic
    Sep 3, 2013 at 8:52
  • @SK-logic - Correct - however the context of the question asked clearly the OP thinks of memory as something you read from and write to. No need to confuse him more:)
    – mattnz
    Sep 3, 2013 at 8:59
0

The answer provided by @Mason Wheeler was accurate, but I think it is possible to put your question under another perspective. Judging by your question, it sounds to me the concept you need to perfectly understand the difference between a cache and a register is the data path. As Mason properly pointed out, the logic of the CPU (i.e., its data path) is designed in a such way that the memory information can not be dealt with directly by the CPU and that's why the registers exist. In fact, the CPU is not even capable of decoding the current instruction of the program it is running if that instruction was not loaded in the proper register first (usually the one named IR, "Instruction Register").

This is related to the way the CPU is wired. There is no physical path between the memory and the ALU; all data supplied to the ALU have to be buffered somehow in some register. It could be different, but the circuitry required to connect the memory into the ALU directly would be too complex: it is easier and more efficient to mediate all communication between the memory and the ALU via the register file, as determined by the aforementioned data path. In fact, even when a given instruction specifies a memory position as an operand (an addressing mode known as direct addressing), the corresponding data unit is loaded into a register known as the MBR (Memory Buffer Register, sometimes called MDR, Memory Data Buffer).

Notice that from the point of view of the CPU, it doesn't really matter if the information (data or code) comes from the main memory or the cache, but the latter is much faster. Caches exist for performance reasons, registers exist because of the CPU's design (i.e., because of the data path). Clever programmers (clever compilers, actually) try to maximize the use of registers in order to minimize memory accesses (registers are faster than either cache or memory). This is justifiable because information stored in registers tend to be used multiple times and, in fact, this is one of the principles of the RISC philosophy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.