The CPU of a computer handles all the input from various input devices such
as mice and keyboards and also the output, for the user to see the result
of his input.
Under the hood, the CPU executes billions of instructions per second,
according to its instruction cycle. The instruction cycle, also known as
the fetch-decode-execute cycle is what the CPU follows from boot-up to shutdown.
The cycle can be split in multiple stages. Even though cycle may differ for
different CPUs based on their instruction sets, it is similar to the following stages:
- Fetching: The next instruction is fetched from the memory address stored in the
Program Counter and is stored in the Instruction Register. At the end of fetching,
the Program Counter points to the next instruction.
- Decoding: The decoder interprets the instruction from the Instruction Register
- Executing: The Control Unit of the CPU passes the decoded information to the relevant
function units of the CPU to perform the actions required by the instruction, such as reading values, performing arithmetic or logic functions on them and writing the result back to a register.
The result is stored in memory or sent to an output device.
Receiving input from peripherals is done using interrupts. A CPU interrupt is a signal that causes some out-of-band processing to occur. When that happens, the CPU branches off to do some other function and, when it is finished, it returns to what it was doing right before the interrupt. That way, input can be processed right away by the CPU.