I was wondering how to use GCC on my C source file to dump a mnemonic version of the machine code so I could see what my code was being compiled into. You can do this with Java but I haven't been able to find a way with GCC.
I am trying to re-write a C method in assembly and seeing how GCC does it would be a big help.
I haven't given a shot to gcc, but in case of g++. The command below works for me. -g for debug build and -Wa,-adhln is passed to assembler for listing with source code
g++ -g -Wa,-adhln src.cpp
godbolt is a very useful tool, they list only has C++ compilers but you can use
-x c
flag in order to get it treat the code as C. It will then generate an assembly listing for your code side by side and you can use theColourise
option to generate colored bars to visually indicate which source code maps to the generated assembly. For example the following code:using the following command line:
and
Colourise
would generate the following:Use the -S (note: capital S) switch to GCC, and it will emit the assembly code to a file with a .s extension. For example, the following command:
gcc -O2 -S -c foo.c