views:

232

answers:

6

Possible Duplicate:
How do you get assembler output from C/C++ source in gcc?

Hello out there!

I have a C and C++ source code that I wanted to see in assembly code. How can I produce an equivalent asm codes for those? What tools should I use? Or is it already possible in using tools such as the gcc compiler? Then what commands should I execute? I prefer tools on top of Linux particularly Ubuntu.

Thanks in advance!

+10  A: 
gcc -S x.c

This should produce the assembly that gcc thinks is equivalent to your code. Beware though, the optimizer can do some very tricky things which may be hard to see are functionally equivalent to your code. Especially in c++ where the optimizer is so dependent on inlining and stuff like that.

From the gcc man page:

-S

Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified. By default, the assembler file name for a source file is made by replacing the suffix .c, .i, etc., with .s.

Input files that don't require compilation are ignored.

Evan Teran
Can't you use some option `-O0` where you can turn off most optimizations?
B Johnson
@B Johnson: sure, but even at -O0 gcc does some minor things.
Evan Teran
+3  A: 

For gcc, use the -S switch. You will get files with the .s extension where you can see the assembly code generated.

PeterK
+3  A: 

I'm going to take the median route between having already answered this in one capacity (it's by using gcc -S, assuming you've installed the gcc ubuntu package). But the question's already been asked several times.

Marc Bollinger
Generally, you identify duplicates in the comments to the question, rather than as an answer.
dmckee
A: 

Here is a simple project for converting C to asm

http://www.codeproject.com/KB/cpp/KhanC2ASM.aspx

Here is another one

http://www.hex-rays.com/idapro/

You can find many disassemblers with googling.

Judas Imam
the first is a compiler, not able to understand fully C, nor C++ at all, and not able to create the final binary, so it produces the assembly code; does it emit x86 code? (why it is labelled cross-compiler? cross with respect to what? not clear by the page, likely more in the doc?); the second is a disassembler, which is not able to convert C/C++ source code to anything, but works on executables (disregarding the source code language... it could be fortran, pascal... whatever), but likely the same OP has not a clear idea on his own Q.
ShinTakezou
@Shin: Compilers targeting assembly (rather than targeting object code) are *very* common. And "cross" in this context is "cross platform", which is to say that you can prepare x86 code while running the program on perhaps a MIPS or PowerPC based machine.
dmckee
A: 

The gcc -S produces the source (for the gas assembler) you could be interested in. But remember that expecially for C++, a lot of code you could find interesting is indeed linked standard libraries, run time... whatever according to the language). You can disassemble the final code of course (e.g. simply with objdump), where you can see startup codes and other codes added by linker (but of course, because of the dynamic binding, you won't see the code for, say, the C standard library, since it is not embedded, unless you create your final executable with static linking, when doable, i.e. the "static" version of the lib must exist)

As consequence, what you obtain is not a stand-alone equivalent asm code of what C/C++ sources do. This sort of translation is hard, expecially since asm is a very "basic" language, so that to be able to translate it "1-1" you need a lot of code, and of course the "translation" will depend on the system (e.g. a self-made output routines at the end have to use O.S. provided API/system call)... basically you have to rewrite C/C++ libraries and runtime, when needed (for C++)...

Translating into other language, e.g. from C++ to another OO language, could be simpler, but it is not what you're interested in.

ShinTakezou
A: 

I really appreciate all your answers.

Yup, I found out that gcc -S outputs gas assembler code. But I'm also interested in the equivalent NASM code. What I did was I just used DISASM to extract the equivalent NASM code.

Just another question: Do you guys know how to output the disassembled code produced by DISASM instead of it directing it to stdout? I read the man page but probably I missed it out.

Thanks guys!

neilmarion
try output redirection, i.e. `ndisasm -o100h filename.com > filename.txt`
Gianluca
you said you prefer tools on top of "Linux", and chosen an answer pointing to "windowssian" tools, moreover where the first link is a poor compiler that can't be more interesting than the `gcc -S` stuff... and disassemblers are a different "matter"; anyway, gcc can emit code in the intel syntax too, more suitable to be converted into nasm; the opt is `-masm=intel`; output redirection (the `>`) is a common syntax in many (if not all) command-line interpreters
ShinTakezou
Hmm... I wasn't aware that I have chosen an answer. Sorry for that. I started using stackoverflow just for a couple of days only.
neilmarion
Yup. Disassemblers and assemblers are way different to each other I realized.
neilmarion
This should have been a separate question on its own, particularly considering that the original question here was closed as a duplicate and is likely to be deleted.
Bill the Lizard