In the last tutorial — compiling, we learned how the compiler translates the expanded source code to assembly code, and how to create a file containing compiled code. Now, in this tutorial, our focus will be on understanding the role of the assembler in the process of translating source code into executable. So, let’s get into it.
Assembling the Assembly Code
Review the diagram again:
The program execution process
The process of translating the assembly code to object code is called assembling, and the job is done by the software program called an assembler. Like the compiler, the assembler is also a translation program but it can only take assembly code as the input (as it can only interpret assembly code) and produce object code as the output. The object code is the machine code which your computer can understand.
The object code contains machine instructions but it is not ready for execution. This is because there is a high possibility that in our program we may have used pre-defined functions from standard C library. References to those must be resolved before execution. This is done in the next stage which we will learn in the upcoming tutorial.
Viewing the Object Code
In order to generate the object file (which has the .o extension) from the assembly code file, which we generated in the last tutorial, we need to type the following command:
gcc -C add.s -o add.o
-C: the flag that stops the process after assembling and helps in generating the object file.
-o: generates the output file add.o.
Remember to execute the command in the current folder where your program is located.
After executing the above command, you will observe a new file with the .o extension. If you open this file, you will see some cryptic text which is almost impossible to read. This is because the file contains machine code which is meant to be interpreted by your computer, not you.
Up Next
Up to now, we have only translated the source code to the object code. In the next tutorial, we will understand how to bring different object files together and link them to make our code work. See you there 😁
Leave a comment