Basic Cpu Organization

When you write a program for your microcontroller you are really writing a program that is executed by the μC CPU (central processing unit), executing various instructions designed into the CPU instruction set, so it's worth a bit of time to take a brief look at the innards of a typical μC CPU.  If the program is written in ASM, the programmer writes the program using these CPU instructions directly.  If the program is written in a high-level language, the compiler translates the HLL lines into corresponding sequences of CPU instructions.  What follows is not in any sense meant to be a comprehensive discussion of CPU architecture design. Rather, it is just enough detail (I hope!) to make the sections that follow understandable.  There's lots of additional detail on the net and in datasheets. 

In the simplest sense, a CPU is that part of the microcontroller that executes instructions.  It does this in a series of steps:

·         Fetch an instruction from the "next instruction" memory location pointer

·         Execute that instruction

·         Advance the "next instruction" pointer accordingly

Every computer program is just a repetitive execution of this sequence.

CPU REGISTERS

Any CPU will have a set of onboard registers, which can be viewed as very fast memory locations.  These registers will either be addressed automatically (Instruction I always operates on register R), or they will be addressed by a few bits in the instruction operation code (op code).  A typical CPU will have the following types of registers: 

Data Registers

These registers act as sources and destinations for arithmetic, logic and shifting instructions. Sometimes these registers are called accumulators. If the data register can also be used for general addressing functions, it becomes a "general-purpose" register, as described below.

Addressing Registers

These registers are used to hold addresses for memory data accessing. Depending on the CPU design, these registers may be full-fledged general purpose registers (the same register can be used for data manipulation or data addressing, or these registers may be limited and designed only for holding addresses used for data accesses.

Stack Pointer

The stack pointer is an address register which points to a section of memory that is used for the CPU hardware stack. The hardware stack is the stack that is used by the hardware for subroutine calls and returns, and for interrupt calls and returns. It is also possible for the user program to use the same stack, pointed to by the stack pointer, for saving and restoring other data. Or, the user program may use another stack, pointed to by another address register. The difference between such software stacks and the hardware stack is that only the hardware stack is invoked by the CPU instructions for subroutine calls and returns, by the interrupt mechanism, and in many cases by instructions for pushing and popping stack data.

Not all CPU designs use a hardware stack pointer.  Some just reserve a single address register, often called a link register, which serves as a one-deep stack on the CPU itself, thus resulting in very quick subroutine calls and returns.  If a deeper stack is needed (almost always the case at some point in a program), user or OS code must copy memory addresses between the link register and a software-maintained stack.

Program Counter (also known as Instruction Pointer)

This is the address register that points to the current (or next) instruction to be executed. It may be accessed by special instructions, or it may be a standard address regiser or even a full general-purpose register.  It will automatically advance to point to the next instruction in a program, and will automatically be adjusted based on program jump, call and return instructions.