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 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.
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
- Vaidehi Joshi. (2017, December 13). A Deeper Inspection Into Compilation And Interpretation. Retrieved December 30, 2021, from https://medium.com/basecs/a-deeper-inspection-into-compilation-and-interpretation-d98952ebc842
- (2020, February 10). C++ Compiler Explained: What is the Compiler and How Do You Use It? Retrieved December 30, 2021, from https://www.freecodecamp.org/news/c-compiler-explained-what-is-the-compiler-and-how-do-you-use-it/
- Luke I. Wilson. (2018, June 10). Understanding Compilers — For Humans. Retrieved December 30, 2021, from https://medium.com/@thelukaswils/understanding-compilers-for-humans-ba970e045877
- (n.d.). Compiler – Intermediate Code Generation. Retrieved December 30, 2021, from https://www.tutorialspoint.com/compiler_design/compiler_design_intermediate_code_generations.htm
- (n.d.). The Programming Process. Retrieved December 30, 2021, from http://www2.hawaii.edu/~takebaya/ics111/process_of_programming/process_of_programming.html
- John Smith. (2021, December 17). Compiler Vs. Interpreter: What’s the Difference? Retrieved December 30, 2021, from https://www.guru99.com/difference-compiler-vs-interpreter.html