If assembly language is only a readable way to represent machine code, then why are HLLs converted to assembly first and then to machine code?

Shouldn't HLLs be directly converted to machine code?


This is customary practice on Unix systems because it simplifies the compilers. Compiling is largely not dependent on the gritty details of machine code, and many compilers can all target the same assembler.

Some compilers target an intermediate abstract machine (like JVM) or even an intermediate concrete machine (MIPS) which is not the actual hardware the code will everntually run on.

Some compilers do go directly to machine code. It's not very portable and portability is a big thing now (it didn't used to be back in the stone age of computing.)

  • "it didn't used to be back in the stone age of computing.": Well C was born as a high-level, portable assembly language, i.e. it was closer to the machine architecture than other languages like Pascal but abstract enough to be portable. – Giorgio Jul 29 '12 at 7:31
  • +1 for "simplifies the compilers" - if you have a perfectly good assembler already, why reinvent the wheel by spitting out binary opcodes from your HLL compiler when it's so much simpler to emit assembly code? – Carson63000 Jul 29 '12 at 8:01
  • "on Unix systems" isn't this on almost all systems? At least I haven't seem a modern system/compiler where it wasn't the case – stijn Jul 29 '12 at 8:23
  • 1
    @Giorgio: I disagree with the characterization of C as a "portable assembly language". An assembly language program specifies a particular sequence of machine instructions. A C program specifies run-time behavior. That's a huge semantic difference. – Keith Thompson Jul 29 '12 at 9:14
  • 1
    @Keith Thompson: Maybe "portable assembly" is the wrong formulation, but still C is a very thin layer on top of assembly. At least in the first versions of C, most C constructs could be mapped to assembly in a very straightforward way. E.g. the only allowed function return types were types that would fit into a processor register (the 'accumulator' register was often used for this). – Giorgio Jul 29 '12 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.