Obtain Assembly source code of C++ in Code::blocks/Mingw.


Obtain Assembly source code of C++ from Code::blocks/Mingw.
We cannot obtain an assembly code of C++ 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 code::blocks and 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 for 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.


 

1. Adding the ‘bin‘ directory to ‘environment variables‘.

 

*For Windows

 
i) Go to computer->(click right button)properties -> (Left column)Advanced system settings -> Environment Variables -> (Under ‘system variables’ find) Path and add the bin directory of the MinGW example-“C:\Program Files (x86)\CodeBlocks\MinGW\bin” to the Path and restart your computer.
 

*For Unix

 
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 to 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 ‘hello.s‘,assembly source code of hello.cpp.
 
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

g++ hello.cpp

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 “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 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 a functions are overloaded their names appear the same in C++ program but in Assembly source code the names generated for the overloaded functions are different.So the compiler can distinguished between the functions which are overloaded.This concept will be explained in detail in Chapter 4.
 
Note::Assembly language is way faster than C++.Perhaps,I’ll learn Assembly language after C++.