What is source code?
- Source code is Human-readable and descriptive code that is easy to understand, maintain, and debug
- Computers cannot directly execute source code; it must be translated into machine code
- The process of converting source code into machine code is called translation
What is a translator?
- Translators convert source code from a high-level language to a low-level language
- There are three main types of translators:
- Interpreters
- Compilers
- Assemblers
Interpreters
- Interpret source code line-by-line and executes it on the fly
- Easier to debug, allows incremental testing, and is generally faster to start execution
- Slower execution time overall and requires the interpreter to be present during the execution
Compilers
- Translates the entire source code into machine code at once and then executes it
- Faster execution time, no need for the compiler during execution
- Longer initial compilation time and can be more challenging to debug
Assemblers
- Assemblers translate assembly language into machine code
- Unlike interpreters and compilers that work with high-level languages, assemblers deal with low-level languages
- The diagram below shows examples of programming languages, from low-level to high-level
Levels of Abstraction in Programming Languages
Translators used for common languages
| Programming Language | Translator |
|---|---|
| C | Compiler |
| C++ | Compiler |
| Java | Compiler |
| Python | Interpreter |
| JavaScript | Interpreter |
| Ruby | Interpreter |
| Swift | Compiler |
| Assembly Language | Assembler |
| PHP | Interpreter |
Why do different languages have different translators? (non-syllabus content)
- Level of Abstraction
- High-level languages like Python or JavaScript are further from machine language and often use interpreters to allow more flexibility and ease of development
- Low-level languages like C or Assembly are closer to machine code and typically use compilers or assemblers for efficiency
- Execution Model
- Languages like C and C++ are compiled into machine code specific to a target platform
- This allows for optimisations to make the code run more efficiently
- But the code is generally not portable between different platforms
- Languages like Python and JavaScript are interpreted, meaning they are translated line-by-line at runtime on any platform
- This provides greater portability, as the same code can run on different platforms
- This may sacrifice some performance compared to compiled code
- Development Paradigm
- Interpreted languages allow determining variable types at runtime, which makes development faster
- e.g.
var x = 5(Interpreter will calculatexto be a number at runtime)
- e.g.
- Compiled languages enforce stricter type-checking at compile time, which requires additional work across the project
- e.g.
String var name = 'Michael';(Compiler will demand thatnamehas a data type before compiling)
- e.g.
- Interpreted languages allow determining variable types at runtime, which makes development faster
Worked Example
The program below is written in assembly code using the Little Man Computer instruction set. It is supposed to take in two numbers and output the higher.
INP STA NUMA INP STA NUMB SUB NUMA BRP NOTA LDA NUMB BRA QUIT NOTA LDA NUMA QUIT OUT HLT NUMA DAT NUMB DATState what type of translator program would be needed to convert the code above into machine code.
[1]
Answer:
Answer that gets full marks: Assembler.