How to Turn Source Code Into Machine Code?

by Carson
Source code to machine code

When you run a program, do you know that the computer can’t execute the source code shown on the screen? Instead, it has to be converted into machine code before it can be directly run. Therefore, let’s explore how computers turn source code into machine code in this article.

What Are Compilers and Interpreters?

What are the tools needed to make source code executable? We have two options: Using a compiler or an interpreter. Compilers parse and analyze the code to generate an output file in machine code. Remember that compilers translate a program all at once, and the machine code is the exact executable version of the source code.

How about interpreters? They do the same thing. However, the source code is converted as it runs, meaning that no version of the machine code is stored on disk. That means, when the computer reaches the instruction when executing the program, the interpreter translates it and stores the instructions directly in RAM.

How to decide whether to use a compiler or an interpreter? It depends on the programming language. For example, programming languages like C and C++ require the source code to be compiled in full before it can be run. However, other languages like Python and JavaScript need an interpreter. Therefore, you can run the source code, and the interpreter is responsible for the compilation. Keep in mind that languages that employ compilers are generally faster.

How Is Source Code Translated into Machine Code?

After introducing compilers and interpreters, we will get to the process where source code becomes machine code. Firstly, do you know why programs need to be very well-structured, and the computer will produce an error when you have even a single grammatical mistake? It is because it’s the way a compiler works. It first needs to split the code up into multiple small parts using a process known as tokenization. If any syntax errors are lurking in your program, it will mess up with the tokenization, thus making the compiler produce an error. Also, comments will be separated when parsing the program and will not be included in the machine code.

Then, the compiler analyzes the tokens and starts writing object code (intermediate code), depending on the compiler. They can theoretically write machine code directly, but an intermediate layer of code makes it easier for developers to write the compiler since the analysis part will be identical no matter what the model of the CPU core is. This is because the same piece of machine code doesn’t do the same thing for every CPU model. Instead, each CPU model supports different sets of machine code, so the compiler needs to take care of that.

Finally, the compiler will create a binary file and then write machine code to it based on the intermediate code. The instruction will be written according to the CPU model of the computer. When compilation finishes, the program is ready to run.

An illustration that summarizes the process of compiling programs
Image created using Canva

Interpreters work pretty much the same way, but the source code is compiled line by line, and the instructions are directly stored in RAM instead of being written in a binary file.


We explained how source code is turned into machine code using compilers and interpreters in this article. Remember that these programs are what make computers capable of comprehending and executing human-readable source code, and enable us to write complicated programs.

References and Credits

  1. Vaidehi Joshi. (2017, December 13). A Deeper Inspection Into Compilation And Interpretation. Retrieved December 30, 2021, from
  2. (2020, February 10). C++ Compiler Explained: What is the Compiler and How Do You Use It? Retrieved December 30, 2021, from
  3. Luke I. Wilson. (2018, June 10). Understanding Compilers — For Humans. Retrieved December 30, 2021, from
  4. (n.d.). Compiler – Intermediate Code Generation. Retrieved December 30, 2021, from
  5. (n.d.). The Programming Process. Retrieved December 30, 2021, from
  6. John Smith. (2021, December 17). Compiler Vs. Interpreter: What’s the Difference? Retrieved December 30, 2021, from

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.