AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Software behind the screen

Week 26 · 4.3 Computer software · 3 lessons
3

Source code to machine code

Computing 9: Topic 4.3, pp. 320–334
GoalExplain why programs need a translator and compare a compiler with an interpreter.
New words
source code · a program written by a person in a high-level languagemachine code · binary patterns that the processor understands directlycompiler · a translator that deals with the whole program at onceinterpreter · a translator that deals with one line at a time
Explanation

People write source code in a high-level language, with English-like words such as IF and WHILE. A processor cannot follow such words. It follows only machine code, which is made of binary patterns, and each pattern means one command. So a translator, which is system software, turns source code into machine code. A compiler translates the whole program in one go. If it finds a syntax error, nothing runs. If there is none, it makes an executable file, which can be run again later without the compiler. An interpreter works through the program line by line, translating and running each line in turn. It keeps going until it meets an error, then stops. It makes no executable file, so the interpreter is needed every time the program is run. Operating systems, utilities and translators are all system software.

Worked examples
QuestionCompilerInterpreter
How much is translated at a time?the whole programone line
Is a translator needed every time the program runs?no, the executable file is enoughyes
Is an executable file made?yesno
Compiler or interpreter? The table compares them on three questions. The answers explain why a compiled program can be sent to people who do not own the compiler, while an interpreted program needs the interpreter on the computer each time.
Pseudocode
1OUTPUT "Start"
2total ← 4 + 5
3OUTPUT total
4OUTPT "End"
An error on line 4. The four lines below are written in an imagined language, and OUTPT on line 4 is a spelling mistake. A compiler reads the whole program first, finds the mistake and runs nothing, so nothing is printed. A line-by-line interpreter runs lines 1 to 3, so it prints Start and 9, and then stops at line 4 with an error message. We use an imagined language because Python checks a whole program for spelling mistakes of this kind before it runs any line, so Python would print nothing, like a compiler. The lines are only a picture of the idea: a computer never runs pseudocode, and real translators differ in detail.
SituationBetter choiceReason
a finished game is sent to thousands of playerscompilerthe executable file runs without a translator
a learner tests a short program after each changeinterpreterit runs straight away and stops where the error is
a company wants a program to start at once, every timecompilerit is translated only once
Choosing a translator. Each situation gives a reason for one kind. A finished game for thousands of players should be an executable file that runs without any translator. A learner who changes a line and runs the program again and again is helped by an interpreter, because it runs at once and stops at the line with the problem.
Class activity

Two ways to translate (no computer). Invent four symbols and what they mean, for example ▲ = clap, ■ = stamp, ● = turn round, ◆ = jump. Write a secret program of eight symbols on paper, with one symbol that is not on the key sheet as the “syntax error”, in the fifth place. Your partner has the key sheet. Round 1, compiler: the partner first translates all eight symbols into words on a new sheet, finds the error and does nothing. Round 2, interpreter: the partner translates one symbol, does the action, then the next, until the error stops them. Count how many actions were done in each round.

Practice
1
Write compiler or interpreter for each description.
DescriptionTranslator
(a) It translates the whole program before anything runs.?
(b) It has already run the earlier lines when it meets an error.?
(c) It produces an executable file.?
(d) It is needed every time the program runs.?
2
The program below, written in an imagined language, has a mistake on line 3. Write what is printed when it is run by a compiler and by a line-by-line interpreter.
Pseudocode
1OUTPUT "Hello"
2size ← 2 * 3
3OUTPT size
4OUTPUT "Next"
5OUTPUT "End"
3
Why can a CPU not follow source code that uses English-like words directly? What fixes this?
4
Write operating system, utility software or translator for each job.
JobKind of system software
(a) scanning a downloaded file for malware?
(b) giving each running program its share of RAM?
(c) turning source code into machine code?
(d) making a new microphone work with the computer?
(e) checking a username and password at log in?