# How does a microprocessor operate in sequence?

I am trying to understand how microprocessors work. I am savvy about full adders and boolean arithmetic and buses and the like, and in fact, I have "built" a full adder before (in a computer simulation).

But as I try to learn more, I always get stuck on this: how does a microprocessor do things in sequence? All of the documentation and videos say "The control unit does this, and then it does this, and then it does this..." but how can it do that? In my full adder, you would set your inputs and it would pump out the answer, and there was no way for it to start doing something else. So what allows microprocessors to carry out several steps?

EDIT: For the people who have the same question... A processor has states that it operates in which control its behaviour, and these states are defined by what data is stored in it. Certain devices (such as latches or flip-flops) allow the processor to change its state with each clock pulse. I don't understand all the details but I hope this helps.

• The missing piece of the puzzle here is sequential logic. Read about flip flops and latches to start. – Matt Young Jan 27 '14 at 15:58
• This was exactly what I needed. With these keywords I have found a lot of explanation, and I think I can take it from here. Thanks very much! – Mahkoe Jan 27 '14 at 16:04
• @Mahkoe you could post what you learned as an answer to your own question, and accept it. – Phil Frost Jan 27 '14 at 16:43
• See also "state machine" – pjc50 Jan 27 '14 at 22:23

All microprocessors internally have a set of registers. Most are general purpose, and are used for arithmetic (+, -) or logical (AND, OR, XOR) operations, and other purposes. However there is one dedicated register called the program counter, or PC (not to be confused with the PC on your desk).

When a microprocessor powers up, the PC is loaded with a fixed value, often 0. The PC is a pointer into program memory, i.e. if the PC is 0, then an instructions from address 0 is fetched and executed. The PC is then incremented. I'll assume a 16-bit (two byte) microprocessor architecture, with a byte-addressable memory, so the PC is incremented by two each time. So the instructions are executed in sequence, 0, 2, 4, 6, 8 etc.

This sequential order is broken two cases -- a branch instruction (sometimes called a jump) and a subroutine call. A branch instruction contains the address of the next instruction to be executed. So if the instruction at memory address 8 was BRA 100, the next instruction to be fetched would come out of memory location 100. The next instruction after that would come from location 102, etc.

Branch instructions can be either unconditional or conditional. The example just given was an unconditional branch -- it is always taken. You can also have conditional branches, which depend on some condition such as a particular register being zero. If the register had an initial value, and was repeatedly being decremented at some point before the conditional branch instruction, this would form a loop.

A subroutine call also goes to another location, but when the code there (the subroutine) is finished, it executes a return instruction and returns back to the instruction following the subroutine call.

40    CALL 80     (subroutine call)
42    (comes back here after subroutine executes RET instruction)
.
.
.
80    (first instruction of subroutine)
.
.     (more instructions)
.
88    RET    (will go back to address 42)


There is usually a special area in memory called the "stack" which keeps track of the address for the subroutine to return to.

If you can find a copy of 'The Art of Digital Design' by Prosser & Winkel, this has a good explanation of how microprocessors work at the logic gate level (the last part of the book is a detailed worked example of designing a PDP-8 compatible CPU). I found the book very helpful in understanding how an arbitrary sequential algorithm can be constructed from logic gates: the key for me was thinking of the algorithm in terms of an algorithmic state machine, which is then fairly straightforward to convert to logic.