How does a computer run a program?

Computer is just an overgrown calculator but how does this overgrown calculator is doing simple calculations to playing video games. 

Relationship between SSD/HardDisk, RAM, and CPU

Usually a program is stored in the storage(SSD or HDD or Hard drives) until the program is not opened. When the program is open or in use, it is placed in the RAM where all the current executing applications, programs, etc will be placed. If the program requires any arithmetic operation to do, it will be sent to the CPU (Processor).

RAM (Random Access Memory)

It is where all the instructions are placed. Since there is no order how the instruction is read or written, it is called Random Access Memory.

Let's consider this as the visual model of the simple computer system. 



CPU 

CPU is the heart of the computer, and it connects to all other devices and make it work. CPU contains one or more processing units.  Each unit is called core. A core contains ALU, control unit and registers.  Each core has its set of registers. 

Clock 

It is one of the device, sits on the CPU. With every tick of the clock, CPU goes for fetch, decode, execute cycle. 

Registers

The CPU has 8 general-purpose registers, each capable of storing 32-digit binary numbers. In addition to 32-bit data, they can also store 16- or 8-bit data. Most of the registers have some or all of the following registers:

  • Program Counter  (Must Read *)
  • Memory Address Register (MAR)
  • Memory Data Register (MDR)
  • Current Instruction Register (CIR)   (Must Read *)
  • Accumulator (ACC)   (Must Read *)
These registers are bits of fast storage where the CPU holds value that it's working on right now. There are 16 general purpose registers in the x86-64 architecture.

Program Counter

The program counter always holds the address of next instruction to be executed. 

The initial value in the Program Counter is 0000. 

The PC is reset to 0 when the computer is restarted. 

Program counter increments its value by 1 during the fetching cycle or increments to other value during the execution cycle according to the current instruction. 

In other words, PC keeps track of instruction cycle. 

Instruction Register 

It loads the instruction from the memory(RAM)

Accumulator

<to be updated>

ALU 

ALU performs arithmetic and logical operations on data and can operate on data only those are in its registers. 

BUS 

A bus is a high-speed internal connection. Buses are used to send control signals and data between the processor and other components.

Three types of bus are used.

Address bus - carries memory addresses from the processor to other components such as primary storage and input/output devices. The address bus is unidirectional.

Data bus - carries the data between the processor and other components. The data bus is bidirectional.

Control bus - carries control signals from the processor to other components. The control bus also carries the clock's pulses. The control bus is unidirectional.





The computer program is made up of sequence of instructions. Instructions are the assembly code. 

Click here to get the assembly instructions of the program. 

Program broken into Instructions in RAM

A few instructions are longer than the other, so when this is placed in the RAM, it will be broken apart across two memory addresses.


RAM will hold both instructions and data in separate locations as shown in the visual model. Instructions will be stored in Text segments whereas the data will be spread across any segments such as text, data, stack, heap. Click here to read the memory layout of a program.



The above diagram shows what are the instructions and how they are stored in RAM.

Now the program execution starts. 

CPU Execution

Fetching Cycle

  • The first step of the operation is to fetch the instruction from the program memory. Instruction to be fetched from the program memory is indicated by the program Counter. The current counter value is 0000, so it fetches the instruction from program memory 0000. 


This is placed in instruction register, and at the same time program counter is incremented by 1.


Decoding Cycle

In decoding cycle, it ensures that the instruction is complete or not. If it is complete, it move the next cycle 'Execution'. If it doesn't, it will move to the previous cycle 'Fetching'.

CPU knows that instruction is incomplete or complete based on the opcode of the instructions. Example, if the opcode is 0x02 (which represent LJMP), the complete instruction is 3 bytes. If the opcode is 0x80 (SJMP) the complete instruction is 2 bytes.

The CPU goes into decoding cycle. CPU is not able to execute this incomplete instruction as only half of the instruction is fetched. So, it again goes into fetching cycle. 

It fetches the instruction from program memory indicated by the program counter. 


Once it is fetched, the program counter is incremented by 1. 


Now the CPU goes into decoding cycle. As the instruction in the instruction cycle is complete, the CPU starts execute the instruction. 


Execution Cycle

After executing the instruction, CPU is ready to fetch the next instruction which is instruction 2.


Consider the instruction 1 (2/2) is "LOAD A1 to R1". Once it is placed in the RAM,  system sees the PC and starts executing the instruction at 0001. Processor/CPU sends the command by control bus and address by address bus during the execution cycle.





The system is instructed to load the data at address 1(A1). 


The data bus carries the data in address A1 from RAM to CPU and ends up in Register 1.




It goes into all the fetching, decoding, and execution cycle again for instruction 2.



Now instruction 2 is in execution cycle.  Consider the instruction 2 is 'LOAD A2 to R2'




Assume the instruction 2 wants to Jump to instruction 5 at memory location 0008.

While executing the instruction, CPU changes the program counter to 0008. 


After the execution of instruction 2, CPU fetches instruction 5. 
The instruction is complete, so the CPU executes the instruction. 

Consider the instruction 5 is 'R1 + R2 = R3'. ALU performs the arithmetic operation and stores the value in R3. 







After this, consider we have further more instructions in the RAM. The instruction is 'Store R3 to A3'.


As the data bus can send data bidirectionally, it will store the data in RAM A3. 


<to be updated>


You could inspect the assembly and syscalls the program makes. If you are compiling with g++ main.c to generate an a.out binary, run the following commands after compilation:

  1. To get the assembly: objdump -S a.out

  2. To get the syscalls: strace ./a.out


Reference 

Workings of Program Counter

Program Counter Basics - YTube

Fetch-Execute Cycle

Reading a program -Utube

How computer understands the program -UTube

EChalk Tutorial

CPU registers - SO link

Bus, Control Unit

YOutube 

All the pictures are taken from the UTube videos linked here. 

Comments