Friday, 29 November 2013

A deeper insight into “Hello World!” in C

Posted by srini0x00 on 10:52

Motivation:

In the world of Computer Science, probably the very first output that every programmer would see on his computer screen is “Hello World!”.

So, Lets recall the Hello World! program one more time before actually getting into the nitty gritty details of ARM Assembly Language.

Let the game begin

When we compile and run a very simple C Program, all we see is a binary giving us an output.

 Source Code  -> Compiler -> Binary


But, in the process of generating the binary, there will be some intermediate files generated during different phases which we often don't care. 

Sourcecode -> pre processing -> Compiling -> assembling -> linking -> Binary


Lets look at all these Phases with a sample program. I am using an Ubuntu machine with Intel Processor. 
Let’s first write a simple C Program as shown below.
/* Hello World Program in C */
#include<stdio.h>
main()
{
printf("Hello World!\n");
return 0;
}

Compile the above code in a way that it saves all the temporary files in the current directory where we have written our code.

gcc –save-temps helloworld.c –o hello

The above command generates the following files as shown in Figure 1.1
helloworld.i      -preprocessed file
helloworld.s     -assembly file
helloworld.o    -object file
hello                -binary

Figure 1.1

Pre Processing: 

Pre-processing is setting up everything ready for a compiler to do its job (Compiling). 

WikiPedia: A pre-processor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a pre-processed form of the input data, which is often used by some subsequent programs like compilers. 

In our case, the output after preprocessing is helloworld.i. We can see this file by opening it with any text editor or by using the following command.
cat helloworld.i

Compiling: 

Compiling is basically a process where it takes the high level language’s source code as an input and generates an output usually to a lower level language like assembly.

In our case, the output after compiling is helloworld.s. We can again see the code as explained in preprocessing phase.

Assembling: 

The previous step has generated an assembly language code. Here in this step the code generated by the compiler will be assembled and a new output will be generated for a linker to link it to a final executable. 

In our case, the output after compiling is helloworld.o. 

Linking: 

As explained in the previous phase, this is where the linker will link the object files to a final executable.

In our case, the output after compiling is hello which is the final executable. Even if we try to see the code of this file using any text editors, we end up understanding nothing since it is an executable file.

Now, to see the output, we can execute it as shown in figure 1.2

 figure 1.2

Summary

The Programmer’s ultimate goal is, processor should process and execute what the programmer is asking it to do(Printing "Hello World!" onto the screen in our example). Since, our processor cannot understand the programs we are writing in a high-level language like C, we are converting them into a form which our processor can understand.

Hope this information helps you. Please post your comments for any queries or suggestions.

Kindly Bookmark and Share it:

0 comments :

Post a Comment

 

Recent posts

Recent Comments

Bookmark & Share