105

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.

6
  • 2
    the gcc options that control optimizations gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Optimize-Options.html Commented Apr 23, 2011 at 18:03
  • 3
    The optimized code is the exact code that corresponds to your C file. Looking at the disassembly of the un-optimized code creates the wrong impression. It will make you think you can do better. Commented Apr 23, 2011 at 18:42
  • 1
    Like Hans says - the idea of writing in assembly code is to do it better than the compiler. If you look at the optimized code you will see just how hard that is! Commented Apr 23, 2011 at 22:01
  • related: How to remove "noise" from GCC/clang assembly output? Commented Feb 4, 2019 at 5:30
  • 4
    Why do people always jump in with smarty comments without reading the Q properly. it is very obvious the user wants an output as clear as possible in order to help him. Compilers are WAY cleverer than most coders and anyone who thinks that the optimized code is as helpful to a newbie as the unoptimized code is not thinking it out. What he is doing is a very common way of learning assembler and telling gcc NOT to optimize is a thing. Commented Nov 20, 2020 at 14:32

7 Answers 7

141

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.

Sign up to request clarification or add additional context in comments.

5 Comments

And -O0 is the default (no optimization) if you have not specified any -O flags.
With -O0 you still have optimization: Reduce compilation time and make debugging produce the expected results.
@klm123 Right, but it is a close as you can get which is better than nothing.
If you have a reasonably modern gcc that supports -Og, try it I find that it often gives more readable assembly output than -O0.
@ViktorDahl Thanks for the hint. I ammended the answer.
16

To test without copy elision and see you copy/move constructors/operators in action add "-fno-elide-constructors".

Even with no optimizations (-O0 ), GCC and Clang will still do copy elision, which has the effect of skipping copy/move constructors in some cases. See this question for the details about copy elision.

However, in Clang 3.4 it does trigger a bug (an invalid temporary object without calling constructor), which is fixed in 3.5.

Comments

12

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

Comments

11

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.

Comments

7

You can also control optimisations internally with #pragma GCC push_options

#pragma GCC push_options
/* #pragma GCC optimize ("unroll-loops") */     

.... code here .....

#pragma GCC pop_options

Comments

4

Long time ago, but still needed.

info - https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
list - gcc -Q --help=optimizers test.c | grep enabled
disable as many as you like with:
  gcc **-fno-web** -Q --help=optimizers test.c | grep enabled

Comments

3

You can disable optimizations if you pass -O0 with the gcc command-line.

E.g. to turn a .C file into a .S file call:

gcc -O0 -S test.c

1 Comment

-O0 is redundant as it is the default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.