How a Computer Compiles and Executes a Program

advertisement
Alan Totten
English 202C
June 15th, 2014
Introduction
Programmers write source code using text editors, to create the instructions for their program.
A computer uses a program called a compiler to compile and execute a program. Compilers are
programs that are able to translate source code into computer machine code. The compiler
runs lexical and syntactical analysis to ensure that the input is valid. The compiler creates then,
creates the machine code which is, read by the computer. After the computer computes the
machine code the end-user has to call and execute the program through the command
terminal or by clicking open the executable file.
Source Code
Figure 1: https://apps.carleton.edu
Displays source code that prints out
“Hello World!” when executed.
Source code is text written by the programmer using a specific programming language. The
text is used as a set of instructions for the computer to follow as shown in Figure1. If the
instructions fail to follow the rules of the computer language then, the source code will not
execute correctly. Source is the input part of the process that creates an executable program
(Spinellis, 2003). Since, machine code is difficult for humans to write we write code using highlevel programming languages (e.g. Java, C++, Python). Humans write source code, which in
turn becomes compiled and turned into machine code. Initially programs were written in the
assembly computer language. Assembly is a language that is able to display machine code in
an understandable way for the human programmer.
Compiler
A compiler is an important program that translates source code written from a text editor
created by the end-user to machine code. Source code programs that are written accurately
but not compiled will not run until it is compiled properly. Compilers consist of three different
dimensions.



Front End
Middle End
Back End
First is the front end. In the front end the compiler performs lexical and syntactical analysis.
Lexical analysis is when the compiler runs through the source code and converts to source code
to binary (1’s and 0’s). The syntactical analysis done by the compiler is to ensure that the code
is grammatically correct and without errors. Each programming language is syntactically
different from another such as, how Spanish is syntactically different from the English
language. The middle end ensures the source code has no unidentified variables or wasted
memory. The middle end also, optimizes the machine code to make it execute faster. The
computer will not run the program unless it is syntactically accurate. Every upper-level
programming language comes with a compiler that’s specifically designed for that language.
The back end is what finally converts the source code to machine code. Examples of free
downloadable compilers include Visual Studio for Microsoft users and XCode for Mac OS X
users.
Machine Code
Machine code is one of the lowest-level programming languages that are seen in binary.
Machine code is extremely difficult to read but is a computer’s primary language. Since
machine languages are the only languages understood by computers it is imperative that any
high level source code is compiled and translated to machine code in order for the computer to
execute/run the program. Each processor has its own machine language. After the machine
code is compiled it is stored as an executable file until an end-user notifies the computer’s
operating system (OS) to run the program. Humans rarely write or read in machine code.
Machine code is extremely difficult to translate and understand.
Calling & Executing a Program
An executable file allows the computer to run a particular program based off of the instructions
set forth in the program. The source code gets compiled by the compiler and is turned into
machine code that is used as instructions for the central processing unit (CPU) of the
computer. In order to call and execute the file it must be compiled accurately and saved to the
computer. The end-user then, utilizes the command terminal to perform a system call to “call”
upon the particular file so it can be executed immediately. Or, the end-user must simply click
on the executable file to open it and have the OS run the program. If, the source code that the
programmer had in mind is written precisely without errors and the compiler compiles the
code without issue then the program will run smoothly. On the other hand, the program may
not run at all or may not perform the desired result that the programmer had in mind. Figure 2
below shows the process the computer goes through in order to execute a program.
Figure 2:
http://brasil.cel.agh.edu.pl/~11sustrojny/en/files/2011/05/ANGkomp.jpg
Conclusion
There are several steps involved in the process of compiling and executing a program. First, the
programmer writes the source code using a particular programming language. Next is the
source code is compiled by a compiler to check for errors, optimized, and turned into machine
code for the computer to understand. Finally, after the file is properly compiled it can be
executed in the command terminal by the end-user or double-clicked on the open up and run
the program.
References:
http://cplus.about.com/od/introductiontoprogramming/p/compiler.htm
http://whatis.techtarget.com/definition/machine-code-machine-language
http://whatis.techtarget.com/definition/compiler
http://en.wikipedia.org/wiki/Computer_program
Spinellis, D: Code Reading: The Open Source Perspective. Addison-Wesley
Professional, 2003. ISBN 0-201-79940-5
http://searchsecurity.techtarget.com/definition/executable
http://www.linfo.org/machine_code.html
http://howto.wired.com/wiki/Compile_Software_from_Source_Code
http://brasil.cel.agh.edu.pl/~11sustrojny/en/files/2011/05/ANGkomp.jpg
https://apps.carleton.edu/global_stock/photostock/68601.jpg
Download