Lesson 5 - Programming Languagesx
Download
Report
Transcript Lesson 5 - Programming Languagesx
Lesson 5
GCSE Computing –
programming languages
Candidates should be able to:
explain the difference between high level code and machine
code
explain the need for translators to convert high level code to
machine code
describe the characteristics of an assembler, a compiler and
an interpreter
Slide 2
What 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 hardwaredependent programming language because the instructions
are often specific to a particular series of CPU designs.
Slide 3
Can a program be written directly
In machine code?
0100101001000110
Because each instruction is a binary
number it is very difficult for humans to 1110101010010100
read or develop software directly using 1101000101010101
0100100010110100
machine code.
Assembly language was developed to 1010100101010010
solve this problem (followed by
1010100000100001
increasingly advanced high level
0011101000100001
programming languages).
0010000100101010
1010001101110011
0010101001101001
0010010100101001
Slide 4
What 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).
Slide 5
Comparing 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.
Slide 6
How 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.
INP
LOOP SUB
OUT
BRZ
BRA
QUIT HLT
ONE
ONE // RAM address label LOOP. Subtract value stored at RAM address ONE
// Output the number
QUIT // If at 0, branch to RAM address QUIT
LOOP // Not at 0 so branch to RAM address LOOP
// RAM address label QUIT. End the program
DAT 1 // DATA, store 1 in this RAM address, and label it ONE
Slide 7
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
Slide 8
structures than just simple branches
Why 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 compiler
an interpreter
Slide 9
Comparing a compiler and an
interpreter
A compiler
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.
Slide 10
Comparing a compiler and an
interpreter
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 lineby-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.
Slide 11
Summary
Machine 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.
Slide 12
Task
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