# The accumulator

*The one register the machine computes into*

The accumulator is the single working register at the center of an accumulator-machine CPU: arithmetic and logic operations implicitly use it as one operand and write their result back into it, so an instruction like ADD means add a memory value to the accumulator.

Group: Processor
URL: https://digiwleea.wleeaf.dev/learn/accumulator/

Our CPU is an **accumulator machine**: it has one special [register](https://digiwleea.wleeaf.dev/learn/register8/), the **accumulator** (`ACC`), that every arithmetic instruction reads and writes without having to name it. `LOAD` puts a memory value into `ACC`; `ADD` adds a memory value to whatever is in `ACC` and leaves the sum in `ACC`; `STORE` copies `ACC` out to memory. The accumulator is where a computation accumulates, hence the name.

## Register plus ALU, wired in a loop

Physically the accumulator is a [register](https://digiwleea.wleeaf.dev/learn/register8/) whose output feeds one input of the [ALU](https://digiwleea.wleeaf.dev/learn/alu/), and the ALU's result feeds back into the register's `D` input. Each clock, the ALU combines `ACC` with the other operand (a byte from memory) and the register loads the result. Because the register only captures on the [clock edge](https://digiwleea.wleeaf.dev/learn/clock/), that feedback loop is safe: the new value is written cleanly on each tick, exactly like the [register bit](https://digiwleea.wleeaf.dev/learn/regbit/) feeding itself.

```
on each ADD: ACC <- ACC + memory[addr]
```

_Circuit diagram: The accumulator wired to its ALU (ACCALU): the register ACC feeds the ALU, the ALU result loops back into ACC. Drive the function code (F0, F1) and the B operand and pulse the clock to run operations through it. Open it in the lab and step a sequence, clear, add 5, add 10, subtract 15, watching ACC show 0, 5, 15, 0. The ZERO flag lights when ACC is 0._

## Why one register?

Naming a single implicit register keeps instructions tiny. Because `ADD` always means "add to `ACC`", the instruction does not need to spell out where the result goes, so one operand field (a memory address) is enough, and a whole instruction fits in one byte. That is the design behind our [machine code](https://digiwleea.wleeaf.dev/learn/machine-code/). The tradeoff is that everything funnels through one register, so more values shuttle to and from memory; bigger CPUs add a [register file](https://digiwleea.wleeaf.dev/learn/register-file/) of many general registers to reduce that traffic.

> **WARN:** **Common mistakes.** The accumulator is a single register, not memory: it holds one byte at a time, so a value you want to keep must be [stored](https://digiwleea.wleeaf.dev/learn/machine-code/) before the next `LOAD` or `ADD` overwrites `ACC`. Do not confuse it with the [program counter](https://digiwleea.wleeaf.dev/learn/program-counter/) (which holds an address, not data). And in an accumulator machine the destination of arithmetic is implicit (always `ACC`), unlike register-file machines where you name the destination register.

**Q (Try it):** In our instruction set, where does the result of `ADD 0x2` land, and what was one of its operands?

**A:** The result lands in the **accumulator** (`ACC`). `ADD 0x2` computes `ACC + memory[0x2]` and writes the sum back into `ACC`. One operand is the byte read from memory address `0x2`; the other operand is the accumulator's current value, used implicitly.

### FAQ

**Q:** What is an accumulator in a CPU?

**A:** The accumulator is the single working register an accumulator-machine CPU computes into. Arithmetic and logic instructions use it implicitly as one operand and write their result back into it, so `ADD addr` means `ACC = ACC + memory[addr]`.

**Q:** Why use an accumulator instead of many registers?

**A:** One implicit register keeps instructions small: since the destination is always the accumulator, an instruction only needs to name the other operand, so it fits in a single byte. The cost is more memory traffic; larger CPUs add a [register file](https://digiwleea.wleeaf.dev/learn/register-file/) of many registers to cut that down.

**Q:** How is the accumulator connected to the ALU?

**A:** The accumulator's output feeds one input of the [ALU](https://digiwleea.wleeaf.dev/learn/alu/), and the ALU's result feeds back into the accumulator's data input. Each clock edge the ALU combines the accumulator with the other operand and the register captures the result, a safe feedback loop because the register only samples on the edge.

> **KEY:** The accumulator is the target of the [execute phase](https://digiwleea.wleeaf.dev/learn/execute-cycle/): once an instruction is fetched and decoded, execute routes an operand to the ALU and lands the result here.
