26/317
2 - How does a typical microcontroller work?
The ALU is connected to a register that holds the state of the last calculation done, with bits in-
dicating (among other things) whether the result was zero, negative, or overflowed the ca-
pacity of the ALU. It thus provides a means of testing the data and changing the program flow
accordingly. This register is called the status register.
2.3.2 Program Counter
This register holds the address of the next instruction to execute. It is initialized by the reset
generator to a known value, called the entry point of the program. The first instruction of the
program must thus be found at that address in the program memory.
2.3.3 Instruction Decoder
This circuit takes the instruction fetched from the program memory and translates their native
code into its meaning, determining the actions performed by the core. The instructions fall into
the following categories:
Data processing instructions: they give the type of operation to perform (add, subtract, shift,
etc.) and the address of the operand to be processed.
Program flow control instructions: these instructions modify the value of the program
counter, so that the next instruction executed will not be the one that follows the current one
in program memory. They are called jump and call instructions. In particular, some of these
instructions perform their action only if one or more bits of the status register have certain
values, so as to jump only if, for example, the last calculation produced a zero value, or
continue in sequence otherwise. These instructions provide the means of translating the
branching boxes in an algorithm.
2.3.4 Stack Pointer
The stack is a storage area that has the particularity that the data put into it in a certain order,
can only be retrieved in the opposite order. It is the mechanism used to handle temporary pro-
gram flow disruptions, where the main flow of the program is temporarily put aside and re-
sumed later. This is done using a pair of special instructions. The first one, named CALL, first
stores the address of the next instruction to execute into the stack, before jumping to some
other place. The reciprocal instruction, named RETurn, retrieves this address from the stack
and jumps to the corresponding location, thus resuming the program execution.
These features give the system the capability to execute a program that reads data or binary
states from external sources, performs computations, detects particular characteristics in the
data, and reacts a predefined way to this before sending new data out. Using the interrupt me-
hanism, external events can suspend current processing and allow the incoming data to be
processed and then resume the processing that was interrupted.