Compiling

In this tutorial, we will understand the complete process of compilation of C program.


In the last tutorial, we gained knowledge about the preprocessor’s role in converting source code into the expanded source code. Additionally, we discovered a straightforward command that enables us to access the file containing the expanded source code, which is usually concealed from view.

Once again consider the following diagram

The program execution process

Based on the diagram shown above, we can observe that the expanded source code needs to be given to the compiler, which then produces assembly code as a result. However, it’s important to understand how exactly the compiler accomplishes this task and how we can visually inspect the generated assembly code. These aspects will be covered and explained in detail in this tutorial.


What is C Compiler and what it does?

In general, a compiler converts one type of code into another. Specifically, the C compiler translates expanded source code into assembly code, which can then be used as input for the assembler.

Code translation is a crucial part of software development because the code that makes sense to humans appears as nonsense to computers. Computers can only comprehend binary language, also known as the language of 0s and 1s. Therefore, translation plays a vital role.

The C compiler acts as a translator. Its main task is to understand and convert the expanded source code provided by the preprocessor into assembly code. It’s important to note that assembly code isn’t the same as machine code. Instead, it consists of low-level instructions such as storing a specific value in a register (memory) or retrieving the value stored in another register, among other operations.

Note:  The process of how the compiler performs the translation is quite complex and beyond the scope of this tutorial and course. It involves a lot of intricate details that are extensively covered in dedicated courses specifically focused on compilers and their operations, spanning an entire semester.

The compiler also verifies if the code is correct. For example, if you forgot to include a semicolon at the end of the printf() function, the compiler will display an error message and prompt you to make the necessary modifications for successful compilation. This is because it is not allowed to break the rules of the C programming language. One of these rules is to include a semicolon after each statement in the program.

To view the assembly code within our folder, we can make use of the following command:

gcc -S add.i -o add.s

In the above command:

  1. -S is a flag that stops the assembler from taking the assembly code.
  2. add.s is the name of the file where assembly code is stored.

If the source code is error-free, we will notice a new file named add.s in the C programs folder. By opening this file in Code::Blocks, we can observe a series of assembly language instructions.



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.