Electronic – How does a microprocessor operate in sequence

microcontrollermicroprocessor

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.

Best Answer

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.

Related Topic