📘 CodingMarble Learn

Programming Languages and Translators

A CPU can only run machine code: binary instructions. Low-level languages are machine code and assembly language (short mnemonics such as LOAD and ADD, one per machine instruction). High-level languages such as Python, Java and C are easier for people to read and write, and one line may become many machine instructions. Translators convert code: an assembler turns assembly into machine code; a compiler translates a whole high-level program into an executable before it runs; an interpreter translates and runs one line at a time. Some languages compile to bytecode, which a virtual machine then runs.

🎬 Step-by-step story

  1. The CPU only understands machine code: patterns of 0s and 1s. Nothing else can run on it directly.
  2. Assembly language uses short words like LOAD and ADD. Each one matches one machine instruction. An assembler translates it.
  3. High-level languages like Python look like English. They are easy to read, and one line becomes many machine instructions.
  4. A compiler translates the whole program at once into an executable file. Then the file runs fast, again and again.
  5. An interpreter translates one line, runs it, then moves to the next line. It stops at the first error it finds.
  6. Try it: pick a translator, add or remove an error, and press Run to see what happens.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why can't the CPU just read Python?

The CPU's circuits only respond to binary instruction patterns. Step 1 shows only the bit rows reach the CPU.

Is assembly the same as machine code?

No, but it is very close: each mnemonic stands for one machine instruction. An assembler swaps them 1 for 1, as in step 2.

Why is high-level code easier if it has to be translated anyway?

Because people write and fix it much faster. The translator does the hard work of producing many machine instructions from each line, as step 3 shows.

Why is a compiled program faster to run?

Translation happens once, before running. After that the executable is pure machine code, so no time is spent translating. Watch the orange executable in step 4.

Why do beginners often use interpreted languages?

An interpreter stops right at the line with the error, so it is easy to find and fix. Step 5 shows it stopping at line 2.

What if a compiled program has an error?

The compiler lists the errors and no executable is made, so nothing runs. Try Compiler with the error turned on in step 6.

Low-level languages: machine code and assembly

Machine code is the only language a CPU can run. Each instruction is a binary pattern, for example 10110001. Part of it says what to do (the opcode) and part says what to do it to (the operand).

Assembly language replaces the binary with short memory words called mnemonics, such as LOAD, ADD, STORE. Each assembly instruction matches one machine-code instruction (a 1 : 1 match).

Why use low-level code?

Drawbacks

High-level languages

High-level languages (Python, Java, C#, C, JavaScript) use English-like words and maths symbols. One line, such as total = price * 3, may become several machine instructions.

High-levelLow-level
Easy for people?YesNo
Runs on different CPUs?Yes, after translation (portable)No, one CPU family
Control of hardwareLessDirect
Must be translated?Yes (compiler or interpreter)Assembly: yes (assembler); machine code: no

Most software today is written in high-level languages because programmers work faster and make fewer mistakes.

Translators: assembler, compiler and interpreter

Assembler

Turns assembly language into machine code, one instruction to one instruction.

Compiler

Interpreter

Bytecode and virtual machines

Some languages mix both ideas. Java (and Python behind the scenes) first compile source code into bytecode: an in-between set of simple instructions that is not tied to any CPU. A virtual machine (such as the Java Virtual Machine) then interprets or quickly compiles the bytecode on each device. This gives portability: the same bytecode runs on Windows, Android or Linux, as long as a virtual machine exists for it.

Try it: be the interpreter

Write a 5-line "program" of instructions for a friend (e.g. 1. Stand up. 2. Clap twice. 3. Jumpp. 4. Sit. 5. Smile). First act as an interpreter: read and do one line at a time, and stop at the spelling mistake. Then act as a compiler: read all 5 lines first, list every error, and only act when the list is fixed. Which way found the mistake sooner? Which way ran faster once fixed?

Key formulas and definitions

Worked examples

1. A company sells a game but does not want customers to see its source code. Which translator should it use?

A compiler. It produces an executable file that can be shared without the source code, and it runs fast.

2. A student is learning to program and wants to find mistakes quickly. Which translator is better, and why?

An interpreter. It runs one line at a time and stops at the first error, showing where it is, so the student can fix it and try again at once.

3. Give one reason a programmer might write a device driver in assembly language.

Assembly gives direct control of the hardware and registers and produces small, fast code, which a driver needs.

4. Why can the same Java bytecode run on a phone and a laptop with different CPUs?

The bytecode is not machine code for any one CPU. Each device has its own Java Virtual Machine that turns the bytecode into its own machine code.

Common mistakes

Practice quiz

1. Which language can a CPU run directly?
2. Which translator converts assembly language into machine code?
3. Which translator produces an executable file?
4. An interpreter, on finding an error:
5. Which is an advantage of high-level languages?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between a compiler and an interpreter?

A compiler translates the whole program first and makes an executable; an interpreter translates and runs one line at a time.

What does an assembler do?

It translates assembly language mnemonics into machine code, one instruction for one instruction.

Is Python compiled or interpreted?

Usually it is called interpreted. Behind the scenes, the standard Python first compiles code to bytecode and then a virtual machine runs that bytecode.

Where this is taught

England (GCSE, A level)Year 113.4 Computer systems
England (GCSE, A level)Year 124.6 Fundamentals of computer systems

Learn first

Learn next

Related lessons

All Computer Science lessons