Assembly Language – Why Convert Instead of Machine Code?

assembly

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.

Best Answer

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.

Related Topic