Do programming language compilers first translate to assembly or directly to machine code?

前端 未结 13 1991
渐次进展
渐次进展 2020-12-02 09:52

I\'m primarily interested in popular and widely used compilers, such as gcc. But if things are done differently with different compilers, I\'d like to know that, too.

13条回答
  •  执念已碎
    2020-12-02 10:38

    None of the answers clarifies the fact that an ASSEMBLER is the first layer of abstraction between BINARY CODE and MACHINE DEPENDENT SYMBOLIC CODE. A compiler is the second layer of abstraction between MACHINE DEPENDENT SYMBOLIC CODE and MACHINE INDEPENDENT SYMBOLIC CODE.

    If a compiler directly converts code to binary code, by definition, it will be called assembler and not a compiler.

    It is more appropriate to say that a compiler uses INTERMEDIATE CODE which may or may not be assembly language e.g. Java uses byte code as intermediate code and byte code is assembler for java virtual machine (JVM).

    EDIT: You may wonder why an assembler always produces machine dependent code and why a compiler is capable of producing machine independent code. The answer is very simple. An assembler is direct mapping of machine code and therefore assembly language it produces is always machine dependent. On the contrary, we can write more than one versions of a compiler for different machines. So to run our code independently of machine, we must compile same code but on the compiler version written for that machine.

提交回复
热议问题