I am trying to learn assembly language. I have searched and found how to disassemble a .c
file but I think it produces some optimized version of the program. Is there any way so that I can see the exact assembly code which corresponds to my C file.
Use the command-line option -O0 (-[capital o][zero]) to disable optimization, and -S to get assembly file. Look here to see more gcc command-line options.
Depending on the target and how GCC was configured, a slightly different set of optimizations may be enabled at each -O level than those listed here. You can invoke GCC with -Q --help=optimizers to find out the exact set of optimizations that are enabled at each level. See Overall Options, for examples. Optimize.
Compiler specific pragma gcc provides pragma GCC as a way to control temporarily the compiler behavior. By using pragma GCC optimize("O0") , the optimization level can be set to zero, which means absolutely no optimize for gcc.
GCC has a range of optimization levels, plus individual options to enable or disable particular optimizations. The overall compiler optimization level is controlled by the command line option -On, where n is the required optimization level, as follows: -O0 . (default).
The gcc option -O
enables different levels of optimization. Use -O0
to disable them and use -S
to output assembly. -O3
is the highest level of optimization.
Starting with gcc 4.8 the optimization level -Og
is available. It enables optimizations that do not interfere with debugging and is the recommended default for the standard edit-compile-debug cycle.
To change the dialect of the assembly to either intel or att use -masm=intel
or -masm=att
.
You can also enable certain optimizations manually with -fname
.
Have a look at the gcc manual for much more.
For gcc you want to omit any -O1 -O2 or -O3 options passed to the compiler or if you already have them you can append the -O0 option to turn it off again. It might also help you to add -g for debug so that you can see the c source and disassembled machine code in your debugger.
See also: http://sourceware.org/gdb/onlinedocs/gdb/Optimized-Code.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With