1

What is the advantage of C compiling to assembly instead of binary as all I can think of are disadvantages such as making compilation slower?

  • 6
    C doesn't compile to assembler, necessarily. The Unix compilers have traditionally done so out of principles of modularity, and the [Unix philosophy](https://en.wikipedia.org/wiki/Unix_philosophy) of tools doing one job. But I believe there are plenty of C compilers which do compile directly to machine code. – Steve Summit Aug 05 '19 at 00:55
  • So compiling to assembly is found in some compilers but not others? – cprogrammer100 Aug 05 '19 at 00:58
  • 3
    Yes. MSVC doesn't compile to assembler. And while it's (sort of) able to produce an assembler listing, that assembly can't actually be assembled anymore. – David Wohlferd Aug 05 '19 at 00:59
  • The answers on [What do C and Assembler actually compile to?](//stackoverflow.com/q/2135788) explain some about why compilers are designed this way. – Peter Cordes Aug 05 '19 at 04:45
  • Also related: [Why do we even need assembler when we have compiler?](//stackoverflow.com/q/51780158) somewhat asks/answers why don't compilers always just go straight to machine code. My answer there is pretty close to what I would post as an answer to this question. – Peter Cordes Aug 05 '19 at 05:04
  • the sane way to go is to assembly first. first off you have an assembler before you have a compiler, and you have a linker to round out the tool "chain". second it is far easier to debug the compiler output for the developers of the tool allowing for easier and better debugging making a better compiler product. very few if any solid reasons not to, minimal speed improvment on todays development machines. the major one would be JIT, but look at LLVM to see that not happening. mostly its (straight to machine code) the why did you climb the mountain, because it was there argument. – old_timer Aug 05 '19 at 06:45

1 Answers1

2

This is because it is far easier development wise to target assembly.
* It's easier to read and understand
* Developers are experienced in writing assembly
* As a higher level abstraction it is easier to make the code that generates it portable.

For example it is far easier to let the linker and assembler deal with different object file formats

Bryan
  • 441
  • 2
  • 8
  • All optimization is already done before the compiler emits asm. Assemblers don't optimize (other than choosing the shortest encoding for each instruction). Your third point only makes sense if you mean for humans to read the compiler-generated asm and tweak their C source to be more efficient. If you mean that, say it more clearly. (Although that's not why gcc and traditional Unix compilers work that way. It is something a few people do when optimizing their C / C++ programs, though, e.g. using https://godbolt.org/. But a compiler-generated asm listing like MSVC does works just as well) – Peter Cordes Aug 05 '19 at 03:41
  • The way a compiler prints a Thumb `add` vs. and x86 `add` is not really a reason. Independence from object-file formats is a real reason, though: leave that to assemblers + linkers so compilers only have to know about the calling convention and some stuff like section names (unless the assembler translates `.text` as well.) e.g. GCC can be ported to a new Unix platform without porting binutils, just the C->asm part of the compiler. – Peter Cordes Aug 05 '19 at 03:44
  • 1
    You wouldn't start a GCC port from x86 to ARM by getting anywhere near as far as x86 code-gen before adding some remapping code. (It does help that it's all just string printing, though, like a printf format string that you sub in registers names or other operands.) – Peter Cordes Aug 05 '19 at 03:45