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 :).

6 comments:

  1. you might be interested in creating this,
    http://linuxgazette.net/issue77/krishnakumar.html

    I tried but I don't hv a good floppy/drive. I want to see a working version of this 'A' !

    ReplyDelete
  2. Thank you for link Chinmay. It is great idea. Fortunately I have got flopply drive (rather 2 of them :D).
    I have dream to install LFS (Linux From Scratch) and Gentoo before doing such geeky thing.

    Gentoo - http://www.gentoo.org/
    LFS - http://www.linuxfromscratch.org/

    ReplyDelete
  3. A slight correction. GNU C++ compiler executable is named as g++. So the command is

    # g++ -o .S -S name.cpp

    :)

    ReplyDelete
  4. aww I cannot display angular brackets :-D

    its
    g++ -o name.S -S name.cpp

    ReplyDelete
  5. Thank you for correction Yogeshwar.
    I have corrected that line.

    I tried to write simple code in hello.cpp and created a class in it. Then I used following command -
    gcc -S hello.cpp -o hello.asm
    This command works.

    Can you please tell me, why this works? Is it because gcc knows looking at .cpp extension that it has to use g++?

    ReplyDelete
  6. Here is the email reply I got from Yogeshwar ->

    gcc is able to compiler the program but not link it :)
    Try to create the executable of your code using

    gcc -o hello hello.cpp

    you will be greeted with lot of strange error messages ;-). But all error messages are emitted by the linker,
    none by compiler.

    The best proof of this is

    gcc -c -o hello.o hello.cpp compiler the program (remember -c is compile only, do not link option.)

    but if you try to produce executable it will emit an error.

    Now it suggests that there is a common code for converting source code into assembly code and of course there
    is single executable as that is used to converting that assembler code into object code. But gcc and g++ differ in link
    phase. At least thats what all experimentation suggests.

    Also I though if they are using the same code, that code must have been modelled as reusable so (so is like dll)
    so I checked with ldd command (which takes executable as input and tells us which so's it is using)

    And to my delight I found that both executables use same so's.

    [yogeshwar@localhost C++]$ ldd /usr/bin/gcc
    linux-gate.so.1 => (0xb777d000)
    libc.so.6 => /lib/libc.so.6 (0x00888000)
    /lib/ld-linux.so.2 (0x00864000)

    [yogeshwar@localhost C++]$ ldd /usr/bin/g++
    linux-gate.so.1 => (0xb771b000)
    libc.so.6 => /lib/libc.so.6 (0x00888000)
    /lib/ld-linux.so.2 (0x00864000)

    libc.so.6 is normal std C lib and last one is dynamic linker.
    So it might be the case that common code will be located in linux-gate-.so.1
    But name is not explanatory, so cant be sure about that.

    We will try to dig into it more

    The best way is to download source of gcc (GNU compiler collection) and then look at the Makefile
    of it. that will exactly tell us how files are put together to produce reusable libraries and which code
    exactly gcc and g++ share.

    Hope this helps. :)

    -Yogeshwar

    ReplyDelete