1 - Theory of Computer Science / High & Low Level Languages
|
|
High and Low Level Languages
Computers represent everything with binary numbers.
Program instructions for example If... Then... Else need to be converted into binary. When this conversion takes place it is known as MACHINE CODE.
When a programmer writes their code they use a language such as Visual basic, python, java etc. This is known as HIGH LEVEL CODE.
High Level Code
High level languages are considered "High level" as they are the closest to human language and very different to machine code (Binary). High level languages contain English words such as If..Then.. Else, While and For which helps to make them easy to understand and work with. Most applications that people use are written in a high level language, examples of these languages include Java, Python, Visual Basic, Small Basic, Ruby...
Example:
Example:
High level code example
The above example uses English words and punctuation which makes it easy for people to understand.
Machine code for the same program may look something like this...
1100111000011101100101101101010100101011100101001101010010101111110100001010111011010110101000110101001001001011101001010
Clearly this is very difficult for anybody to understand.
Another advantage that High level languages have is that they are "Portable Languages". This means that unlike Assembly or machine code they are not dependent on the machine / hardware that they are running on.
Machine code for the same program may look something like this...
1100111000011101100101101101010100101011100101001101010010101111110100001010111011010110101000110101001001001011101001010
Clearly this is very difficult for anybody to understand.
Another advantage that High level languages have is that they are "Portable Languages". This means that unlike Assembly or machine code they are not dependent on the machine / hardware that they are running on.
Machine Code
Machine code is a set of instructions that are executed by the CPU. each instruction will be a very specific task e.g. ADD, LOAD, STORE or SUBTRACT.
Each type of CPU has its own set of machine code instructions. A typical machine code instruction may use 2 bytes (8 bits) and is usually split into three parts - OPCODE,REGISTER and OPERAND.
Example:
Each type of CPU has its own set of machine code instructions. A typical machine code instruction may use 2 bytes (8 bits) and is usually split into three parts - OPCODE,REGISTER and OPERAND.
Example:
Opcode - Represents the machine instruction i.e. what it has to do - ADD, LOAD, STORE or SUBTRACT
Operand - This is either the value to be operated on OR the address that holds the value to be operated on. Register - The small piece of memory in which arithmetic can be carried out The exact size of the opcode, register and operand is determined by the processor. Each machine instruction can occupy more than 2 bytes if the processor allow it to. The more bits that can be used, the larger the amount of opcodes can be stored, or the larger the operand can be. Confused? Think about it like this If the Operand could only use 4 bits, The greatest number that could be operated on is 16. If this was increased to 8 bits, it could represent up to 255. If a register is represented by 2 bits, it means that the processor has access to 4 different registers. This is because in 2 bits you can have 4 possible unique numbers - 11, 00, 10 and 01. If the number of bits was increased to 3, the processor would have access to 8 registers as there are 8 possible combinations of unique numbers with 3 bits. |
Although it is possible to write programs in machine code, it is very tedious and likely to produce a lot of errors. It is due to this that most of the programs written today are coded using a high level language. This high level code is then translated into machine code by ASSEMBLERS, COMPILERS and INTERPRETERS.
Assembly Code
Machine code is often referred to as the "First generation language" where as the "Second generation language" is something called Assembly Code.
Programming in machine code is possible... but very time consuming. The next possible step up is assembly code which uses a set of mnemonics to represent each opcode and a denary number to represent the operand.
Example:
Assumptions - For the following example you must assume that Score1 identifies that the operand is in location 10, Score 2 = location 11, Score 3 = location12 and Total = location 15.
Programming in machine code is possible... but very time consuming. The next possible step up is assembly code which uses a set of mnemonics to represent each opcode and a denary number to represent the operand.
Example:
Assumptions - For the following example you must assume that Score1 identifies that the operand is in location 10, Score 2 = location 11, Score 3 = location12 and Total = location 15.
The comparative code for this in a high level language that you are more used to would look like this:
Total = Score1 + Score2 + Score3
Each type of processor will have a different number of instructions that they can process, however, they cannot deal with statements from high level languages such as, IF.. Then.. Else or For/ While...
To test conditions in assembly code, COMPARE and BRANCH instructions are used.
Total = Score1 + Score2 + Score3
Each type of processor will have a different number of instructions that they can process, however, they cannot deal with statements from high level languages such as, IF.. Then.. Else or For/ While...
To test conditions in assembly code, COMPARE and BRANCH instructions are used.
Assembly Code Pros
|
Assembly Code Cons
|
Example Uses of Assembly Code
Real time applications will use assembly code as speed is key and an instant response is needed, examples include medical equipment (Life support) or network software...
Control programs in embedded computer systems will use assembly code, examples are routers, digital watches, washing machines, Air conditioning...
Device drivers will use assembly code, these are programs that tell a computer how to communicate with hardware and use it correctly.
Control programs in embedded computer systems will use assembly code, examples are routers, digital watches, washing machines, Air conditioning...
Device drivers will use assembly code, these are programs that tell a computer how to communicate with hardware and use it correctly.
Assemblers / Interpreters / Compilers
On some of the previous tabs you will have come across the terms assembler, interpreter and compiler. It was briefly explained that they perform some sort of translation of a higher level code into machine code. This is true but there are subtle differences between them and what they can actually translate.
The diagram below should help you to visualize exactly what is capable of translating what.
The diagram below should help you to visualize exactly what is capable of translating what.
As you can see from above, the Compiler and Interpreter deal only with the translation of High Level code into Machine code. (They never translate assembly language.
The Assembler is the translator which translates Assembly language (It never translates high level code)
The Assembler is the translator which translates Assembly language (It never translates high level code)
Assemblers
An assembler will translate assembly code into machine code. The Assembler only deals with the translation of Assembly code into machine code! It never deals with high level languages (The clue is in the name) The process for this is quite simple and involves each assembly code statement e.g.
Assembly code statement:
LDA2 Store4
could be translated into
Machine code statement:
1101011000011010
Note - As high level languages have many complex statements, they cannot be easily translated into machine code with a assembler. The reason for this is that each high level statement might need a number of machine code statements to match it. Due to this an assembler is not up to the job, you will need to use either a compiler or an interpreter.
Compilers
A compiler will translate a high level program into machine code. The compiler works by translating every line of the program code into machine code before running it. Only once the entire program has been translated can the program run. If an error is detected, the programmer will be informed, however they will not get to have seen any of the program function up until the error. At the end of the compilation process a list of all the errors is provided to the programmer.
The process of compiling the program is quite slow as it has to translate every line before it runs. It is not all bad though, once it has finished compiling the program can run very fast as many times as required.
If a syntax error is detected, the compiler is unable to convert the statement which will prevent the object code from being produced.
Interpreters
Like a compiler, an interpreter will convert a high-level language into machine code.
It is different to a compiler as it will work through the program line by line, if the line that it is on has no syntax errors, it will execute it and move on to the next line.
Once an error is detected, it will display an error message to the programmer and stop.
When to use a compilerIf the program is complete, error free and ready to distribute then a compiler is the best choice of translation method. But why?
|
When To use a InterpreterIf you are development stage of a program then an Interpreter is the best choice of translator. But why?
|