Skip to content

Computer Architecture

How a CPU's register file works

A register file is a processor's set of registers, the small, fast stores its instructions work on, all sharing one data input. A destination number names which register a write goes to, and a small decoder turns that number and a WRITE signal into one write enable per register, so only the named one changes. Reading works the other way round: a multiplexer picks which register's value goes out.

One register: a flip-flop that holds

Each bit is a D flip-flop with a chooser in front of D. With its enable at 1, the chooser passes DATA; at 0, it feeds the flip-flop's own Q back, so each tick reloads the same value. A register keeps its number until it's told to take a new one, and a write replaces what was there: nothing is added.

Names are numbers

Give two registers numbers, A = 0 and B = 1. DEST holds the number of the one to write:

enable A = WRITE AND NOT DEST · enable B = WRITE AND DEST

With WRITE at 0, both enables are 0 and nothing changes, whatever DATA says. With WRITE at 1, exactly one enable is 1. Four registers need a 2-bit DEST and a 2-to-4 decoder, and 32 registers need 5 bits, because 2⁵ = 32.

Live experiment

Write to the right register

Two registers as labelled jars, with DATA, DEST and WRITE on switches. On a tick only the jar DEST names takes DATA, and only if WRITE is 1; the other keeps its bit. Then wire B's enable wrongly and write to A: B copies it too.

Try it yourself: free lesson

“Two registers” is lesson 3 of 6 in Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 16 before it first.

Reading: two ports

An instruction such as ADD R3, R1, R2 reads two registers and writes a third. So real register files have two read ports, each a multiplexer choosing a register by number, and a write port, the decoder. All three work in a single clock cycle: the reads feed the ALU, and the result is written back on the clock edge.

Why registers and not memory?

Registers sit right beside the ALU and can be read in a fraction of a cycle, while main memory takes many cycles to answer. So compilers keep the busiest variables in registers and spill the rest to memory. The AVR chip on an Arduino Uno has 32 8-bit registers, R0 to R31, and most of its instructions carry 5-bit register numbers to pick them. 64-bit ARM processors have 31 general registers, each 64 bits wide.

Where you'll meet it

Every processor has one, from the smallest microcontroller to a graphics card, which keeps thousands of registers so that many threads can each have their own. When you read assembly, every name like R3, x5 or rax is an address in the register file.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Two registers” is lesson 3 of 6 in Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 16 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3DEST is 1 and WRITE is 1. On the tick…
    Show the answer

    Answer: B takes DATA and A keeps its bit. DEST 1 names B. Enable B is WRITE AND DEST, which is 1. Enable A is WRITE AND NOT DEST, which is 0, so A holds.

    • Why not “A takes DATA and B keeps its bit”? DEST names the register to write, and 1 names B. A's enable, WRITE AND NOT DEST, is 0.
    • Why not “Neither changes”? WRITE is 1, so the register DEST names does take DATA. Nothing changes only when WRITE is 0.
    • Why not “Both take DATA”? Only one enable can be 1 at a time: one uses DEST and the other NOT DEST.
  2. Question 2 of 3WRITE is 0. On the tick…
    Show the answer

    Answer: Both registers keep their bits. Both enables have WRITE in their AND, so both are 0, and both pickers feed Q back.

    • Why not “Both take DATA”? Both enables include WRITE in their AND, so with WRITE at 0 neither register can take DATA.
    • Why not “B copies A”? No path runs from A to B. Each register takes either DATA or its own Q.
    • Why not “A clears”? Nothing clears a register. With its enable at 0, it feeds its own Q back.
  3. Question 3 of 3Enable A is…
    Show the answer

    Answer: WRITE AND NOT DEST. A is register 0, so it wants DEST at 0: NOT DEST, ANDed with WRITE so nothing changes unless a write is asked for.

    • Why not “WRITE OR DEST”? An OR would write A whenever WRITE or DEST is 1, including every write meant for B.
    • Why not “DEST”? DEST alone ignores WRITE, so A would change on every tick DEST is 1, and 1 names B, not A.
    • Why not “WRITE AND DEST”? That's B's enable. A is register 0, so it wants DEST at 0.
Keep practising: free lesson

“Two registers” is lesson 3 of 6 in Program Peninsula, Computer Architecture World 3. Free with an account. Lessons unlock in order, so you’ll do the 16 before it first.

Questions people ask

What is a register file in a CPU?

The block of registers that instructions can name, with the decoder that steers each write into one of them and the multiplexers that read them out. It's the fastest storage in the processor, from a handful to a few dozen numbers.

How many bits does it take to name a register?

Enough to count them: n bits name 2ⁿ registers. 8 registers need 3 bits, 16 need 4 and 32 need 5. Those bits sit inside every instruction, which is one reason processors don't have thousands of registers.

What is the difference between a register and memory?

A register is a few flip-flops inside the processor, named directly by instructions and read within a clock cycle. Memory is a large array outside the core, reached by address and taking many cycles. Programs load values into registers, work on them, and store results back.

Every topic