A Level · Topic 20.1
A-Level Computer Science 9618 / Topic 20 / A2

Many ways to program.

The same problem can be solved in very different styles. Learn what a programming paradigm is, how low-level code uses addressing modes, how imperative (procedural) code builds on structured programming, and how a declarative program states facts and rules and lets the system find answers to a goal.

The key idea

A programming paradigm is a style or approach to programming, with its own concepts and way of structuring a solution. Low-level programs use the processor's instruction set directly, working with registers and memory addresses through addressing modes. Imperative (procedural) programs are sequences of statements that change the program's state (its variables), organised with sequence, selection, iteration and procedures and functions. Object-oriented programs are built from classes and objects. Declarative programs state facts and rules; the programmer gives a goal (query) and the language's inference engine searches the facts and rules to satisfy it. Imperative code says how; declarative code says what.

Four programming paradigms: four ways of telling a computer what to do Low-level Imperative (procedural) Object-oriented Declarative instructions for one processor: registers, addresses, addressing modes LDX 300 ADD TOTAL a sequence of statements that change variables, grouped into procedures Total ← Total + N classes bundle data with methods; objects are instances of classes S ← NEW Circle("C1", 2) facts and rules describe WHAT is true; the system finds HOW to meet a goal parent(X, Y) IF ... Imperative and low-level code say HOW, step by step; declarative code says WHAT
Fig. 1: the four paradigms in the 9618 syllabus, each with a typical line of code
Section 01

What is a paradigm?

A paradigm is not a language: it is a way of thinking about a program. Many languages support more than one paradigm (Python and Java can be written procedurally or in an object-oriented way), but each paradigm has characteristic features you must be able to describe.

ParadigmCharacteristicsTypical use
Low-leveluses the processor's instruction set (machine code or assembly language); works directly with registers, memory addresses and addressing modes; one instruction per processor operation; machine dependentdevice drivers, embedded systems, code that must be very fast or very small
Imperative (procedural)a sequence of statements executed in order that change the values of variables; structured with sequence, selection and iteration; broken into procedures and functions with parameters and local variablesgeneral-purpose programs; the style of 9618 pseudocode
Object-orienteddata (attributes) and the code that works on it (methods) are bundled into classes; programs create and use objects; encapsulation, inheritance and polymorphismlarge programs built by teams, GUIs, games, simulations
Declarativethe programmer states facts and rules about the problem, not the steps; a goal (query) is satisfied by the system searching the knowledge base and backtrackingexpert systems, AI, database queries (SQL is also declarative)
Examiner trap

Do not describe a paradigm by naming a language. "Prolog" earns nothing; "the program is a set of facts and rules, and a goal is satisfied by searching them" earns the mark.

Section 02

Low-level and imperative code.

Low-level programs must handle every detail themselves, including where each item of data is. Use the lab to remind yourself how each addressing mode finds its operand, then compare the same task written at low level and in imperative pseudocode.

ModeInstructionWhat is loaded into ACC
ImmediateLDM #nthe number n itself
DirectLDD <address>the contents of the given address
IndirectLDI <address>the contents of the address that is stored at the given address
IndexedLDX <address>the contents of (given address + contents of IX)
Relative(e.g. a jump)the address is an offset from the address of the current instruction

The program below adds the four numbers stored at addresses 300 to 303 and leaves the total at address 310. Address 311 is used as a loop counter. Indexed addressing (LDX) steps through the list as IX goes 0, 1, 2, 3.

        LDR #0
        LDM #0
        STO 310
        STO 311
LOOP:   LDX 300
        ADD 310
        STO 310
        INC IX
        LDD 311
        INC ACC
        STO 311
        CMP #4
        JPN LOOP
        END

The imperative (procedural) version of the same task works with named variables and a count-controlled loop, and leaves the addresses to the compiler:

DECLARE Nums : ARRAY[1:4] OF INTEGER
DECLARE Total : INTEGER
DECLARE Index : INTEGER
Total ← 0
FOR Index ← 1 TO 4
    Total ← Total + Nums[Index]
