When I compile a C program, it is compiled first to assembly code, then assembled into machine code. I'm curious why it doesn't just convert straight to machine code in the first place.

  • 2
    Where did you get this assumption from (which is probably wrong for a lot of compilers)? Please edit your question and give a reference, or make clear that this is just your own guessing? Maybe you are referring to one specific C compiler?
    – Doc Brown
    Apr 19, 2014 at 4:45
  • 1
    Actually, it's likely assembled into IR (Intermediate Representation) first, after general parsing is completed by a front-end. Then it's passed onto a back-end that does the real work. Many compiler suites work this way, it allows a lot of optimization work to be shared between every language the compiler supports. Adding a new language could be as simple as implementing something to parse it. There has even been talk of implementing a design like this in OpenGL to make GLSL compiler implementations (which, at the moment, are re-invented by every hardware vendor) a little bit more consistent. Apr 19, 2014 at 22:04

2 Answers 2


It depends on the compiler and the options you provide to the compiler. These days the most widely used compilers will write out machine language by default, but will generate an assembly listing if you request it. It can be helpful to have the assembly listing because a) sometimes compilers have bugs and you want to check the code it's generated, b) you want to understand how the machine code is affected by the CPU pipeline and cache and most people find it much easier to read assembly than machine code.

These days compilers typically convert your program to a highly abstract representation and allow you to write custom back ends to generate different flavors of machine language or even other high level languages.

  • It's worth adding that since assembly language is basically just giving mnemonics to registers and op codes, and is a pretty straight translation between the two, its not like there is any meaningful difference. It's somewhat like asking "why does a newspaper writer delivers a text file instead of setting up the printing press". Apr 19, 2014 at 5:02
  • Not sure what "most widely used compilers" are gcc for example is by default and always direct to assembly language then the assembler is called. LLVM/Clang only just recently added a direct to machine code beta feature. Curious to know what compilers you were thinking of that are "most widely used"
    – old_timer
    Apr 19, 2014 at 17:00
  • @dweich, I was thinking of LLVM but I didn't appreciate how 'beta' the feature was. I also think most JIT compilers directly emit machine code from byte code but that's probably getting away form the OPs question. Apr 19, 2014 at 17:59
  • Another use of the -S option (yes, I know this is specific to some compilers) is to generate an empty program skeleton and then implement part of your program in assembly.
    – Giorgio
    Dec 18, 2014 at 23:23

It's possible for compilers to generate code which is suitable for use as input to a processor (for direct execution), a linker, an assembler, or some other kind of building tool. Which format is useful often depends upon what other code it will need to be combined with. If a compiler for language X which generates output suitable for one brand of linker, and a compiler for language Y generates output suitable for a different one, it may be hard to use a mixture of code X and Y in the same project. If a compiler for language X, however, generates assembly code instead of a linkable object file, it may be easier to use its output with the tool set associated with language Y. Because assembly-language formats aren't completely standardized some massaging may be necessary, but massaging an assembly-language file may be easier than converting a binary object file.

Your Answer

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

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