find and solve || findandsolve.com
Please wait.....

Process of converting source code to executable code


The process of converting source code to executable code is called compilation. When you compile source code, a compiler translates the source code into machine code that can be executed by a computer.

The process of compilation typically consists of the following steps:
  • Preprocessing: The preprocessor scans the source code and processes any preprocessor directives (such as #include or #define). This typically involves inserting the contents of included files into the source code and replacing macros with their expanded definitions.
  • Tokenization: The compiler breaks the preprocessed source code into small units called tokens, which can be identifiers (such as variables or functions), keywords, constants, or operators.
  • Parsing: The compiler uses the tokens to build a syntax tree, which represents the structure of the source code. This process is called parsing.
  • Semantic analysis: The compiler checks the syntax tree for semantic errors, such as undeclared variables or type mismatches.
  • Code generation: The compiler generates machine code (also called object code) from the syntax tree. This machine code is typically stored in a file called an object file.
  • Linking: If the source code consists of multiple object files, they are combined (linked) into a single executable file by the linker. The linker also resolves any external references (such as calls to functions in other libraries) by linking in the necessary code from other object files or libraries.

As stated earlier, programmers write programs by using a source language, which can be either a high-level language or an assembly language; however, a computer cannot understand both these types of programming languages. Therefore, programs written in these types of languages need to be first converted into the machine language code. To convert the source code of a program into machine language code, a language converter is required.

Let's try to understand the need of language converter with the help of an example. If a German and a Russian talk to each other in their own languages, it  would not make any sense to any of them. The German will not be able to understand a single word spoken by the Russian. Similarly, the Russian will also not be understand a single word spoken by the German. Therefore, they will need a third person who can translate everything spoken in German into Russian and everything spoken in Russian into German.

Now, let's compare this situation with our problem relating to source language and machine language. When a program is written in a high-level language or the assembly, a computer cannot understand it. The computer would understand a program  written in machine language only. Therefore, similar to the Russian and German persons, a program written in the high-level language or assembly language needs another program that can convert the source code of the program into machine language code.

Let's now understand how the conversion of source code from a high-level language to the machine language and the assembly language to the machine language takes place.


 High-Level Language to Machine Language Conversion

As stated earlier, programs written by using a high-level language are not understood by a computer.Therefore, such programs need to be converted into the machine language code so that a computer can understand them. Conversion of code from high-level language to machine language is done with the help of the following two language converters:

Compiler: Converts a complete program from a high-level language to machine language. If an error is caught by the compiler in the program during conversion, then the user. On the other hand, if the compiler does not find any error in the program, them the compiler executes the program.

Interpreter: Converts the code written in a high-level language into machine language code, line by line. If the interpreter finds an error in the first line of code during conversion, it displays the error to the user. On the other hand, if the interpreter does not find any error  in the first line of code, then the second line of the code is checked. The same process is followed for the remaining lines of the code as well. Since an interpreter interprets the programs code line by line: therefore, it takes more time to convert a program than the compiler.
 
The process of converting the program code from a high-level language to the machine language is shown in Fig.IP-1.1:




Assembly Language to Machine Language Conversion

The conversion of assembly language code into machine language code is done through another assembly language into machine instrucions, one by one. The conversion of code from the assembly language to the machine language is shown in Fig.IP-1.2:


You have learned how the program written in the source language is converted into the machine language code. The next section explains the various stages of program development cycle.




Related information

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If findandsolve.com felt valuable to you, feel free to share it.

Comments



Report Response