2GCSE Computing – programming languages Candidates should be able to:explain the difference between high level code and machine codeexplain the need for translators to convert high level code to machine codedescribe the characteristics of an assembler, a compiler and an interpreter
3What is machine code?Machine code instructions are the binary code instructions that the CPU actually executes when an executable program runs.Each machine code instruction performs a very specific task:either an operation on some data (addition/subtraction, input/output);or an operation to decide which instruction executes next (branching or conditional branching based on the results of a previous instruction).Machine code can be regarded as an extremely hardware-dependent programming language because the instructions are often specific to a particular series of CPU designs.
4Can a program be written directly In machine code? Because each instruction is a binary number it is very difficult for humans to read or develop software directly using machine code.Assembly language was developed to solve this problem (followed by increasingly advanced high level programming languages).
5What is assembly language and an assembler? Assembly language is a very simple programming language that uses mnemonics (memory aids) to directly represent machine code instructions.There is therefore nearly between assembly language instructions and machine code instructions.An assembler TRANSLATES assembly language instructions into machine code instructions.Because of the one-to-one match, a machine code program translated from assembly language is very efficient (it will require less memory and run faster than a machine code program translated from a high level language using a compiler).
6Comparing machine code and assembly language Advantages of writing programs in assembly language:It usually creates fast running programs because the one-to-one match means that the machine code program created will tend to be very efficient.The translation into machine code will be very fast due to the one-to-one match between assembly language instructions and machine code instructions.Relatively easy to understand compared to machine code due to the use of mnemonics and labels.Disadvantages of writing programs in assembly language:Different versions of an assembly language are often required for different processors making it difficult to transfer programs between processors.Assembly language programs are often written for specific hardware which means they are often incompatible with different hardware.A lot of assembly code is needed to do relatively simple tasks so complex programs require a lot of assembly instructions and it will take a lot time to write the program.
7How are mnemonics used?The mnemonic instructions are just 2 or 3 letter codes that are far easier to remember than number codes. (for example: LDA = LOAD, STA = STORE)Labels are used to:Label the memory addresses of data so the data can be referred to by name;Label the memory addresses of instructions so the program can branch to an instruction at a named memory location.The simple assembly language program below inputs a number, then in a loop it outputs the count down from that number until it reaches zero.INPLOOP SUB ONE // RAM address label LOOP. Subtract value stored at RAM address ONEOUT // Output the numberBRZ QUIT // If at 0, branch to RAM address QUITBRA LOOP // Not at 0 so branch to RAM address LOOPQUIT HLT // RAM address label QUIT. End the programONE DAT 1 // DATA, store 1 in this RAM address, and label it ONE
8Java C++ What is high level code? High-level code (source code) is made up of statements written in high-level languages such as Visual Basic, C++, Delphi and Java.Such languages allow a programmer to write instructions that can be understood far more easily than machine code.High level source code will make use of some or all of the following:Keywords - reserved words such as SORT, IF, FUNCTION etc. which are simple to understand and would involve a lot of programming using machine code.Syntax - rules for the use of keywords and the arguments that go with them.More complex iteration and conditional programming structures than just simple branchesJavaC++
9Why are translators needed? The CPU can only execute machine code instructions.This means that every executable program, no matter what the language it is written in, has to be translated into machine code.This is carried out by two types of program:a compileran interpreter
10Comparing a compiler and an interpreter A compiler is used once the source code has been fully developed and tested.It translates all the source code into machine code and creates a new file which can be executed by the CPU as a stand-alone program.This translation can involve several stages and may take a considerable amount of time because one source code instruction may translate into hundreds of machine code instructions.Advantages of using a compiler:The source code is not included so compiled code is more secure than interpreted code.It produces an executable file so the program can be run without the source code.Disadvantages of using a compiler:It is a slow process translating the source code into machine code.
11Comparing a compiler and an interpreter An interpreter allows the programmer to run the source code but only within the interpreter.It does this by translating the source code into the equivalent machine code line-by-line as the program is running.Advantages of using an interpreter:It is easier to check for errors than with a compiler because the error can easily be traced to the line of source code that generated it.It is faster to develop software because the whole program does not need to be compiled every time something needs to be tested.Disadvantages of using an interpreter:The program cannot be executed without the source code.Because the source code needs to be available and is usually just text, the program to be executed is less secure.An interpreted program will execute more slowly than a compiled program due to the line-by-line translation.
12SummaryMachine code instructions are the binary code instructions that the CPU actually executes.Assembly language uses easily remembered codes to directly represent machine code instructions.All executable programs have to be translated into machine code before the CPU can run them.An assembler translates assembly language programs into machine code.A compiler translates all the source code into machine code and creates a stand-alone program which the CPU executes.An interpreter allows the programmer to run the source code within the interpreter by translating the source code into the equivalent machine code as the program is running.
13Task Using the LMC and the worksheet 1 enter in the programs and execute them to see how the computer deals with the program Try and have a go of the questions in worksheet 2