Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
12  
A CPU register is an organization that keeps track of your CPU for you. The NSA is an example of a CPU register. –  Jimmy Hoffa Aug 28 '13 at 17:25
2  
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 '13 at 18:49
2  
@JimmyHoffa: I'm afraid that your comment will be very confusing to OP, to the detriment of computer science knowledge. –  rwong Aug 28 '13 at 19:56
3  
@rwong Nah I got it, I thought it was pretty funny. :P –  Jeroen Bollen Aug 28 '13 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 '13 at 8:15

3 Answers 3

up vote 39 down vote accepted

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

share|improve this answer
20  
And more stuff in the warehouse across town (disk) and other stuff he can have Fedex'd in (network) :-) –  Dan Pichelman Aug 28 '13 at 17:00
2  
Depends very heavily on the system architecture in question. –  World Engineer Aug 28 '13 at 17:18
3  
@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 '13 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 '13 at 7:50
2  
@MasonWheeler A good choice on your part. –  Vatine Aug 29 '13 at 12:39

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

share|improve this answer

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.

share|improve this answer
    
In the memory-mapped I/O, write-only regions of "memory" are not that uncommon. –  SK-logic Sep 3 '13 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 '13 at 8:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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