Learning Objectives
By the end of this lesson, you will be able to:
- Show understanding of the need for assembler software for translating assembly language programs
- Show understanding of the need for compilers and interpreters for translating high-level language programs
- Explain the benefits and drawbacks of using either compiler or interpreter and justify use of each
- Show awareness that high-level language programs may be partially compiled and partially interpreted (e.g., Java)
- Describe features found in a typical Integrated Development Environment (IDE)
- Understand the translation process from source code to machine code
- Differentiate between single-pass and two-pass assemblers
Key Terms
Language Translator
Program that translates source code written in assembly or high-level language into machine language object code
Assembler
Translates source program written in assembly language into executable object code in machine language
Compiler
Translates whole source program written in high-level language into object code before execution
Interpreter
Translates source program written in high-level language into object code during step-by-step execution
Source Code
Program written in assembly language or high-level language by a programmer
Object Code
Machine language code produced by translator programs (assembler, compiler, interpreter)
Machine Code
Binary code that can be directly executed by the computer's CPU
Mnemonic Code
Abbreviated codes used in assembly language to represent machine instructions (e.g., LDD, STO, ADD)
Symbol Table
Data structure created by assembler containing binary codes for symbolic names and labels
Forward Reference
When a symbol is used in assembly language before it is defined
Intermediate Code
Low-level machine independent code produced during compilation (e.g., bytecode, p-code)
IDE (Integrated Development Environment)
Software application that provides comprehensive facilities for programmers
Prettyprinting
Presentation of program code with automatic colour-coding and indentation
Dynamic Syntax Checking
Finding possible syntax errors as program code is being typed
Debugging
Process of finding and correcting errors (bugs) in a program
Bytecode
Intermediate code produced by Java compiler, executed by Java Virtual Machine
Language Translators Overview
Language Translators are programs that translate source code written in Assembly Language or High Level Language into object code of machine language. Without translators, computers cannot understand human-readable programming languages.
Assembler
Translates source program written in Assembly Language into executable object code in machine language.
- Uses mnemonic codes (e.g., LDD, STO, ADD)
- Machine dependent (not portable between different computers/chips)
- One-to-one translation (each assembly instruction → one machine code instruction)
Compiler
Translates whole source program written in High Level Language into object code before execution.
- Produces executable object file
- Machine independent (portable)
- Instruction explosion (one high-level statement → many machine code instructions)
- Used for distributing finished programs
Interpreter
Translates source program during execution, line by line, without producing separate object file.
- No separate executable file produced
- Machine independent (portable)
- Instruction explosion (one-to-many translation)
- Used during program development
Translation Process Visualizer
Real-Life Example: Mobile App Development
When developers create mobile apps:
- During development: They use an interpreter to test code quickly, finding errors line by line
- For final release: They use a compiler to create an executable app file (.apk for Android, .ipa for iOS)
- For system software: Parts of the operating system might be written in assembly language and use an assembler for maximum speed
Activity 1: Translator Types
Identify which type of translator would be most appropriate for each scenario:
- A programmer is testing a new Python script and wants to find errors quickly
- A software company needs to distribute a finished Windows application to customers
- A developer is writing code for a specific microprocessor in a robot
- A student is learning programming and wants to see immediate results of each line
- A game developer needs maximum performance for physics calculations
Solution:
- Interpreter - Allows quick testing and error identification during development
- Compiler - Produces executable file for distribution, protects source code
- Assembler - Machine-specific code for hardware control (robots, microprocessors)
- Interpreter - Provides immediate feedback, good for learning
- Assembler or Compiler with optimization - Assembly for maximum speed, optimized compiled code for performance-critical sections
Check Your Understanding: Language Translators
1. What is the main purpose of a language translator? [1 mark]
Answer
[1 mark] To translate source code written in assembly language or high-level language into object code (machine language) that the computer can execute.
2. Why is assembly language described as "machine dependent"? [2 marks]
Answer
- [1 mark] Assembly language instructions correspond directly to specific machine code instructions for a particular processor
- [1 mark] Programs written for one type of computer/chip will not work on another type without modification
3. What is the key difference between how a compiler and an interpreter work? [2 marks]
Answer
- [1 mark] Compiler translates the entire source program into object code before execution
- [1 mark] Interpreter translates and executes the source program line by line during execution
4. What does "instruction explosion" mean in the context of compilers and interpreters? [1 mark]
Answer
[1 mark] One line of high-level language source code generates many machine code instructions (one-to-many translation).
5. Give one advantage of using an interpreter during program development. [1 mark]
Answer
[1 mark] Errors can be identified as they occur and corrected without having to wait for the whole source code to be read and analyzed.
Assemblers
An assembler is a language translator that translates source program written in Assembly Language into executable object code in machine language. Assembly language uses mnemonic codes to represent each low-level machine instruction.
Types of Assemblers
Single Pass Assemblers
A single pass assembler puts machine code instructions straight into computer memory to be executed immediately.
- Scans source code once
- Direct execution to memory
- Faster but limited functionality
- Cannot handle forward references easily
Two Pass Assemblers
Source code is scanned twice to complete the symbol table and produce final machine code.
- First pass: Creates symbol table
- Second pass: Generates machine code
- Can handle forward references
- Produces object code for storage and later execution
Two-Pass Assembler Simulation
Watch how a two-pass assembler processes assembly code:
Assembly Code
First Pass
Second Pass
Symbol Table Creation: The assembler scans instructions sequentially. When it meets a symbolic address, it checks if it's already in the symbol table. If not, it adds it. If absolute address is known, it's entered; otherwise marked unknown.
Symbol Table Example
During the first pass, the assembler creates a symbol table:
| Symbolic Address | Absolute Address |
|---|---|
| StartProg | 0 |
| Offset | UNKNOWN |
| Value | UNKNOWN |
| EndProg | UNKNOWN |
Forward Reference Problem: A symbol may be used before it's defined (e.g., JPE EndProg before EndProg: END). Two-pass assemblers solve this by noting forward references in pass 1 and resolving them in pass 2.
Real-Life Example: Embedded Systems
Assembly language and assemblers are used in:
- Operating system kernels: Critical parts of Windows, Linux, macOS
- Robotics: Control systems for precise motor movements
- Medical devices: Pacemakers, insulin pumps requiring reliable timing
- Game consoles: Graphics rendering for maximum performance
- IoT devices: Microcontrollers in smart home devices
These applications need speed and direct hardware control that only assembly language can provide.
Activity 2: Assembler Operations
Based on the assembly code below, answer the questions:
- What would the symbol table contain after the first pass?
- What is the forward reference in this code?
- Why would a two-pass assembler be needed for this code?
- How many machine code instructions would this generate (approximately)?
Solution:
-
Symbol table:
Start: 0
Loop: UNKNOWN (initially, resolved in pass 2)
Count: UNKNOWN (initially, resolved in pass 2) -
Forward reference:
JGT Loop- The jump instruction references the label "Loop" before it's defined in the code. - Two-pass needed: To resolve the forward reference (JGT Loop). The first pass identifies "Loop" as a label but doesn't know its address yet. The second pass can fill in the correct address.
- Machine instructions: Approximately 8-10 machine code instructions (each assembly instruction typically becomes one machine code instruction in assembly language).
Check Your Understanding: Assemblers
1. What is the main difference between single-pass and two-pass assemblers? [2 marks]
Answer
- [1 mark] Single-pass assemblers scan source code once and put machine code directly into memory
- [1 mark] Two-pass assemblers scan source code twice: first to create symbol table, second to generate machine code
2. What is a symbol table and why is it needed? [2 marks]
Answer
- [1 mark] A data structure created by the assembler containing binary codes for symbolic names and labels used in the program
- [1 mark] Needed to replace symbolic addresses with absolute memory addresses during translation
3. What is the "forward reference problem" in assembly language? [2 marks]
Answer
- [1 mark] When a symbol (label) is used in the program before it is defined
- [1 mark] This causes problems for assemblers because they don't know the address of the symbol when they first encounter it
4. List three tasks performed during the first pass of a two-pass assembler. [3 marks]
Answer
[1 mark each, any three]
- Read assembly language program line by line
- Removal of comments and white spaces
- Data values are converted to binary values
- Check opcode for errors
- Creation of Symbol Table
- Creation of a literal table if constants are used
- Identification of System Calls and Subroutines
- Expansion of macros
- Acting upon any directives
5. Why are assemblers still used today despite high-level languages being easier? [2 marks]
Answer
- [1 mark] For tasks that need maximum speed and efficiency (e.g., operating system kernels, device drivers)
- [1 mark] For direct hardware control in embedded systems, robotics, and specialized devices where high-level languages may not provide enough control
Compilers and Interpreters
Compilers and interpreters both translate high-level language programs into machine code, but they work in fundamentally different ways with different advantages and disadvantages.
Compiler Operation
How Compilers Work
A compiler translates the entire source program into object code before execution:
1. Source Code Input
Compiler reads the complete source code file
2. Analysis and Error Checking
Each line is analyzed for syntax errors
3. Intermediate Code Generation
Source code converted to intermediate code
4. Object Code Output
If no errors, intermediate code converted to executable object file
How Interpreters Work
An interpreter translates and executes the program line by line during execution:
1. Start Execution
Interpreter program begins execution with source code
2. Read First Line
First line of source code is read and analyzed
3. Error Check
If error found, reported and execution halts
4. Execute Line
If no error, line converted to intermediate code and executed
5. Next Line
Next line of source code is read, process repeats
Compiler vs Interpreter Comparison
| Aspect | Compiler | Interpreter |
|---|---|---|
| Translation Time | Before execution (compile time) | During execution (run time) |
| Output | Executable object file | No separate object file |
| Execution Speed | Faster (code already translated) | Slower (translating during execution) |
| Error Detection | All errors found before execution | Errors found as they occur during execution |
| Memory Usage | More (stores object code) | Less (no object code storage) |
| Portability | Object code is machine-specific | Source code is portable, interpreter handles machine differences |
| Best For | Finished programs, distribution | Development, learning, scripting |
Partial Compilation and Interpretation
Many modern languages use a hybrid approach that combines compilation and interpretation:
Java Example
- Step 1: Java source code compiled to bytecode (.class files)
- Step 2: Bytecode interpreted by Java Virtual Machine (JVM)
- Step 3: JVM translates bytecode to machine code for specific computer
- Advantage: "Write once, run anywhere" - bytecode works on any computer with JVM
Python Example
- Step 1: Python source code compiled to bytecode (.pyc files)
- Step 2: Bytecode interpreted by Python Virtual Machine
- Step 3: Can use Just-In-Time (JIT) compilation for speed
- Advantage: Combines portability of interpretation with speed of compilation
Key Insight: Why Hybrid Approaches?
Pure interpretation is too slow for production software. Pure compilation lacks portability. Hybrid approaches like Java's bytecode system give the best of both worlds: reasonable speed through partial compilation, and portability through interpretation by a virtual machine.
Real-Life Example: Web Browsers
Web browsers use different translation approaches for different web technologies:
JavaScript
WebAssembly
HTML/CSS
This shows how different translation methods are chosen based on performance needs and use cases.
Activity 3: Compiler vs Interpreter Analysis
Analyze the following scenarios and decide whether a compiler or interpreter would be more appropriate:
- A software company is developing a new video editing application for Windows
- A teacher wants to show students how Python executes code line by line
- A developer is creating a script to automate file backups on a server
- A game studio is optimizing their game engine for maximum performance
- A student is learning programming and makes frequent syntax errors
Solution:
- Compiler - Commercial applications need the speed of compiled code and distribution as executable files
- Interpreter - Perfect for teaching as it shows execution step by step
- Interpreter or scripting language - Scripts are often interpreted for flexibility and quick changes
- Compiler with optimization - Game engines need maximum performance, which compiled code provides
- Interpreter - Immediate error feedback helps learners correct mistakes quickly
Check Your Understanding: Compilers & Interpreters
1. What is the main advantage of using a compiler for distributing software? [2 marks]
Answer
- [1 mark] The executable file produced by the compiler can be distributed to users
- [1 mark] Users have no access to the source code, protecting intellectual property
2. Why do interpreted programs generally run slower than compiled programs? [2 marks]
Answer
- [1 mark] Compiled programs are translated entirely to machine code before execution
- [1 mark] Interpreted programs must be translated line by line during execution, adding overhead
3. What is bytecode in the context of Java programming? [2 marks]
Answer
- [1 mark] Intermediate code produced by the Java compiler
- [1 mark] Low-level machine independent code that is interpreted by the Java Virtual Machine (JVM)
4. Explain the concept of "partial compilation and interpretation" with an example. [3 marks]
Answer
- [1 mark] Some high-level language programs are first compiled to intermediate code
- [1 mark] The intermediate code is then interpreted for execution
- [1 mark] Example: Java source code is compiled to bytecode, which is then interpreted by the Java Virtual Machine
5. What is one disadvantage of distributing compiled object code to users? [1 mark]
Answer
[1 mark] Compiled object code is less secure because it could contain a virus or malware that is difficult to detect in machine code.
Integrated Development Environments (IDEs)
An Integrated Development Environment (IDE) is used by programmers to aid writing and development of programs. It combines several tools into a single application to streamline the development process.
Key Features of IDEs
Prettyprinting
Automatic presentation of code with colour-coding of keywords, functions, comments, and strings. Also provides automatic indentation for better readability.
Source Code Editor
Allows programs to be written and edited without needing a separate text editor. Speeds up development as editing can be done within the same software.
Context-Sensitive Prompts
Displays hints, keywords, and available identifiers appropriate at the current insertion point. Shows predictions of code being entered (auto-complete).
Dynamic Syntax Checking
Finds possible syntax errors as code is being typed. Alerts programmer immediately, underlining or highlighting statements that don't meet language rules.
Expand/Collapse Code Blocks
For larger programs, code blocks can be collapsed to single lines, allowing programmers to focus on the code they're currently developing.
Debugging Tools
Includes debugger to run program under development. Allows single stepping through code, setting breakpoints, and examining variables and expressions.
IDE Feature Simulator
Try out different IDE features on this sample Python code:
IDE Features
How IDEs Help Programmers: IDEs combine all these features to make programming faster, easier, and less error-prone. They're especially helpful for beginners who are learning syntax and debugging techniques.
Debugging Features in IDEs
Single Stepping
Execute program one line at a time. Allows programmer to see exactly what happens at each step and identify where errors occur.
Breakpoints
Set points in code where execution stops automatically. Programmer can examine variable values and program state at that point.
Report Window
Shows contents of variables and expressions evaluated at breakpoints. Helps identify logic errors by showing actual vs expected values.
These debugging tools are essential for finding and fixing logic errors that wouldn't be caught by syntax checking alone.
Real-Life Example: Learning to Program
When students learn programming, IDEs provide crucial support:
- Syntax highlighting: Colour-coding helps distinguish keywords, variables, and comments
- Immediate error feedback: Dynamic syntax checking underlines errors as they're typed
- Auto-complete: Suggests correct function names and parameters, reducing typos
- Step-by-step debugging: Allows tracing through code to understand program flow
- Built-in interpreter: Quick testing of code snippets without leaving the IDE
Modern IDEs like VS Code, PyCharm, and Eclipse make learning programming more accessible by handling routine tasks and providing helpful feedback.
Check Your Understanding: IDEs
1. What is an Integrated Development Environment (IDE)? [2 marks]
Answer
- [1 mark] Software application used by programmers to aid writing and development of programs
- [1 mark] Combines several tools (editor, compiler/interpreter, debugger) into a single application
2. What is "prettyprinting" in an IDE? [2 marks]
Answer
- [1 mark] Presentation of program code with automatic colour-coding
- [1 mark] Keywords, built-in functions, comments, strings and identifiers are shown in different colours for better readability
3. How does dynamic syntax checking help programmers? [2 marks]
Answer
- [1 mark] Finds possible syntax errors as program code is being typed
- [1 mark] Alerts programmer immediately, allowing errors to be corrected during writing rather than after attempting to run the program
4. Describe two debugging features available in most IDEs. [4 marks]
Answer
- [2 marks] Single stepping: Allows programmer to execute program one line at a time to see exactly what happens at each step
- [2 marks] Breakpoints: Programmer can set points in code where execution stops automatically, allowing examination of variable values and program state at that point
5. What is the purpose of context-sensitive prompts in an IDE? [2 marks]
Answer
- [1 mark] Displays hints, keywords, and available identifiers that might be appropriate at the current insertion point
- [1 mark] Shows predictions of code being entered (auto-complete) to speed up coding and reduce errors
Key Takeaways
- Language translators convert source code (assembly or high-level) into machine code object code
- Assemblers translate assembly language (machine-dependent) using mnemonic codes in a one-to-one translation
- Single-pass assemblers put machine code directly into memory; two-pass assemblers scan code twice to handle forward references
- Compilers translate entire high-level programs before execution, producing executable object files
- Interpreters translate and execute high-level programs line by line during execution
- Compiled programs run faster but are machine-specific; interpreted programs are portable but slower
- Hybrid approaches like Java use partial compilation to bytecode, then interpretation by a virtual machine
- IDEs (Integrated Development Environments) combine tools like editors, compilers, and debuggers
- IDE features include prettyprinting, dynamic syntax checking, context-sensitive prompts, and debugging tools
- Debugging tools in IDEs allow single stepping, breakpoints, and variable examination
- Assembly language is used for speed-critical tasks like OS kernels, robotics, and embedded systems
- High-level languages with compilers are used for application software development and distribution
- Interpreters are ideal for program development, learning, and scripting due to immediate feedback
Question Bank
1. Compare and contrast compilers and interpreters. [6 marks]
Marking Scheme & Answer
- [2 marks] Compiler: Translates entire program before execution. Produces executable object file. Faster execution. Errors found before execution.
- [2 marks] Interpreter: Translates and executes line by line. No separate object file. Slower execution. Errors found during execution.
- [1 mark] Compiler use: Finished programs, software distribution, performance-critical applications
- [1 mark] Interpreter use: Program development, learning, scripting, rapid prototyping
2. Explain the two-pass assembly process and why it is necessary. [5 marks]
Marking Scheme & Answer
- [1 mark] First pass: Scans source code to create symbol table, remove comments, check opcodes, convert data to binary
- [1 mark] Second pass: Uses symbol table to replace symbolic addresses with absolute addresses, generates machine code
- [1 mark] Symbol table: Contains binary codes for symbolic names and labels used in program
- [1 mark] Forward reference problem: When symbols are used before they are defined in the code
- [1 mark] Necessity: Two passes are needed to resolve forward references - first pass notes all symbols, second pass can resolve references once all symbols are known
3. Describe how Java uses both compilation and interpretation. [4 marks]
Marking Scheme & Answer
- [1 mark] Java source code is first compiled to produce bytecode (intermediate code)
- [1 mark] Bytecode is low-level machine independent code stored in .class files
- [1 mark] When program runs, bytecode is interpreted by the Java Virtual Machine (JVM)
- [1 mark] JVM translates bytecode to machine code for the specific computer it's running on
- [Additional] This allows "write once, run anywhere" - same bytecode works on any computer with JVM installed
4. Identify and describe four features of an IDE that help programmers. [8 marks]
Marking Scheme & Answer
- [2 marks] Prettyprinting: Automatic colour-coding of keywords, functions, comments, strings and automatic indentation for better code readability
- [2 marks] Dynamic syntax checking: Finds syntax errors as code is typed, alerts programmer immediately, underlines/highlights incorrect statements
- [2 marks] Context-sensitive prompts: Displays hints, keywords, and available identifiers appropriate at current insertion point (auto-complete)
- [2 marks] Debugging tools: Single stepping through code line by line, setting breakpoints to stop execution, examining variable values and expressions
5. Explain why assembly language is still used today despite high-level languages being easier. [4 marks]
Marking Scheme & Answer
- [1 mark] Speed and efficiency: Assembly provides maximum performance for time-critical tasks
- [1 mark] Direct hardware control: Needed for operating system kernels, device drivers, embedded systems
- [1 mark] Small code size: Important for systems with limited memory (microcontrollers, IoT devices)
- [1 mark] Specific applications: Robotics, medical devices, game console programming where precise timing and control are essential
6. What are the advantages and disadvantages of using an interpreter during program development? [6 marks]
Marking Scheme & Answer
- [3 marks] Advantages:
- Errors identified immediately as they occur during execution
- No need to wait for entire program to be compiled before testing
- Good for learning - immediate feedback helps understand program flow
- Easier to debug - can stop and examine state at any point
- [3 marks] Disadvantages:
- Slower execution compared to compiled programs
- Source code must be distributed with interpreter for others to use
- Less secure - source code is visible to users
- Not suitable for performance-critical applications
7. Describe the steps a compiler takes to translate source code into machine code. [5 marks]
Marking Scheme & Answer
- [1 mark] Compiler program reads the source code file line by line
- [1 mark] Each line is analyzed for syntax errors (if error found, it is recorded)
- [1 mark] If no error found, the line is converted to intermediate code
- [1 mark] Process continues until whole source code has been processed
- [1 mark] If no errors in entire source code, complete intermediate code is converted to object code (machine code)
8. What is the difference between machine code and bytecode? [3 marks]
Marking Scheme & Answer
- [1 mark] Machine code: Binary code that can be directly executed by a specific computer's CPU
- [1 mark] Bytecode: Intermediate code that is machine independent, produced by compilers like Java compiler
- [1 mark] Execution: Machine code runs directly on hardware; bytecode needs an interpreter/virtual machine (like JVM) to execute
9. Why would a programmer choose to write in assembly language rather than a high-level language? [3 marks]
Marking Scheme & Answer
- [1 mark] When maximum performance is required (e.g., game physics, real-time systems)
- [1 mark] For direct hardware control (e.g., device drivers, embedded systems, robotics)
- [1 mark] When working with limited memory/resources (e.g., microcontrollers, IoT devices)
- [Additional] For reverse engineering or understanding how high-level code translates to machine operations
10. Explain the purpose of debugging tools in an IDE and give two examples. [4 marks]
Marking Scheme & Answer
- [2 marks] Purpose: To help programmers find and correct errors (bugs) in programs. Debuggers run programs under development and allow examination of program state, variables, and execution flow.
- [1 mark] Example 1: Single stepping - executing program one line at a time to see exactly what happens
- [1 mark] Example 2: Breakpoints - setting points where execution stops automatically to examine variable values
- [Additional] Report window - shows contents of variables and expressions at breakpoints