1

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.

user176121
  • 710
  • 2
  • 11
  • 22
  • possible duplicate of [Using GCC to produce readable assembly?](http://stackoverflow.com/questions/1289881/using-gcc-to-produce-readable-assembly) – mmmmmm Jan 06 '12 at 12:07

4 Answers4

4

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
  • 295,876
  • 54
  • 310
  • 348
1

assuming you are using gcc, http://www.delorie.com/djgpp/v2faq/faq8_20.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
  • 14,540
  • 3
  • 37
  • 44
0

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
  • 48,066
  • 12
  • 84
  • 109
  • 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 Apr 27 '10 at 23:40
0

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
  • 19,934
  • 4
  • 57
  • 70