2

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!

Community
  • 1
  • 1
neilmarion
  • 2,372
  • 7
  • 21
  • 36

5 Answers5

12
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
  • 87,561
  • 32
  • 179
  • 238
3

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

PeterK
  • 6,287
  • 5
  • 50
  • 86
3

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.

Community
  • 1
  • 1
Marc Bollinger
  • 3,109
  • 2
  • 27
  • 32
0

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
  • 9,432
  • 1
  • 29
  • 39
-1

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.

onurbaysan
  • 1,248
  • 8
  • 27
  • 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 Jul 01 '10 at 09:03
  • @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 --- ex-moderator kitten Jul 02 '10 at 19:47