Does a C++ compiler generate machine code via assembly language code (i.e., c++ compiler first converts C++ code to assembly language code and then uses assembly language compiler to convert it to machine code), or is assembly language output generation just an option for reference or debugging purposes?
-
3An "assembly language compiler" is called simply an *assembler*. – Ben Voigt Mar 20 '11 at 23:05
2 Answers
It doesn't have to, but most do it anyway, as the same assembler (program) can be used for the output of the C/C++/whatever-to-assembler compiler.
g++ for example generates assembler code first (you can see the generated assembler using the -S
switch).
MSVC does it too (/FAs
).

- 10,080
- 10
- 57
- 92
-
Yup, and that way any optimizations implemented at the assembly language level can benefit multiple languages, and sometimes even code written in a mixture of languages. – Ben Voigt Mar 20 '11 at 23:07
-
@Ben: Assemblers don't optimize much, if anything. About each compiler has some (often source language independent) intermediate language though, which is indeed subject to many optimizations and is sometimes quite similar to machine-agnostic assembly code (e.g. LLVM). – Mar 20 '11 at 23:16
-
thanks for your reply. does someone know if JAVA/.NET JIT compilers also emit an assembly language, or directly the machine code? – paseena Mar 20 '11 at 23:50
-
@quant: they emit an intermediary language. Named IL in .NET, bytecode in Java. Its translated to machine code on the target machine by a just-in-time compiler. Imagine the assembly language of a fictional CPU. You can buy CPU cores that execute Java bytecode directly so it's not *that* fictional. – Hans Passant Mar 21 '11 at 12:50
They used to, a long time ago, although that was typical only for C compilers. The first one I used worked that way, a long time ago. Not unusual for ones that generated code for unusual hardware and operating systems, they saved the cost of writing the object file generator and leveraged existing linkers.
Modern compilers don't bother with the extra step of generating machine code as text and running an assembler afterward, they generate the machine code directly in binary form. The compile speed advantage is fairly significant, text isn't cheap. The option to generate it in textual form from binary is pretty simple to implement.

- 922,412
- 146
- 1,693
- 2,536