2

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
laura
  • 359
  • 2
  • 6
  • 17
  • 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 - on strike Dec 22 '16 at 06:55
  • 1
    @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 Dec 22 '16 at 07:35
  • 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 Dec 22 '16 at 16:06

3 Answers3

3

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.

Richard
  • 56,349
  • 34
  • 180
  • 251
  • 2
    `gcc -S -fverbose-asm` will actually put comments in the asm, so it's more meaningful than simply disassembling the machine-code output. ICC (Intel's compiler) puts percentage estimates for branch-taken probability on branches. clang's `-fverbose-asm` includes comments showing what SIMD vector shuffles do (e.g. `xmm0 = xmm1[3,2,3,2]`). – Peter Cordes Dec 23 '16 at 02:09
  • @Richard:Thank you , i got your point that it depends upon implementation .There may be the two cases -: 1.Compiler converts HLL to Assembly language and then assembler converts assembly language to LL language (machine code/object code)2.Comipler directly converts HLL into LLL(machine code/object code) ..it would be thankful if you please help me out over these two concept:OBJECT CODE or MACHINE CODE ..When these are generated ?(under which cases ) ? – laura Dec 23 '16 at 07:23
  • 1
    @laura: The format of this site is to ask, whenever possible, only one question at a time. If an answer to that question does not already exist here, you should ask it as a new question. – Richard Dec 23 '16 at 19:39
  • I believe in your 2nd paragraph you're using assembler twice instead of assembly. Right? assembly = language, assembler = "programme" that converts assembly to machine code. – Andre Elrico Aug 21 '18 at 11:18
2

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.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
0

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.

Sakshi
  • 1