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.
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.
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.
| Paradigm | Characteristics | Typical use |
|---|---|---|
| Low-level | uses 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 dependent | device 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 variables | general-purpose programs; the style of 9618 pseudocode |
| Object-oriented | data (attributes) and the code that works on it (methods) are bundled into classes; programs create and use objects; encapsulation, inheritance and polymorphism | large programs built by teams, GUIs, games, simulations |
| Declarative | the programmer states facts and rules about the problem, not the steps; a goal (query) is satisfied by the system searching the knowledge base and backtracking | expert systems, AI, database queries (SQL is also declarative) |
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.
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.
| Mode | Instruction | What is loaded into ACC |
|---|---|---|
| Immediate | LDM #n | the number n itself |
| Direct | LDD <address> | the contents of the given address |
| Indirect | LDI <address> | the contents of the address that is stored at the given address |
| Indexed | LDX <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
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Which best describes a programming paradigm?
In the lab, which addressing mode adds the index register to the operand to find the address?
Using the clauses in this lesson, what is the result of the goal needs_goggles(X)?
Which feature is characteristic of imperative (procedural) programming?
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).
| Goal | Result | Why |
|---|---|---|
| studies(X, maths) | X = ana, ben | clauses 02 and 03 match; X is instantiated to each in turn |
| needs_goggles(X) | X = ana, cal, dev | each person whose subject S is practical (physics or chemistry) |
| classmate(ana, Y) | Y = dev, ben | S = physics gives dev; backtracking to S = maths gives ben; ana is excluded by NOT(X = Y) |
| needs_goggles(ben) | false | ben only studies maths, which is not a practical subject |
10 flight(london, paris). 11 flight(paris, rome). 12 flight(rome, athens). 13 flight(london, rome).14 one_change(X, Y) IF flight(X, Z) AND flight(Z, Y).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.
Unlocks once the four checks above are done. Worth more XP, written to A Level Paper 3 and Paper 4 standard.
Address 80 holds 95, address 95 holds 12 and IX holds 0. What does LDI 80 load into ACC?
Clauses: 20 owns(sam, dog). 21 owns(kim, cat). 22 owns(lee, dog). What is returned by owns(X, dog)?
Which statement about declarative programming is correct?
Why is relative addressing useful in low-level programs?
This skill is now recorded on your profile. Keep the chain going.