Knowledge: How do computers understand your code? What really happens after you run your code?
So programmers you certainly know how to write a program to calculate the factor of a number or check if a string is a palindrome or not or even implement the Dicer shortest path algorithm by reading code but do you know-
How your program is actually understood by a computer? Ever thought of it?
The most basic thing which you already know computers understand only binary data
but what does that mean it means that computers can only deal with zeros and ones no other things but have you ever wondered why?
Let me tell you your computers are made of circuits these circuits are nothing but a sequence of gates like and gate, or gate so by gate you definitely heard these names right?
So all these gates are actually made of transistors now
A transistor is what we can call a fundamental building block of a computer just like how a cell is a fundamental unit of life so in the simplest words-
A transistor is what makes a computer work
but what exactly is this transistor?
It is a component that can basically hold two states the first state is on which we represent as one and the second state is off which we reversed as zero now it must make sense why computers can only deal with zeros and ones right now
Let's come into the big picture how we say that a program like -
How does that happen?
- Well this conversion from source code to machine instructions is made possible by a set of magical tools together known as the compilation system to know more about how this conversion works let's actually take a step-by-step approach on how a program is written compiled and finally executed
- Let's consider the above simple C program which basically adds two numbers and prints the sum of these numbers first we save this program with a dot C extension let's say we save it as hello dot C the next step is the compilation we can use the GCC compile which is open source to compile this C program you open your terminal or command prompt and then type in GCC space hello dot C - o and then hello this will create an executable file named as hello which we can finally execute and when we execute it the computer actually execute the file and prints the output.
- This means that at this stage the computer understood our code and it actually computed what is written inside the program so basically it added the two numbers and printed the output now.
Let's break down what just happens initially when you save the hello dot C file on your computer it is actually saved as a sequence of bytes the program that you have written basically contains English alphabets special characters and numbers that's it.
Now in order to convert these individual tokens or characters into binary-
We first convert each character into a decimal format and go on to convert these decimal values into their binary format so most computers follow the ASCII standard to do the same
So basically, each character has its own corresponding numeric value for example the character hash(#) has the ASCII value 35 and the character small.I has the ASCII value 1.5 and similarly, all the characters have their own numeric value once the characters are converted into their ASCII values they are then converted into binary format,
Each character is represented using eight bits or in other words one byte now that everything is in binary this sequence of bytes can finally be saved on your computer's file system.
The next step is where you compile your program with GCC now this is the big step because in this step your source code is converted into the executable machine instructions there are
- In the first step, your program is fed to the preprocessor which basically checks all the header files in your program and adds the code of all these header files into your program, and then saves this as an intermediate file named hello with a dot I extension so it's basically hello dot I for our program since we are using the STD i/o dot H header our preprocessor adds the code of stdio.h into our program this hello.I file is fed to the compiler which converts this source code into an assembly code and then
- saves it as hello dot i the assembly language is just a human-readable format of the machine code this step is actually optional because generating the assembly code from a source code is not necessary it is only generated because the programmer will be able to read and understand this assembly code and get to know what the processor will execute from a low-level this assembly code is then passed to the assembler this is a step where the assembly code is converted by the assembler into the machine instructions, in other words, the binary form
- It is then saved as hello dot o, at last, comes the linking phase in this phase a program called the linker checks if there are any dependencies in your code in our code since we are using the printf function which is defined in the standard c library the printf dot o file which contains the hole for that printf function is actually imported and linked with our hello dot o file and this finally creates our executable file hello which we can finally execute or our command prompt or terminal.
0 Comments