1

I would like to see, after compiling some code (for example C), the resulting code in machine language or assembly. How can that be done?

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
CCox
  • 13
  • 4

4 Answers4

3

Many compilers (including gcc and clang) have an -S option that makes them output assembly instead of a binary file.

Alternatively you can view the assembly for an existing binary file using a disassembler. For example objdump from the GNU binutils can show you the assembly for a given binary using the -d (--disassemble) or -D (--disassemble-all) options.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
2

From the GCC documentation (assuming gcc) found at https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#Overall-Options:

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

For example, gcc -S hello.c would output an assembly file named hello.s.

jdc
  • 680
  • 1
  • 8
  • 11
0

compile your code with the -g flag, and run with gdb

gcc -g main.c
gdb a.exe

and in gdb:

(gdb) layout asm

There is more info in a related post here

Julito Sanchis
  • 1,406
  • 2
  • 10
  • 10
0

If you're compiling your source code: gcc -S file.c

If you don't have the source code, just the binary file: objdump -d exe

yoones
  • 2,394
  • 1
  • 16
  • 20