"It has often been said that a person does not really understand something until he teaches it to someone else. Actually a person does not really understand something until after teaching it to a computer, i.e., expresses it as an algorithm "
Introduction
A computer is a machine that is made up of at least one input device, one output device and a central processing unit. The duty of the computer is it accepts data from the user, manipulates the data according to the instructions ordered by the user and then output the information. The main device of the computer is the central processing unit. The CPU manipulates data according to instructions known as programs. The CPU manipulates data by reading instructions that are written only in machine language. This is 0 and 1. A typical machine language would look like this,
Instructions that are run by CPU are known as programs, programs are designed by programmers. Computers understand only machine language but humans cannot write programs in machine language, it is impossible to remember the numbers and it is very difficlut to comprehend. To solve this problem, programmers use assembly language to write programs. It is not perfect but it is simpler than machine language, an assembly might look like this,
ADD R6, R2, R6
The main disadvantage of assembly language is that it cannot automatically manipulate the data. You have to tell the processor what data to manipulate, store and remove from the register. For example on the above assembly, the command is to add R2 to R6. The command will tell the processor to put R2, in register, then to put R6 in register, then add R2 and R6. Because we don't want to know how data is manipulated we care for is the results. So programmers designed an easier programming language that hides the details that we don't need to know. The easier programming language is called high level language.
High level language, such as C++ and FORTRAN are easier to write. It hides the details that a user does not need to see. A high level language would look like this,
Answer= 2+6
You don't have to be an expert to understand high-level language because it uses human language. The main disadvantage of high level language is that you have limited control to manipulate the processor, so programmers created another language that is understandable to human and at the same time it enables the programmer to manipulate the processor. This language is called C programming language.
All programming languages serve the same purpose, and that is to write instructions. So where do we write instructions so that we can create programs? Well it depends on the operating system we are using, for windows operating system we use the software "Note pad" to write instructions. When you type in instruction in the notepad, this is called writing a command or codes. As we previously learned that computers understand only machine language, so if you write your codes in machine language the processors will understand it.
Incase you write your codes in programming language other than machine language, you need to convert it. If you write it in assembly language, you have to use an assembler to convert your code machine language and if you are using C programming language or high level language, you have to use a compiler.
After when the compiler or assembler converted your codes into machine language, the codes will be saved in different file called exe (executable file). We can't create any program without a compiler or assembler.
When we buy any software, we simply execute the program. This means that the program is in machine language and it has already been converted to machine language and it's ready to be used. In the next paragraph we will understand what a machine language is.
What is machine language?
We had a brief introduction to machine language in the last page, in the next paragraph we will explain how machine language works and some examples.
We understood earlier that a machine language consists of binary digits that the computer reads and understands. Computers can only understand machine language. As seen the diagram on the right. When we write a program, the CPU cannot understand or see what we write, it only sees 2 binary codes, 1 and 0.
Even thought assembly and machine language are similar they are not the same. Assembly language consists of both numbers and words, but machine language only have 2 numbers, which are zero and one (0, 1).
A combination of zero and one represents a command to the processor, for example a command to "ADD" would look like 1010. A command is also known as instructions. A collection of instruction makes up a program. For example a short program will look like this:-
1001110
1100011
011100
11000110
01100001
Example
The best way to understand how machine language works is to understand by an example. Let say that a torchlight has a processor and main memory, the processor of the torchlight can adjust the light beam high and low, and check the on and off switch. The machine instructions are binary codes and it each codes corresponds a certain command:-
Machine Instruction
Machine Operation
0000 0000
Stop
0011 0111
Turn beam on
0000 0001
Adjust beam high
0000 0010
Adjust beam low
0000 0100
Go back to start of program
0000 1000
Skip next instruction if switch is off
And the memory for the torchlight system:-
Address
Machine Instruction
0
1
2
3
4
The torchlight is wired so that when the switch is turned on, the instructions are executed one by one and by order starting at address zero. The program can be re-started at address zero with the instruction 0000 0100. The torchlight is supposed to turn the beam on while the switch is on, and stop when the switch is off.
A processor of a computer is smililar to this example, only the computers consist of transistors that read on and off. Electricty flows according to the on and off. This on and off is represented by zero and one. The processor will perform a certain execution based on a certain zero and ones. For example 1101110 are to add 2 numbers.
Unless you can write instruction in binary codes, you can run the program directly, but we all now it's almost impossible. So how can we run programs written in human language? As we have mentioned earlier. We have to compile or interpret the written instruction into machine language. In the next paragraph we will be introduced to the two types of language translator
The two most commonly used language translators : an interpreter and a compiler
Programmers start to prepare a program by writing the program instructions at an appropriate language level, and this is done by a text editor, such as notepad in windows operating system. The language used to write these instructions cannot be understood by the computer, because it is either written in high level language or an assembly language. Therefore a translator is needed to convert from human language to machine language.
The two types of language translators we are going to explain are a compiler and an interpreter.
A compiler
A compiler is a computer program that translate human file
(That is instruction written in a language that is not understandable to human) into a form that the computer understand. What it does is it takes the file that the programmer have written, for example if a program write a file as text file called filename.txt, the compiler will translate it into myfile.exe which could be run by the processor. A compiler basically is a gap between human and computer. When compilers were first created, it could only convert symbols into bits (zero and ones). These simple compilers were then used to write improved compiler that can convert other characters than symbols. This is a good advantage for programmers as now they can add more ideas and features to a program. Compilers are also created to translate different types of task, so a specific compiler is developed for a particular task or language. Multistage compilers translate human language and make it close to what a computer understands (pseudcode), while a multiple pass compiler will convert it into executable file, the final stage.
Apart from translating from human language to machine language a compiler also check if your program has errors. It will not translate your program if it finds errors. There are two types of error that could be detected by a compiler. They are syntax error and logic errors. Logic errors are grammatical error and are hard to detect , while syntax error are spelling mistakes and are easy to detect. A newer compiler have been created, it is called cross compiler. A cross compiler is portable, which means, it's if a text file is created by a certain manufacture in a certain computer; it can be compiled in a different computer. Its results thought are not perfect and not the same.
How does a compiler work?
The 4 stages that are performed by a compiler are tokenising, syntax analysis, semantic analysis and translation. The first part is turning the program from characters into token that represent the construction of a program. They are ketwords of the language, that is words like "if","print" or "end", "+", "-".
The tokeniser reads each character on the program and labels them as for example ("print"), and it will not read comments that are written for the purpose of undertsnading and not function. Once this stage is complete, the next stage is to look for error. A sysntax analyser will check of the tokens are in the right other for example for C++ language, the instruction ABC++ is syntactically correct, but the instruction ABC_+: is incorrect. The third stage, semantec analyser, is to check the meaning of the instructions. If it is meaningful to the compiler then it will move to the last stage, the final stage, the translation.
An interpreter
An interpreter is another program translator that translates instructions written in high level language by a programmer, into a machine languge the computer understands. Unlike a compiler an interpreter translates the instructions at the moment the program is run. The programmer writes the program by for example using notepad, and then instructs the interpreter to run the program.
An interpreter , unlike a compiler, it will take the program and translates one line at a time, and runs it, when its done , it will then translates the second line and then runs it and so fore. Examples of an interpreter are BASIC, JavaScript and LISP.
Choosing language translators
Choosing language translators depends on the how you write and run your program. Both have advantages. The main advantages of a compiler is they can spot errors easily, espicially syntax error and the main of interpreters is that you can run the program as soon as you have had it written, interpreters are more portable, which means they can run in many types of computers