How An Instruction Is Executed In Computer System?

Things were overwhelming when I got to know what is happening inside it. So, I tried to make it less overwhelming for new learners.

Anuj Kumar
8 min readJun 4, 2019
Photo by Skitterphoto from Pexels

Good Evening Everyone, This is my first post on Medium. If I did a few mistakes; let me know in the comments. So, Let’s Dive In.

Whether you are using your computer or mobile phone right now, You are always giving instructions to its hardware to perform your desired needs. For example, if you open any photo you previously clicked in, you are instructing the hardware to load that photo from your storage to memory (RAM) and then display it on your screen.

For the aid of memory, or in case if you don’t know it, A computer is an electronic device that can process or store your data according to the given instructions. Now again, the computer does all of this work in binary format, since it gives better flexibility and precision to the hardware. We give Input to the CPU or the Central Processing Unit, then the instruction is processed (At first it is fetched, then decoded and at last it is executed). Then, we are provided with an output for the previously given input.

Since the computer is made up of digital logic circuits, all inputs and outputs of the computer system are either “0” or “1”. So, every program, instruction, data, etc. is in the form of binary numbers. As a quick FYI, Usually, any developer who builds an application software for any device; they code in high-level languages (Such As C, C++, Java, etc.) since they are more human-like; thereby more easy to understand. But, hardware can not understand it since it is not in binary form. So, the source code of the application software is at first converted into machine-level language. Machine-Level Instructions are very hard to understand directly by us, and we often do mistakes while writing programs in machine language directly. For ease of developer, there are some translating tools you might have previously heard of namely Compiler, Interpreter And Assembler. The source code of the developer’s application is given as an input for such translating programs and it gives the equivalent machine level program as its output so that the hardware can understand it.

(EDIT: From Feedback; I have decided to post on THIS Topic In Upcoming Weeks — How These Programs Translate The Given Source Code In Machine Level Language ? )

Also, A computer always follows a continuous cycle known as Instruction Cycle endlessly until the system has been turned off, i.e., the hardware continues to follow this cycle until the system is no longer in the state to execute those instructions. To understand how instructions are executed in a computer; we will go in more detail about the instruction cycle. But, we need to understand the program and instruction first as a part of prerequisite knowledge.

A Program is a set of instructions that does perform the desired task. On the other side, instructions can be meant as a group of bits that has a specific meaning. Meanwhile, when a program is currently running, it is known as a process. In most cases, instructions of the program are executed sequentially in a running process, while modern systems can execute these instructions concurrently, often in parallel, with the help of parallel processing. Also, the memory can be meant as a byte-addressable array in which the operand/data can be addressed in chunks as small as a single byte. Also, to keep track of things happening inside the hardware, we do use a low-level storage device named register. We do keep an intermediate form of data, instruction, operands, values, everything you can think of inside registers since they are one of the fastest types of data storing medium available in the world. Also, checking/storing intermediate values in memory will take more access time; thereby reducing performance.

We have talked enough to get started with the process of executing a single instruction. So, when a system starts the program, many steps are performed beforehand to execute the first instruction. When the last program was running and was about to end its execution, it had already stored the address of the first instruction of our upcoming program in an internal register named Program Counter (PC). This register stores the address of the next instruction to be executed. The intention of replacing the address for the next instruction is done so that when the last instruction finally executes, the system already knows the instruction to be executed next and thus improving overall performance.

Photo by Pixabay from Pexels

An Instruction is completed in three major stages. They are Fetch, Decode And Execute. These stages are followed in the same order whenever an instruction is given to the machine. As the name suggests, in the fetch stage, the instruction is fetched from the memory by the address stored at the Program Counter. So, when hardware reaches the address in the main memory where instruction is residing, it simply copies that instruction and the instruction is stored in the internal register named Instruction Register. This register stores the currently executing instruction to understand it. Now, The Address residing in Program Counter can be changed to the next instruction so that it points to the next address.

Now, we do have a copy of the currently processing instruction in the Instruction Register. And That’s where next stage kicks in. That is, the decode stage is now active (We do still need to fetch the necessary operands after the decoding of instruction has been completed). Now, all we need to do is decode and execute the instruction. One Stage Is Completed And Two More To Go.

