Number 3
1. Programming Paradigms Overview
A programming paradigm is a fundamental style or approach to computer programming. Cambridge Paper 3 covers four primary paradigms:
Programming Paradigm | Definition & Core Characteristics |
|---|---|
Object-Oriented (OOP) | Programs using the concepts of class, inheritance, encapsulation, and polymorphism. |
Declarative | Programs that specify the desired result rather than how to get to it. It instructs a program on what needs to be done using facts, rules, and queries to satisfy goals. |
Imperative (Procedural) | Programs with an explicit sequence of commands that update the program state, with or without procedure calls. Uses variables changed via assignment statements and relies on repetition/iteration. |
Low-Level | Programs using the instruction set of a processor. |
2. Object-Oriented Programming (OOP)
Key Definitions & Terminology
Class: A template or blueprint from which objects are created. It defines the structure including attributes/properties with data types, methods (subroutines acting on attributes), getters/setters, and a constructor.
Object / Instance: An occurrence or instantiation of a class during the execution of a program.
Encapsulation: The process of putting properties (data) and methods together as a single unit. It restricts external access to data and hides values of a structured data object inside a class.
Inheritance: The process by which a derived class (subclass) inherits/takes all properties (attributes) and methods from a parent class (superclass or base class). Represents an "is a" relationship. Inherited attributes and methods can be extended, copied, or overridden in the subclass.
Polymorphism: Allows methods with the same name to be redefined or behave differently in derived/inherited classes or subclasses.
Aggregation: The process by which one class contains another class or represents a hierarchy of nested classes/objects. Represents a "has a" relationship between classes.
Property / Attribute: Data items, variables, or characteristics defined within a class.
Method: A programmed function, procedure, or subroutine defined as part of a class that implements behaviors and acts on properties.
Getter: A method used to access or return the value of a property/attribute.
Setter: A method used to update or change the value of a private property/attribute.
Constructor: A special method (e.g.
PUBLIC PROCEDURE NEW) used to initialize attribute values when instantiating objects of a class.
Class vs. Object: Key Differences
Feature | Class | Object (Instance) |
|---|---|---|
Definition | Defined only once in program code. | Created/instantiated multiple times from a class. |
Memory Allocation | No memory is allocated when a class is defined. | Memory space is allocated whenever an object is created. |
Execution State | Cannot be manipulated as it is not active in memory. | Can be actively manipulated during runtime. |
Inheritance | Can use inheritance to derive new classes. | Cannot use inheritance. |
Standard Pseudocode Syntax for OOP
CLASS Node
PRIVATE Content : STRING
PRIVATE Pointer : INTEGER
PUBLIC PROCEDURE NEW(NewContent : STRING, NewPointer : INTEGER)
Content <- NewContent
Pointer <- NewPointer
ENDPROCEDURE
PUBLIC FUNCTION GetContent() RETURNS STRING
RETURN Content
ENDFUNCTION
PUBLIC PROCEDURE SetContent(Value : STRING)
Content <- Value
ENDPROCEDURE
ENDCLASS
(Reference: Official Mark Schemes)
3. Declarative Programming (PROLOG Logic Programming)
Declarative logic programming states a program as a set of logical relations using facts, rules, and goals.
Core Components
Facts (Clauses): Stated truths representing relationships or properties.
Example:
customer(petrov).Example:
owns(projector, petrov).
Variables & Goals: Variables start with an uppercase letter (e.g.
Owner,Pet,List). Querying a goal returns all matching substitutions.Goal Query:
owns(projector, Owner)returnsOwner = petrov, susan, sammy.
Rules: Conditional logic structured as
Head IF Body.AND Logic: Expressed using
ANDor commas,.NOT Logic: Expressed using
NOTor\+.Syntax Example:
may_buy_device(D, A) IF device(D) AND customer(A) AND NOT owns(D, A).
4. Low-Level Programming & Assembly Language
Assembly programs execute instructions directly using processor registers, primarily the Accumulator (ACC).
Addressing Modes
Immediate Addressing (
#n): The operand contains the denary valuendirectly (e.g.LDM #500loads the number 500 into ACC).Direct Addressing (
<address>): The operand specifies the memory address whose contents are loaded into ACC (e.g.LDD 500loads the contents stored at location 500 into ACC).Indirect Addressing (
<address>): The location at<address>contains a second address; the contents of this second address are loaded into ACC (e.g.LDI 500).
Assembly Instruction Set Reference Table
Opcode | Operand | Explanation |
|---|---|---|
LDM |
| Load the denary number |
LDD |
| Load the contents of location |
LDI |
| Load the contents of the second address (stored at |
STO |
| Store the current contents of ACC at location |
ADD |
| Add the contents of address or immediate value |
SUB |
| Subtract the contents of address or immediate value |
INC |
| Add 1 to the contents of the register. |
CMP |
| Compare the contents of ACC with the contents of location |
JPN |
| Following a |
|
| Gives a symbolic address |
5. File Processing
File handling covers sequential processing (text files) and random access processing (binary files using record structures).
Pseudocode File Operations
Access Type | Command Pseudocode | Purpose |
|---|---|---|
Sequential |
| Opens file for reading or writing sequentially. |
Sequential |
| Reads a line from text file into |
Sequential |
| Writes text to file. |
Sequential |
| Checks if end-of-file is reached. |
Random |
| Opens file for direct record access. |
Random |
| Moves file pointer directly to record |
Random |
| Reads record at current location into record variable. |
Random |
| Writes record data directly to current location. |
Both |
| Closes open file stream. |
User-Defined Record Data Types (UDTs)
Records are defined using TYPE ... ENDTYPE before file operations:
TYPE TAccount
DECLARE AccountNumber : INTEGER
DECLARE LastName : STRING
DECLARE FirstName : STRING
ENDTYPE
DECLARE Customer : TAccount
(Reference: Official Mark Schemes)
6. Exception Handling
Definition & Core Purpose
Definition: Exception handling is a process that responds to unwanted or unexpected events when a program is running to prevent the program/computer from halting or crashing unexpectedly.
Implementation Mechanism: Managed using exception handling routines such as
try ... except / catchconstructs to catch runtime errors and display meaningful error messages.
Key Reasons for Including Exception Handling
To trap runtime errors before they reach the OS.
To prevent a program from terminating unexpectedly during execution.
To produce meaningful error messages to inform the user of issues.
Causes & Specific Examples of Exceptions
Category | Exception Cause / Example | Explanation |
|---|---|---|
Programming / Logic Errors | Division by zero | Dividing a number by zero evaluates to infinity, which the processor cannot compute, crashing the program. |
Programming / Logic Errors | Array index out-of-bounds | Attempting to access an index position outside declared array limits. |
User Errors | Incorrect data type / File name input | Entering text when an integer is expected, or typing a non-existent file path. |
Hardware / Hardware Device Failures | Printer disconnect / Disk crash | Losing connection to an external peripheral or hard drive failure during read/write. |
Runtime / File Errors | File Not Found / Unexpected EOF | Opening a non-existent file or reading past end-of-file ( |
Hardware and Virtual Machines
Module 1: Virtual Machines (VMs) & OS Roles
1. Definitions & Core Architecture
Virtual Machine (VM): A standalone software environment running on a physical computer that emulates the hardware and/or software of another computer system. It is not a physical machine, but VM software contains all components of a physical computer system. The host machine provides all the physical resources e.g RAM/ROM and is controlled via a host operating system. A guest OS runs within the VM/host OS and multiple guest OS can run on the same VM
Host Operating System: The primary operating system installed on the physical machine. It controls all physical hardware resources (such as RAM, ROM, and processor access), runs the virtual machine software, and provides the user interface to operate it.
Hypervisor: The software layer running between the host OS and guest OS that creates, manages, and executes virtual machines while allocating host physical resources to the guest OS.
Guest Operating System: The operating system running inside the virtual machine. It controls virtual hardware/software during emulation, provides a virtual user interface, and accesses physical host hardware indirectly via the hypervisor and host OS. Multiple guest OSs can run on the same physical host.
2. Benefits of Virtual Machines
Compatibility & Legacy Software: Allows applications or legacy software incompatible with the host computer or host OS to run via a compatible guest OS.
System Isolation & Protection: Suspicious software, crashes, or malware inside a guest OS are contained entirely within the VM and cannot infect or crash the host machine or other VMs.
Cost Savings: Eliminates the need to buy additional physical hardware to test or run multiple software environments.
Multi-OS Testing: Allows multiple operating systems or new computer system architectures to coexist and be emulated on a single machine.
3. Limitations & Drawbacks of Virtual Machines
Degraded Performance: Less efficient than running on native physical hardware due to indirect hardware access requests, execution of extra emulation code, and shared system resources (CPU/RAM load).
Hardware Constraints: Performance is strictly dependent on host capabilities, and certain modern hardware/software features cannot be fully emulated or directly accessed.
Maintenance Complexity: Setting up, managing, and maintaining both the host OS and virtual machine environments increases overall administrative complexity and cost.
Inaccurate Performance Metrics: Software response times and performance benchmarks cannot be accurately measured inside a virtual machine.
Module 2: Processor Architecture (RISC vs. CISC)
1. Characteristics of RISC (Reduced Instruction Set Computer)
Instruction Set: Uses a small/low number of simple, fixed-length, and fixed-format instructions.
Clock Cycles: Instructions execute in a single clock cycle.
Registers & Memory: Uses many general-purpose registers. Memory access is restricted strictly to explicit LOAD and STORE instructions, making heavy use of RAM.
Control Unit & Cache: Uses a hardwired control unit. Cache is split into separate data and instruction caches.
Hardware Design: Emphasis is placed on software, requiring simpler circuits with fewer physical transistors. Highly suitable for pipelining.
2. Characteristics of CISC (Complex Instruction Set Computer)
Instruction Set: Uses a large instruction set with variable-length instructions and many instruction formats.
Clock Cycles: Instructions are complex and converted into micro-instructions requiring multiple clock cycles.
Registers & Memory: Uses fewer general-purpose registers. Many instruction types address memory directly using a wide range of complex addressing modes.
Control Unit & Cache: Uses micro-coded / programmable control units. Relies heavily on cache memory.
Hardware Design: Emphasis is placed on hardware with complex internal circuitry. Has lower/poorer pipelineability.
3. Summary Comparison Table
Feature | RISC Processor | CISC Processor |
|---|---|---|
Instruction Set Size | Small / Few instructions | Large / Many instructions |
Instruction Format & Length | Fixed-length, simple format | Variable-length, many formats |
Clock Cycles per Instruction | Single clock cycle | Multiple clock cycles |
General-Purpose Registers | Many registers | Few registers |
Control Unit Type | Hardwired control unit | Programmable / Micro-coded |
Addressing Modes | Few / Limited addressing modes | Many complex addressing modes |
Memory Access | Only via LOAD / STORE instructions | Many instruction types directly address memory |
Pipelining Suitability | High / Straightforward pipelineability | Low / Complex pipelineability |
Cache Structure | Split cache (data & instructions) | Unified cache / Frequently used |
Design Emphasis | Software emphasis (fewer transistors) | Hardware emphasis (complex circuits) |
Module 3: Pipelining & Interrupt Handling
1. Pipelining Concepts & Stages
Definition: An instruction execution technique where multiple instructions are processed concurrently to increase CPU instruction throughput (number of instructions completed per unit of time).
The 5 Pipeline Stages:
Instruction Fetch (IF)
Instruction Decode (ID)
Operand Fetch (OF)
Instruction Execute (IE)
Write Back Result (WB)
Execution Rules:
Each subtask stage completes in 1 clock cycle.
No two instructions can execute the exact same subtask stage during the same clock cycle.
Subsequent instructions begin in successive clock cycles (e.g., Instruction 2 starts IF on Clock Cycle 2 while Instruction 1 moves to ID).
2. Interrupt Handling Process
The processor checks for an interrupt at the start or end of a fetch-execute cycle.
If detected, current program execution stops temporarily, and the status of all registers is pushed/saved onto the stack.
The Interrupt Service Routine (ISR) is executed to process the interrupt.
Once serviced, register contents are restored from the stack, and normal program execution resumes.
3. Effect of Pipelining on Interrupt Handling
Pipelining adds complexity because multiple instructions in different stages of completion are in the pipeline when an interrupt arrives.
Flushing the Pipeline: All instructions currently in operation inside the pipeline are discarded (flushed), except for the last instruction that has reached the write-back stage.
The ISR is then applied to service the interrupt.
Once serviced, the processor restarts fetching starting from the next sequential instruction.
Module 4: Parallel Processing & Computer Architectures
1. Flynn's Taxonomy
SISD (Single Instruction, Single Data): Sequential architecture containing one processor, one control unit, and one memory unit. Executes a single instruction stream on a single data stream.
SIMD (Single Instruction, Multiple Data): Parallel architecture using multiple/array processors. Performs the exact same operation simultaneously across multiple different data streams. Takes advantage of pipelining.
MISD (Multiple Instruction, Single Data): Parallel architecture where multiple processors independently perform different operations on the same single data stream simultaneously.
MIMD (Multiple Instruction, Multiple Data): Parallel architecture with many processors that execute different instructions on different data sets asynchronously / independently.
2. SIMD vs. MISD Comparison
Similarities:
Both are parallel processing architectures using multiple processors.
Both may incorporate pipelining.
Neither is suited for pure sequential software (such as compilers or office software).
Differences:
SIMD executes 1 instruction across many data streams; MISD executes many instructions on 1 data stream.
MISD is more complex than SISD architectures.
3. Massively Parallel Computers
Involves linking together an extremely large number (thousands) of processors or computers to form a single powerful system.
All processors work simultaneously on the same problem, with each processor responsible for its assigned part.
Processors communicate via interconnected data networks or shared memory pools.
Module 5: Boolean Algebra, Logic Circuits & Memory Components
1. Half Adder vs. Full Adder
Half Adder:
Purpose: Adds two single binary bits.
Inputs: A, B
Outputs: Sum (E), Carry (F)
Logic Equations:
Sum E = A XOR B [ or A'B + AB' ]
Carry F = A AND B [ or A . B ]
Full Adder:
Purpose: Adds two binary bits plus an incoming Carry bit from a previous addition stage.
Inputs: A, B, Carry-In (C)
Outputs: Sum (Y), Carry-Out (Z)
Sum-of-Products (SoP) Expressions:
Sum Y = A'B'C + A'BC' + AB'C' + ABC (In word form: NOT A AND NOT B AND C OR NOT A AND B AND NOT C OR A AND NOT B AND NOT C OR A AND B AND C)
Carry-Out Z = A'BC + AB'C + ABC' + ABC (In word form: NOT A AND B AND C OR A AND NOT B AND C OR A AND B AND NOT C OR A AND B AND C)
2. SR Flip-Flop
Purpose: Used to store a single binary digit / bit (1-bit memory).
Circuit Construction: Constructed using two cross-connected NAND gates or two cross-connected NOR gates.
Inputs/Outputs: Inputs labeled Set (S) and Reset (R), with outputs Q and NOT Q (Q').
3. Karnaugh Maps (K-Maps)
Benefit: Makes simplifying logic expressions easier without using complex Boolean algebra; guaranteed to produce an optimal sum-of-products solution.
Looping Rules: Group adjacent 1s in powers of 2 (1, 2, 4, 8, 16), wrap loops around grid edges, and make groups as large as possible to eliminate redundant variables.
4. Essential Boolean Algebra Laws
De Morgan's Laws:
(A . B)' = A' + B' (NOT (A AND B) = NOT A OR NOT B)
(A + B)' = A' . B' (NOT (A OR B) = NOT A AND NOT B)
Double Negation Law:
(A')' = A (NOT (NOT A) = A)
Absorption Laws:
A + (A . B) = A
A . (A + B) = A
Distributive Laws:
A . (B + C) = (A . B) + (A . C)
A + (B . C) = (A + B) . (A + C)
Identity & Annulment Laws:
A . 1 = A
A + 1 = 1
A . 0 = 0
A + 0 = A