Software Engineering 101: how computer works
There is some bit of information about computers which I was never told in school or at the university. This writing is trying to cover these bits in details for those interested out there.
Processor’s guts
In essence, processor consists of these main blocks:
- instruction decoder, which decodes the operation code (see below) and tells processor what it should do next
- ALU (arithmetical-logic unit) - the device, which performs all the operations a processor is capable of - arithmetic (addition, subtraction, multiplication) and logical (bitwise OR, AND, XOR, NOT; comparing numbers)
- registers are the places, where processor takes the input data for each operation and stores the results of each operations
- data bus - the place, which passes data to or from memory (RAM, peripheral devices and other types of memory)
- address bus passes memory addresses between processor and memory (to be read from or written to)
To best illustrate our examples, consider this much simplified processor model:
It has only three registers, ALU and data/address buses. I will explain all the examples below in reference to this processor.
Workflow
Processor works discretely, meaning it executes commands at certain points in time (as opposed to continuous work, where something works all the time). Those points in time are marked by clock signals (or simply, clocks). Processor is connected to (or has an internal one) clock signal generator, which regularly tells processor to operate. Operation is nothing but a change of the internal processor’ state.
So how does processor knows what to do next? On the first clock signal, CPU loads the command with the help of instruction decoder. On the next clock tick, processor starts executing an instruction (or a command) - it either copies data to / from registers or RAM, or involves ALU. On the next clock, processor increases the command counter by one and hence proceeds to the next instruction from the pool.
In reality, this mechanism is somewhat more complex and involves more steps to just even read the command from the pool. And the pool itself is a unit on a processor chip as well and acts as another register. But to explain the processor operation, I’ve simplified that.
Read more to where I explain how a program in Assembly language is transformed to ones and zeroes; show a sample processor instruction set and how a program in C could be compiled to that instruction set.