The Machine Language

All most everyone knows, the computational program that we use to write using various languages like C, C++, Java, Python etc. is just for our understanding. Whereas the machine understands some other language called the machine language. So the question is how does and who transforms our source code (human-written computational programming instructions) to the machine language.

Well, there is a various level of transformation. And it turns out there are different players for handling in this game them.

So I'm here giving a demonstration of C programming language for your understanding.

Program: Hello World




So now you have completed your job of writing the Hello World program. Now, what's the next phase?

The next phase is 'Include' as you have mentioned to include stdio.h which is a header file sitting in your header libraries folder location.



See, here it is. So the next question is who will include these data from the header file to your main program file. That's the job of the preprocessor. Yes, you don't have to worry about it. To see the preprocessed file:
Step 1. Go to the command window.
Step 2. Type cpp file_name.c > file_name.preprocess
Step 3. Now you'll find a new file in the existing path named with  file_name.preprocess.


When you'll open that file, this is how it'll look. And you can find your code at the end of this file.

So, what's next? It's time to compile the code to verify if there is any syntax error. And convert them to a much low-level language called assembly code.

Step 1. Go to the command window again.
Step 2. Open the path where your main program exists.
Step 3. Type gcc -S *.s (This will create an assemble code for all the existing .c file in that path)

So in that path, you'll find another file now name as file_name.s. And this is how it looks:


Great, you don't have any compilation error. Now, this assembly code is a bit harder for us to understand but does machine will understand this. Surprisingly, no. And why? Because the computer can sense only in the form of 0 or 1. And what's that language is and which file contains that? Actually, that's called machine language, and the file which contains that language is known as the object file.

So, how do we get that?
Step 1. Go to the command window again.
Step 2. Open the path where your main program exists.
Step 3. Type gcc -c *.c (This will create an object code for all the existing c file in that path)


And this is how it looks.๐Ÿ˜„๐Ÿ˜„ ๐Ÿ˜„

Great!!! So, are we done?

Oh!!! Almost. We need something else to add here.

What?

Let's say you have many .c files which are depended with each other. Example: you have another file where you have defined your function definition. And the function is called from some separate file. So how our machine will know which file to run first!!!

Doesn't that happen during preprocessing?

No, the preprocessor will only include those files or replace some symbol when you ask him to do so using the preprocessor symbol: '#'.
Example: #include, #define

Then who will do that?

The name of that guy is called linker. It'll link all your object files to a single one called executable machine file or .exe file.

So using this command on your command line screen: gcc *.o -o HelloWorld


And this is how it'll look. Can you think like how come just a 5 line HelloWorld program is creating so many things in this execution file? Well, you think of it.

Now you are ready to execute your program. But if you don't want to go through all these, just type:
gcc file_name.c -o file_name. This will give you your final executable file without creating any other files.

Happy Learning :)

Comments

Popular posts from this blog

Network Packet Processing Simulation

Last Evening : Simon Games