Now, We are at the edge of the decode stage; I need to get you how those instructions were written in the first place. For Example, we are adding two numbers stored at variables a and b and storing the result in the third variable named c. All we need to do in most of the high-level language is to write a similar code as follows:

int a=30, b= 50, c=0;

c=a+b;

The above lines are code snippets from a C/C++ Program, That is what usually developers do at high-level. Here, we are initializing the variables, giving them values and then, at last, storing the sum of the first two. But at low-end, It needs to be converted into binary/machine-level language. For Example, if we use a compiler, it would automatically create a machine-level program. But in low-level, we will initialize three registers for each variable (They are limited in number due to hardware constraints, let me remind you of that). Then, we will write a similar instruction in the intermediate form of machine level (Assembly Language).

ADD C, A, B

A DC F ’30'

B DC F ’50'

C DC F ’0'

Again FYI, DC stands for define constant, and all we are doing is that we are initializing variables A, B, and C. These variables are stored in the working purpose register of the CPU. Then, we are adding A and B and storing the result in C. The machine level code would be similar to this :

10110010100100 [These Are Not Correct Ones … Just Demonstration Of How It Really Looks Like :) ]

Now, In the binary instruction, for the 16-bit processor, the first bit resembles Addressing Mode while the next 3 bits constitute the OP-CODE (Operation Code). Meanwhile, the next 12 bits store the operands or its address. Op-Code specifies the type of instruction like Addition, Subtraction, etc. Op-code For different processor/architecture has different values for the same operation. Addressing mode identifier tells the processor whether the operand is at the given address, or the operand’s address is given in that address; which indirectly gives us the address of the operand.

Now, enough of how instructions are written in the first place. Let’s come back to the decode stage. For ease of understanding, let us take a 16-bit processor in which the first bit is addressing mode identifier and the next three bits define operation code. So, the op-code can be of 3-bits in this example. So, there are 2³ possible combinations possible. They can be one of the following:

000, 001, 010, 011, 100, 101, 110, 111

So, whenever the op-code varies from 000 to 110, the instruction is memory-reference instruction, explicitly defining the operation like addition, increment, etc. (Memory resembles Main Memory) Here, Most Significant bit tell us about the operand whether it is directly accessible from address defined in instruction or the operand’s address is given in that address.

But, in the case, if the op-code is 111, and the most significant bit is 0, then the instruction is register reference instruction, while the operation is specified in few bytes of remaining 12 bits since registers are less in number — they don’t need too many bits. Similarly, If the op-code is 111 and the Most significant bit is 1, then it is Input/Output Reference Instruction. Similarly, an operation is specified in the remaining 12 bits of the instruction in a 16-bit processor.

Back to our example, in which we were adding two numbers and storing it back to a third value. Then, we do get the 3-bits specifying the op-code from instruction present in Instruction Register. Here, for each different operation, the control unit of the CPU gives a different output signal to act properly. The op-code is decoded by the decoder which outputs “HIGH” from respective lines if any respective op-code is present. Just a short example here, if third opcode 010 is Addition, then the third line coming out from 3 x 8 Decoder will be active. Now. the control unit has its internal logic which executes each unique operation. And that ends the decode stage.

The last Stage Is Fairly simple, The Processor understands the operation to be executed by now and all it has to do is to fetch the necessary operands and operate on it. Similarly, it copies the address of operands from the instruction register (It is still storing current instruction and the operands associated with it). Then, the processor put the address of the operand in the address register. Address Register stores the address of any given operand or instruction. The processor fetches the operands from an address at address register in memory and puts in the working/general purpose register. Then, the task is executed is completed successfully. When the task is finished, the processor has completed the last stage. And that’s how instruction is executed by the computer system.

It is very easy to do a task/work from our favourite devices. Scientists and Engineers have evolved the way how we accomplish those tasks by their great minds. I was fascinated by the science behind it which embedded all these things in such a small size. It has evolved from the size of a large hall to the size of our smartphone.

From my side, I tried to make you learn or know these things with the least level of knowledge in the field of computer science. Any Feedback from you would be appreciated. That’s All For Today :)

--

--

Anuj Kumar
Anuj Kumar

Written by Anuj Kumar

Developer & Tech-Geek. I am here at Medium to make computer science simpler to the newbies & pros with easy to read articles. linkedin.com/in/anujk2901/

No responses yet