CS26: Programming Languages & Translators
Machine code, assembly language, high-level languages, and the translators that convert between them.
Machine code, assembly language, high-level languages, and the translators that convert between them.
There are three main levels of programming language:
A machine code instruction might look like: 10110000 01100001
The first byte (10110000) is the opcode - it tells the CPU to move data. The second byte (01100001) is the operand - the value 97 in decimal. Together, this instruction means "move the value 97 into a register."
| Mnemonic | Meaning | Description |
|---|---|---|
| MOV | Move | Copies a value to a register or memory location |
| ADD | Add | Adds a value to a register |
| SUB | Subtract | Subtracts a value from a register |
| CMP | Compare | Compares two values and sets flags |
| JMP | Jump | Jumps to another instruction (unconditional) |
| JE | Jump if Equal | Jumps if the previous comparison was equal |
| LD | Load | Loads data from memory into a register |
| ST | Store | Stores data from a register into memory |
Python: print("Hello World")
Java: System.out.println("Hello World");
C#: Console.WriteLine("Hello World");
Each of these simple one-line statements might translate to dozens of machine code instructions. The high-level language hides this complexity from the programmer.
| Property | Machine Code | Assembly Language | High-Level Language |
|---|---|---|---|
| Format | Binary (0s and 1s) | Mnemonics (MOV, ADD) | English-like statements |
| Translation needed | None (CPU executes directly) | Assembler | Compiler or interpreter |
| Portability | Not portable (CPU-specific) | Not portable (CPU-specific) | Portable (with recompilation) |
| Ease of reading | Very difficult | Difficult | Easy |
| Ease of writing | Very difficult | Moderate | Easy |
| Debugging | Very difficult | Difficult | Easier |
| Execution speed | Fastest | Fast | Slower (translation overhead) |
| Hardware control | Direct, complete control | Direct, complete control | Limited (abstracted) |
| Lines of code for same task | Most | Many | Fewest |
A compiler translates the entire source code of a high-level language into machine code all at once, creating an executable file.
An interpreter translates and executes source code one line at a time, without creating a separate executable file.
An assembler translates assembly language (mnemonics) into machine code (binary).
| Property | Compiler | Interpreter |
|---|---|---|
| Translation | All at once, before running | One line at a time, during running |
| Output | Creates executable file | No executable file created |
| Execution speed | Fast (already translated) | Slow (translating while running) |
| Error detection | Before running (compile time) | During running (runtime) |
| Distribution | Share executable (source hidden) | Must share source code |
| Debugging | Harder (error in compiled output) | Easier (errors found at exact line) |
| Re-running | No recompilation needed | Must re-interpret every time |
| Platform dependence | Executable is platform-specific | Source code is platform-independent |
| Examples | C, C++, Java (to bytecode) | Python, JavaScript, Ruby |
| Scenario | Best Translator | Why |
|---|---|---|
| Developing a commercial application | Compiler | Creates fast executable; source code is protected |
| Learning to program / debugging | Interpreter | Immediate feedback; errors found at exact line |
| Writing device drivers | Assembler | Need direct hardware control and speed |
| Web development (client-side) | Interpreter | JavaScript is interpreted in the browser |
| Embedded systems | Assembler / Compiler | Need speed and hardware control; limited resources |
| Rapid prototyping | Interpreter | Quick testing and modification without recompilation |
| Mistake | Why It's Wrong | How to Fix It |
|---|---|---|
| Saying "compilers are faster" | Compilation is slow; execution of compiled code is fast | Be precise: compiled CODE runs faster |
| Saying "interpreters find errors before running" | Interpreters find errors during execution (runtime) | Compilers find errors before running; interpreters during |
| Confusing assembler with assembly language | Assembly language IS a language; assembler IS a translator | Assembly = language; Assembler = translator program |
| Saying all high-level languages use compilers | Some use interpreters (Python, JavaScript) | Match the language to its typical translator |
Q1: Explain the difference between machine code and assembly language.
Q2: Compare a compiler and an interpreter in terms of how they translate code and when they find errors.
Q3: Why might a programmer choose to write in assembly language instead of a high-level language?
Q4: What is the role of an assembler?
Q5: Give two advantages and two disadvantages of using a high-level language compared to assembly language.
✗ Thinking assembly language is the same as machine code ✓ Assembly language uses mnemonics (ADD, MOV, LDR) which are human-readable. Machine code is pure binary (1s and 0s) that the CPU executes directly.
✗ Believing high-level languages are always better than low-level languages ✓ High-level languages are easier to write and portable, but low-level languages give direct hardware control and can be more efficient for specific tasks like device drivers.
✗ Confusing assembly language mnemonics with high-level language commands ✓ Assembly mnemonics (LDR, STR, ADD, MOV) correspond to single CPU instructions. High-level commands (print, if, for) may translate to many machine code instructions.
✗ Forgetting that all code must eventually become machine code to execute ✓ Regardless of whether you write in Python, Java, or assembly, the CPU can only execute machine code (binary). Every language must be translated to machine code at some point.
Compare high-level and low-level programming languages, giving two advantages of each. Explain why a device driver might be written in a low-level language. [5 marks]
High-level languages (e.g. Python, Java): Advantage 1: Easier to read, write and debug because they use English-like commands Advantage 2: Portable — code can run on different hardware architectures with minimal changes Low-level languages (e.g. assembly): Advantage 1: Direct hardware control — can access specific memory addresses and CPU registers Advantage 2: More efficient — produces faster, more compact code with no unnecessary instructions A device driver would be written in a low-level language because it needs direct access to hardware components (ports, registers, memory-mapped I/O). High-level languages abstract away these hardware details, making them unsuitable for the precise, hardware-specific operations a driver must perform. Low-level code ensures the driver can communicate with the device at the register level.
AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including programming languages and translators: compilers and interpreters for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application — 40%): Apply knowledge and understanding of computer science, including programming languages and translators: compilers and interpreters to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including programming languages and translators: compilers and interpreters, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.