NEXT Index
OUTPUT Total
Worked example Trace the low-level loop
Addresses 300 to 303 hold 5, 9, 2 and 7. Trace the low-level program above and state the final contents of address 310.
  1. LDR #0 sets IX to 0; LDM #0 then STO 310 and STO 311 set the total and the counter to 0.
  2. Pass 1: LDX 300 loads the contents of 300 + 0 = 5. ADD 310 gives 5, stored in 310. IX becomes 1. The counter becomes 1; CMP #4 is false, so JPN jumps back to LOOP.
  3. Pass 2: LDX 300 loads the contents of 301 = 9; total 14; IX 2; counter 2; jump back.
  4. Pass 3: loads 2 from 302; total 16; IX 3; counter 3; jump back.
  5. Pass 4: loads 7 from 303; total 23; IX 4; counter 4. CMP #4 is now true, so JPN does not jump and END returns control to the OS.
Answer: Address 310 holds 23. The loop ran four times; the low-level version needed a counter in memory because CMP can only compare the accumulator.
Your turn
Address 150 holds 210, address 151 holds 75, address 210 holds 42 and IX holds 1. What does each instruction load into ACC: LDM #150, LDD 150, LDI 150, LDX 150?
Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which best describes a programming paradigm?

Quick check+10 XP

In the lab, which addressing mode adds the index register to the operand to find the address?

Quick check+10 XP

Using the clauses in this lesson, what is the result of the goal needs_goggles(X)?

Quick check+10 XP

Which feature is characteristic of imperative (procedural) programming?

Section 03

Declarative: facts, rules and goals.

A declarative program is a knowledge base. Facts state what is true; rules state how new facts follow from existing ones. You then ask a goal, and the system searches the clauses in order, matching (instantiating) variables and backtracking to find every answer. In this notation constants start with a lower-case letter and variables with an upper-case letter.

01 studies(ana, physics).
02 studies(ana, maths).
03 studies(ben, maths).
04 studies(cal, chemistry).
05 studies(dev, physics).
06 practical(physics).
07 practical(chemistry).
08 needs_goggles(X) IF studies(X, S) AND practical(S).
09 classmate(X, Y) IF studies(X, S) AND studies(Y, S) AND NOT(X = Y).
GoalResultWhy
studies(X, maths)X = ana, benclauses 02 and 03 match; X is instantiated to each in turn
needs_goggles(X)X = ana, cal, deveach person whose subject S is practical (physics or chemistry)
classmate(ana, Y)Y = dev, benS = physics gives dev; backtracking to S = maths gives ben; ana is excluded by NOT(X = Y)
needs_goggles(ben)falseben only studies maths, which is not a practical subject
Worked example Write facts and rules from information
Direct flights run from london to paris, paris to rome, rome to athens and london to rome. Write facts for the flights, a rule one_change(X, Y) that is true when you can fly from X to Y by changing once, and give the result of the goal one_change(london, Y).
  1. One fact per flight, in the form flight(from, to): 10 flight(london, paris). 11 flight(paris, rome). 12 flight(rome, athens). 13 flight(london, rome).
  2. A change means flying from X to some intermediate city Z, then from Z to Y: 14 one_change(X, Y) IF flight(X, Z) AND flight(Z, Y).
  3. Goal one_change(london, Y): the first flight from london gives Z = paris; flight(paris, Y) gives Y = rome.
  4. Backtrack: the next flight from london gives Z = rome; flight(rome, Y) gives Y = athens.
Answer: Y = rome, athens. The programmer never wrote a loop or a search: the rule says what a one-change journey is, and the system finds them.
Examiner trap

Variables must start with a capital letter and constants with a lower-case letter. Writing studies(Ana, physics) makes Ana a variable, so the clause would be true for anyone.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to A Level Paper 3 and Paper 4 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Address 80 holds 95, address 95 holds 12 and IX holds 0. What does LDI 80 load into ACC?

Exam style+20 XP

Clauses: 20 owns(sam, dog). 21 owns(kim, cat). 22 owns(lee, dog). What is returned by owns(X, dog)?

Exam style+20 XP

Which statement about declarative programming is correct?

Exam style+20 XP

Why is relative addressing useful in low-level programs?

Skill unlocked

Low-level, imperative and declarative paradigms, compared.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
What a paradigm is, low-level code with addressing modes, imperative programming, and declarative facts, rules and goals.