3

I don't understand the difference between an accumulator-based CPU architecture and a register-based CPU architecture. I know x86 is register-based but it has an accumulator-like register. I only ever hear people asking the difference between stack-based and register-based, but not register-based and accumulator-based. What are the advantages and disadvantages of each? And can I get some example assembly codes from each, where they differ, as well?

1

4 Answers 4

11

A register-based CPU architecture has one or more general purpose registers (where "general purpose register" excludes special purpose registers, like stack pointer and instruction pointer).

An accumulator-based CPU architecture is a register-based CPU architecture that only has one general purpose register (the accumulator).

The main advantage/s of "more that one general purpose register" is that the compiler doesn't have to "spill" as many temporary values onto the stack; and it's easier for the CPU to do more independent instruction in parallel.

For an example imagine you want to do a = (b - c) + (d - f) + 123. For an "apples vs apples comparision" I'll use Intel syntax 32-bit 80x86 assembly for both examples (but only use EAX for the accumulator-based CPU architecture).

For accumulator-based CPU architecture this may be:

    mov eax,[b]     ;Group 1

    sub eax,[c]     ;Group 2

    add eax,123     ;Group 3

    mov [a],eax     ;Group 4
    mov eax,[d]

    sub eax,[e]     ;Group 5

    add [a],eax     ;Group 6

Note that most of these instructions depend on the result from the previous instruction, and therefore can't be done in parallel. The ";Group N" comments are there to indicate which groups of instructions can be done in parallel (and show that, assuming some form of internal "register renaming" ability, "group 4" is the only group where 2 instructions are likely to be done in parallel).

Using multiple registers might give you:

    mov eax,[b]           ;Group 1
    mov ebx,[d]

    sub eax,[c]           ;Group 2
    sub ebx,[e]

    lea eax,[eax+ebx+123] ;Group 3        

    mov [a],eax           ;Group 4

In this case, there's one less instruction, and 2 less groups of instructions (more instructions likely to by done in parallel). That might mean "25% faster" in practice.

Of course in practice code does more than a relatively simple calculation; so there's even more chance of "more instructions in parallel". For example; with only 2 more registers (e.g. ECX and EDX) it should be easy to see that you could do a = (b - c) + (d - f) + 123 and g = (h - i) + (j - k) + 456 in the same amount of time (by doing both calculations in parallel with different registers); and it should also be easy to see that for accumulator-based CPU architecture you can't do the calculations in parallel (two calculations would take twice as long as one calculation).

Note: There is at least one "potential technical inaccuracy" in what I've written here (mostly involving the theoretical capabilities of register renaming and it's application on accumulator-based CPU architectures). This is deliberate. I find that going into too much detail (in an attempt to be "100% technically correct" and cover all the little corner cases) makes it significantly harder for people to understand the relevant parts.

1

Beyond the obvious difference of one register vs. many registers, a significant difference is in the way that operands are specified.

An architecture with many registers may specify a binary operator with three registers: one target and two sources — so can do something like: r0 = r1 + r2.  Program variables can live in the registers when appropriate.

Accumulator instructions sets implicitly source and target the accumulator so need only to specify one explicit operand to make a binary operation, and, that other operand is going to have to come from memory since there is only one register (an accumulator), so something like: acc = acc + memoryOperand.  An accumulator architecture necessarily uses memory to store program variables; the accumulator is in such high demand that it generally has to be used for every little operation, so doesn't usually store longer-lived program variables.

Architectures with registers vary, some are strict load/store architectures and others allow memory operands with arithmetic operators.  Some restrict operands to two, so a binary operator will both source and target one of the two operands.

-1

In the accumulator based, operations are done via interaction with the accumulator register at each step of the way. For example, (a + b) - d is Done as, put a in the accum, add b to the accum, value in the accumulator now contains the sum of a and b. Subsequently subtract d from accum gives final result. For complex operations there is a significant time sacrifice. Whereas register based can store values in individual registers and operations can be between registers. Hence (a+b)-d involves put a in reg x, put b in reg y, put d in reg Z. Add X and y. Subtract Z. This is more convenient and generates shorter code in complex programs. Saves time.

-2

The practical differences between accumulator and register based architectures are the following:

accumulator based: the code is smaller and runs slower.

register based: the code is larger and runs faster.

1
  • 3
    Do you have anything to back up these statements? Text books? Articles? Wikipedia? Examples? Mar 3, 2017 at 1:45

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