We cannot obtain an Assembly code of C++ program from code::blocks directly but we can from MinGW(GNU GCC compiler). If you have installed Code::blocks then MinGW has already been installed as it comes with the Code::blocks and it is the compiler of code::blocks.
To check whether MinGW is present or not, go to the code::blocks installed directory and inside it you will find MinGW folder where all the compilers of C, C++, Fortran, etc. is present. If you don’t find it go to this link http://www.mingw.org/ and download the installer and run the .exe file. If you find it follow the steps given below.
1. Adding the ‘bin‘ directory to ‘environment variables‘
i) Go to computer-> and click right mouse button
ii)Find properties -> ( and look at left column) Advanced system settings and click on it.
iii) Find ->Environment Variables
iv)And -> (Under ‘system variables’ find) Path and
V) Add the bin directory of the MinGW example-” C:\Program Files (x86)\CodeBlocks\MinGW\bin” to the Path and
vi)restart your computer.
i)The GCC has already been included so, you can go to the next topic .
2. Getting the assembly code in Windows and Linux.
i) Start the command prompt(in Windows) or Terminal in Linux. You can find your command prompt by going to search and type cmd then ‘Enter’ . Now, change the directory to where your .cpp file is present. For example, my hello.cpp file is present in “C:\MY_PROGRm\hello“, the command is
cd /D C:\MY_PROGRm\hello
/D is not necessary if you are not changing the drive. This means if your hello.cpp is present in C drive, /D is not necessary but if your hello.cpp is present in D or E or any other drive use /D.
ii) Next run the following command
cpp hello.cpp > hello.i
This command will generate an intermediate file ‘hello.i‘ file. An intermediate file is obtained after replacing alias name with the value defined by #define in the .cpp file.It also involve some other processes.
iii) Run the following command to get the assembly code of hello.cpp.
g++ -S hello.i
This will give you ‘
iV) If you want an executable (.exe) file(in Windows) or .out (in Linux) file from command prompt and Terminal without using the Code::blocks, run the following command
This will produce ‘a.exe‘ file(in Windows) and a.out in Linux. Run this .exe file and you will see the console screen with the output “Hello world!“. To run the a.out file in Linux simply type ./a.out in the terminal and press ‘Enter’.
You may wonder why do we even need Assembly language source code While learning C++. Here is a reason! We know that while compiling C++ source code at some point, an assembly source code of the file is obtain (for more information go to compile-time and run-time, stack and heap, static and dynamic ).
Assembly language is the closest language to the machine language, so by looking at the Assembly source code some of the concepts of C++ like overloaded function can be explained. When functions are overloaded their names appear the same in the C++ program, but in Assembly source code the names generated for the overloaded functions are different. So the compiler can distinguish between the functions which are overloaded; the overloaded concept is explained in detail in Chapter 4.
Note::Assembly language is way faster than C++. Perhaps, I’ll give Assembly language tutorial after some time.