What Happens Inside the Processor When You Run a Program

What Happens Inside the Processor When You Run a Program

A simple and clear explanation of how the CPU fetches and executes instructions stored in memory.

Hello my friends! In this blog you will get an overview of what a program is and how it is executed inside the processor in terms of computer architecture and organization. If you are CS student/grad then you must have studied the subject, or will study.

Some terms you should be aware of

Before understanding the flow of program execution inside the CPU, you will need some information on certain terminologies and parts of processor. Let's look at them one by one.

  1. Instructions: Instruction are explicit commands that govern the transfer of information within the processor(CPU) or between the processor and the I/O devices and specifies the type of operation to be performed(arithmetic or logical).

  2. Program: Program is a list or collection of instructions that tells the processor how the instructions should be run in order to perform a task, like adding or subtracting two numbers.

  3. Registers: They are high speed storage devices in-fact they are the fastest storage unit in a computer and is integrated on the same IC chip as the processor. A register can store one word(A collection of 'n' bits is called a word) of data.

  4. PC: PC or Program Counter is a special purpose register which stores the memory address of the next instruction that is to be executed.

  5. MAR: MAR of Memory Address Register, it holds the memory address of the location from where data is to be Read or Written.

  6. MDR: MDR or Memory Data Register contains the data that has to be Read or Written out of the addressed location.

  7. IR: IR or Instruction Register is a special purpose register just like PC, it holds the instruction that is currently being executed.

  8. ALU: ALU(Arithmetic and Logical Unit) is that part of the CPU which carries out arithmetic and logical operations, like add, subtract, AND, OR, etc. on the operands provided to it.

  9. Control Unit: Control Unit coordinates the working of memory, ALU and I/O Units.

Now we are good to go to understand the typical operating steps of execution of a program inside the CPU.

Execution of the program

The program is written using input devices and is stored in the memory. Execution of the program starts as soon as the PC is set to point to the first instruction in the program. The contents of the PC (memory address of the first instruction) are transferred to MAR and it sends a Read control signal to the memory.

There is some time required to access the memory, once that time has elapsed, the addressed word, or the instruction that was fetched by MAR using the address provided by the PC is loaded into MDR.

Now the contents of MDR are transferred to IR. The instruction is now decoded and is checked whether it involves an operation that requires ALU, if it is so then it is necessary to obtain the required operands.

Operands can reside in the memory or in the general purpose registers, if the operands are in the GPR then they can be directly accessed by the ALU. If they are in the memory, it has to fetched by sending its address to MAR and initializing a Read Cycle.

When the operand is fetched into the MDR, it is transferred into the ALU, after one or more operands are fetched in the same way, the ALU now can perform the desired operation on the operands, then the result is sent back to the MDR.

The address of the location where the result has to stored is sent to MAR, and a Write signal is initiated and the data in the MDR is written in the specified memory location.

At some point during the execution of the instruction the value of the PC are incremented so that the PC points to the next instruction that has to be executed. Thus as soon as the current instruction execution is completed a new instruction fetch may be started.

This is how a program is executed in a processor. It was just and overview and you can learn about it in depth when you study computer organization.

Tata👋

Like the blog if you do and you can follow me on Twitter as well I will love to interact and discuss tech with you all there. I will see you in the next blog. Happy coding!!