1

What is the difference between native code, machine code and assembly code?

Hi, I am Korean, so my English can be confusing to you.

I read the contents of the link above and I want to know why C build process includes 'assemble' process.

The below is the C build process that I know.

Source file -> Preprocess -> Compile (in this process, assembly code is made) -> Assemble (in this process assembly code is converted to machine code) -> Link -> Executable files (.exe)

I saw 'Timwi' above the link says " Unmanaged code refers to code written in a programming language such as C or C++, which is compiled directly into machine code."

If C or C++ can be compiled directly into machine code, why 'assemble' process is needed? The purpose of including this 'assemble' process is just for debugging? or has important and practical reasons?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
DoyoHntr
  • 87
  • 5
  • I do want to mention sometimes people use "Assembly code" and "Machine code" interchangeably in the C world, as they often have a 1-to-1 transformation. – Ranoiaetep Mar 30 '21 at 01:41
  • 2
    Assembly step is not required/needed. The compiler author(s) are free to choose how they design their tool including the output of the compiler and steps from C to the final binary. Using assembly is a nice modular approach (the unix way, despite the name gnu is very much unix). But not required/needed. – old_timer Mar 30 '21 at 02:08
  • @old_timer Thank you so much! That's the answer I want! – DoyoHntr Mar 30 '21 at 11:28
  • 1
    for clarity, assembly step is only not required if the compiler chooses to do it internally. early on these phases were done manually and used seperate programs; today compilers tend to have an integrated assembler specific to their needs. In the original quote, he means C has no language runtime; you don't execute the language itself to run your program inside iself. That's how "managed languages" work; they implement a virtual machine for your program to run in, and actively participate in execution. In some sense, C's "virtual machine" is the operating system. – l.k Mar 30 '21 at 13:45

1 Answers1

4

Timwi was simplifying the build process.

The point is that C code is typically not compiled to bytecode for a virtual machine, but to native code for your machine. This is "direct" because there's no further processing to be done at run time to make the program runnable, as would typically be used with languages like C# or Java.

There may be a multi-step process, including compiling, assembling, and linking, to achieve that, but the steps in the process aren't important to the point Timwi was making.

The Photon
  • 1,242
  • 1
  • 9
  • 12
  • 4
    Some compilers (e.g. clang and MSVC, but not GCC) *do* compile *directly* to machine code. Their internal data structures will represent asm instructions, but never in the form of ASCII text like `mov eax, 123` unless you ask them to write asm output instead of an object file. – Peter Cordes Mar 30 '21 at 02:20
  • Thank you for answering! Have a nice day! :) – DoyoHntr Mar 30 '21 at 08:53
  • @PeterCordes, Edited accordingly. – The Photon Mar 30 '21 at 15:57