Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly language output in a C++ compiler

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?

like image 894
paseena Avatar asked Feb 24 '23 18:02

paseena


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).

like image 128
tstenner Avatar answered Mar 05 '23 08:03

tstenner


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.

like image 28
Hans Passant Avatar answered Mar 05 '23 06:03

Hans Passant