2
votes

Well Going through basic Functioning of computer's instruction /program, I learnt that We write source code in High Level Language.Compilers convert it into low level language (Machine code/object code). I also learnt that Assembler converts assembly language into machine code/object code.
Then I have got following doubts:

  1. From where this assembly language is generated if compilers directly convert high level to low level.

  2. if the conversion process has to go through assembly language i.e

High Level Language ====> Assembly language ====> object code/machine code,
then who converts this high level language to assembly language and what is the use of it?

3
In general, no one. High-level compilers do not convert source code into assembly language. Rather, they convert it into object code/machine code directly. Assembly language is a human-readable version of object/machine code, designed for a programmer. (However, as the other answers have suggested, it is almost always possible to ask a compiler for a high-level language to give you an assembly-code listing of the object/machine code that it generated, and this is where the confusion starts to happen).Cody Gray
@CodyGray: gcc is very widely used, and really does write asm as text output from the compiler and run an assembler on it, as a separate process running a separate executable. (either tmp file or pipe). binutils is even a separate package from gcc itself. So some compilers certainly do compile this way. Clang doesn't, and other less-portable compilers also often go straight to machine code.Peter Cordes
You can view Assembly language as a way of representing machine language. There is no need for a compiler to make a stop at the assembly language station,David Hoelzer

3 Answers

3
votes

This is a very general question (and also a bit difficult to understand, to be honest).

A compiler for a high-level language could convert high level code into assembler and a secondary utility could convert assembler into what you call machine code. A compiler could also produce machine code directly. Either option is valid and it is up to the compiler's designers to determine which is most appropriate.

That said, assembly is one step away from being "machine code", so it is often useful to be able to read it to determine what the compiler has done. Sometimes this will lead to insights that allow one to optimize the high level code; other times, a 1337 programmer might opt to edit the assembly by hand. For this reason, even if a compiler appears to produce machine code directly, it is often the case that it can produce assembly code instead.

See this SO answer for further details.

2
votes

It's just a problem of imprecise terminology and specific implementations.

In the "classical" model, the compiler converts high level code to assembly, the assembler assembles it to machine code that gets stored into object files, which then are linked to generate an executable file.

Normally all these steps are mostly hidden (especially the assembly part) because typically you invoke the compiler through a "compiler driver", which automatically invokes all the parts of this toolchain, although generally there are options to stop the flow at some level to inspect what's going on (stopping at the assembly level to inspect the work of the compiler is interesting enough that there are even several sites dedicated just to that).

Still, this is both quite a high level view, and depending on the language and implementation some steps may be missing or handled differently - for example, you can have the compiler generate straight machine code, or the linker generate assembly/machine code instead of just linking (this happens all the time when you enable link time code generation). So, the schema above is just that - a useful schema to understand the basic flow, it's by no means exhaustive of the possibilities that can be done. As long as high level language enters, some kind of executable code exits, anything goes.

0
votes

Actually compiler never converts high level language into machine level language. This definition is true but only for C language. Because in java file.java for example will be converted into byte code by a compiler which is neither High or Low Level Language but is an intermediate language. Compiler is a program because it converts source code or language into target code or language where source code can be High Level Language but Target code should be lower than Source code because if the level of both become same than it is called a pre-processor.