Search This Blog

Monday, January 25, 2010

Generating assembly code for C++ file

1. Using visual studio 2005 or 2008 :
  i. Create simple hellow world win32 console application.
 ii. Right click on the file (let's say hello.cpp) in the solution explorer tree.
iii. Select properties.
iv. Expand C/C++ in left side tree of the window raised.
 v. Click on output files under C/C++
vi. On right side of the dialog see entry in table "Assembler output", select option "Assembly with source code (/FAs)"
vii. Select OK to close this dialog.
viii. Use F7 to build the project.

In the Debug folder of your project Hello.asm (or filename.asm) should be generated.

2. Using Visual C++ 6 :
 i. Create simple hello world win32 console application.

 ii. Select the file (let's say hello.cpp) in the solution explorer tree
 iii. Select Project Menu - Settings.
 iv. Select C/C++ tab.
  v. In category select "Listing Files" from drop down.
 vi. In listing file type: drop down select "Assembly with Source Code"
vii. For Listing file name: write path as Debug/HelloWorld.asm -> this step is must unlike new Visual studio.
viii. Select OK.
viii. Use F7 to build the project.

In the Debug folder of your project HelloWorld.asm (or filename.asm) should be generated.



3. Using GCC/ G++:
Very easy compared to visual studio. :)
To generate assembly code for hello.cpp use following command -
g++ -S Hello.cpp -o Hello.S


Note:
The code generated by above options also contains many lines which you might find 'not-relevant'. To read this .asm file is your skill :).

Friday, January 22, 2010

C++ Change value of const int

This is interesting question -

I have following statement in C++ program -
const int a = 22;

and I want to change the value of a to some other value.


////////////////////////////////////////////////////////////////////////////////////////////////

I tried following code -
#include

using namespace std;

int main() {

const int a = 22;

const int *ptr_to_a = &a;
int *ptr;
ptr = (int*)ptr_to_a;

(*ptr) = 5;
cout << style="color: rgb(0, 153, 0);">// Prints 22 => Visual studio 2005 and 2008 debugger shows value of a 5!

const int &b = a;
cout << class="blsp-spelling-error" id="SPELLING_ERROR_12">endl; // Prints 5

int c = a;
cout << class="blsp-spelling-error" id="SPELLING_ERROR_14">endl; // Prints 22

return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////

Looking at the above code. I thought this problem is compiler dependent. But Visual Studio 2005 and 2008 do not give any warning, rather (I confidently dare to say that) this is bug in Visual Studio that it shows value of a 5 after "(*ptr) = 5;" (in debugger), but prints 22!
The problem is probably not compiler dependent because I got same output 22, 5, 22 using GCC (g++) compiler (I tried this on Cywgin - 32 bit Windows XP and 64 Bit Linux).

Probably the simple and logical answer to this question is - you can not change the value of "const int a".

Note I have also tried tricks like -
int *ptr = (int*)&a;
and
int *ptr = const_cast(ptr_to_a);

If you have any solution please post it in comments.