-1

While in most cases its not necessarily, recently I have taken to reading the generated assembly of my C code every so often, when I'm curious as to the kinds of optimizations my compiler is making. Especially for low level code or functions which are called many times.

Examples include:

  • Checking if a change to the C code has any impact on the generated code.
  • Checking if the same expression used twice is optimized into one, or if assigning a variable makes a difference.
  • Check for changes which may bloat-generated code-size.

Now this is not a perfect method by any means, its possible that some difference in the generated output is a red-herring, because of all sorts of internal logic & thresholds within the compiler, but its still interesting at times.

At this point while looking at the assembly I can lookup each command, but not really read the assembly (amd64 as it happens).

So my question is, has anyone found it useful to learn a certain amount of assembly to write better C code? (or at least get more insights into their code).

  • If so, how much is useful to know?
  • What kinds of issues did it help you resolve?

Note, the danger here is that one writes C code targeting the assembly which is very specific to the developers own system - any advantage may be lost on a different compiler or architecture, taking that into account, Im still interested in other developers experience

closed as primarily opinion-based by GlenH7, gnat, david.pfx, Dan Pichelman, World Engineer Aug 3 '14 at 17:03

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • "avoid asking subjective questions where … every answer is equally valid" (help center). meta.programmers.stackexchange.com/questions/6483/… – gnat Aug 3 '14 at 14:41
  • 3
    In my opinion, very valuable indeed. Unfortunately, questions seeking opinions are not. – david.pfx Aug 3 '14 at 14:53
  • Different opinions provide different perspectives about a subject matter. I don't understand why thought provoking questions are discouraged. – rpattabi Aug 3 '14 at 15:46
  • Note, I tried to keep the scope of the question very narrow- rather than asking Should I learn assembly, or similar, in hopes the way this question is phrased leads to a limited set of useful answers (based on real experience). – ideasman42 Aug 3 '14 at 16:49
  • 1
    I write C code for microcontrollers. It's a low level code, which runs pretty close to hardware. Being able to read assembly generated from C code comes handy sometimes. At the same time, I stay away from writing in assembly. Fortunately, I've never had a need to write in assembly. – Nick Alexeev Aug 3 '14 at 23:11
3

Sometimes you need to be able to read some assembly. For example when optimizing code through compiler instrumentation (see for example here). One needs to know what the costs of type casting, data alignment and so on, have for performance.

Also, there are some issues like different function call conventions (pascal vs stdcall) and other system specific issues which are of relevance. Again, it is useful to understand what the compiler does, what the different technologies provide (SSE, AVX,...) and debug some weird problems.

Once you are aware of such problems, you write better code:

  • data type awareness (do I need double/long, or does it suffice with float/int)
  • avoiding unnecessary casts

Usually one avoids such problems by defining its own types and sticking to conventions. You help the compiler do a better job, and it is also easier to understand/maintain.

  • Good point regarding casts, with GCC I manage to catch most with -Wsign-conversion -Wdouble-promotion -Wsign-compare -Wconversion but its still possible casts happen implicitly within function calls. – ideasman42 Aug 3 '14 at 16:47
2

It is not always useful (but sometimes it can help) to read the generated assembly code. Small tip when compiling with GCC, compile with

 g++ -Wall -fverbose-asm -O2 -S somesourcefile.cc

(the -fverbose-asm gives more readable assembly code)

You may also look at intermediate representations. With g++ try passing -fdump-tree-all (with your optimization flags). Beware, it gives hundreds of dump file.

BTw, you might find useful my GCC MELT plugin

At last, be aware that generally speaking compilers optimize better than human programmers do.

Not the answer you're looking for? Browse other questions tagged or ask your own question.