DEV Community

Ashmita Chakraborty
Ashmita Chakraborty

Posted on

What's your CPU actually doing?

What's a CPU? I see CPU as a big over-growned calculator. Isn't it so? What is a calculator? It is an embedded device. How does it work? Of course we need a processor or controller to do the job for us. This over-growned calculator goes from playing with numbers to sending stuffs over the internet, downloading and installing a new software, playing games, recording music, etc.

Theoretically , "CPU is divided into three parts for its execution to take place. This is a kind of processor that connects to every other peripherals and make it work...,etc. etc."

To get the crux of CPU, we need to consider CPU a Black-Box which consists of a Clock. This clock ticks in a periodic way to form a cycle called "FETCH-DECODE-EXCUTE" cycle. This black-box must contain registers. CPU has registers to keep track of instruction cycle (Program Counter), registers to load instructions from memory (Instruction register) and an accumulator(A). Next thing to visualize an over-growned calculator to have is to keep a placeholder for the instructions and its values, which is RAM - Random Access Memory. When we say Random Access Memory, we mean randomly accessing memory , in other words it doesn't matter in what order or when the information is read or written. The cycle consisting of 'FETCH-DECODE-EXECUTE' steps form the three steps of a processor.

Let's understand with a simple program. Let's say we have the following contents in RAM.
---------------------RAM-------------------------
ADDRESS VALUE
0 LOAD 7
1 ADD 5
2 STORE 7
3 JUMP 1
4 0
5 3
6 0
7 2

Instruction has two components. First part : Instruction itself Second part : Memory address. So, in each clock tick, the processor will FETCH the instruction, DECODE the instruction and EXECUTE the instruction and will continue in a loop. Let's understand the above program step by step.

Clock Ticks ------Process Type ---- Register Type----Value Stored
1 ----------------- FETCH --------- PC set to --------- 0

CPU fetches memory address 0 and puts the instruction LOAD in instruction register
2 ----------------- DECODE --------- IR ------------- LOAD 7
Here the first component is called instruction LOAD and second component is called memory address 7.
3 ----------------- EXECUTE --------- A -------------- 2
Processor executes the instruction. It takes the value at address 7 and loads it into A(accumulator).
========================== 1st cycle =============================
Clock Ticks ------Process Type ---- Register Type----Value Stored
4 -----------------FETCH-------------PC set to-----------1
5 -----------------DECODE------------IR----------------- ADD 5
6----------------- EXECUTE----------- A ---------------2 + (3) = 5
Processor executes(adds) the value at address 5 with the value at Accumulator and store the result in Accumulator register
========================== 2nd cycle =============================
Clock Ticks ------Process Type ---- Register Type----Value Stored
7 -----------------FETCH ------------ PC set to ----------2
8 -----------------DECODE------------ IR --------------STORE 5
9 -----------------EXECUTE------------ A -----------------5
Processor STORE the value in accumulator at address 5.
============================ 3rd cycle ===========================
After the 3rd instruction cycle, the RAM contents looks like :
---------------------RAM-------------------------
ADDRESS VALUE
0 LOAD 7
1 ADD 5
2 STORE 7
3 JUMP 1
4 0
5 5
6 0
7 2

Clock Ticks ------Process Type ---- Register Type----Value Stored
10 ----------------- FETCH---------- PC set to------------ 3
11 ----------------- DECODE----------- IR ------------- JUMP 1
12 ----------------- EXECUTE---------- A ----------------- 5

Processor now executes the instruction present in address 1 which is ADD 5. In the next cycle, the processor will now ADD the value at address 5 with the value at Accumulator and store it in Accumulator. So the value at Accumulator will be 8. This cycle will run in loop. This is the ability of a processor to jump, to loop and to build instructions recursively makes the foundation of an over-growned calculator or COMPUTER. The value at A keeps on incrementing. In this program, there is not HALT command being used.

In the next article, I will show you with more complex examples how the fetch, decode and execute works.

Top comments (0)