views:

249

answers:

4

I was wondering if there's some special way to compile high-level code (preferably from c/c++ or java) to get the corresponding assembly code.

+1  A: 

assuming you are using gcc, http://www.delorie.com/djgpp/v2faq/faq8%5F20.html tells you to gcc -O2 -S -c foo.c

look at the manual/doco for your compiler - i m sure there is an option to do it.

Chii
+3  A: 

gcc can dump an assembly listing using -S switch - it will emit the assembly code to a file with a .s extension. For example, the following command:

gcc -O2 -S -c foo.c

will leave the generated assembly code on the file foo.s.

If you want to see the C code together with the assembly it was converted to, use a command line like this:

gcc -c -g -Wa,-a,-ad [other GCC options] foo.c > foo.lst

which will output the combined C/assembly listing to the file foo.lst.

Most compilers will support something similar to aid debugging the compiler itself. For Visual C++, see this guide.

Paul Dixon
Do you know if g++ has this function? If so how do you use it? thx
Yes it has! Check man page of g++ to get more details
Aviator
The option for g++ should be exactly the same
Paul Dixon
i was too lazy to read the man page, haha. guess i have to now. thx for the help everyone~~
A: 

Those languages are quite different in that C and C++ are normally compiled to machine code, whereas Java uses a virtual machine. You cannot sensibly compile Java to assembly language (if you want to see the machine code, use a debugger), but with C or C++ it should be quite easy, depending on your compiler. For example, when using gcc, simply give it a -S option and it will produce an assembly code file instead of the object code.

Philipp
There are Java compilers (like [gcj](http://gcc.gnu.org/java/)) that do compile native code rather than JVM bytecode. You could look at the generated assembly for those.
Kristopher Johnson
A: 

Many compilers come with the option of listing the generated assembly code. For example, gcc has the -S option, which will stop the compilation before assembling and leave you with assembly files.

Avi