KS5H446 1.2.4 30 min read 10 question quiz 20 exam marks

Types of Programming Language and Assembly

The paradigms you must compare, Little Man Computer, addressing modes, and how to answer a question that asks you to justify a paradigm.

Paradigms

Procedural

Programs are sequences of instructions grouped into procedures and functions, operating on data passed between them. Control is expressed with sequence, selection and iteration.

  • Straightforward to learn and to follow
  • Well suited to problems that are naturally a series of steps
  • Becomes difficult to manage as programs grow, because data and the code acting on it are separate
  • Examples: C, Pascal, and Python used procedurally

Object oriented

Data and the methods that operate on it are bundled into objects created from classes.

  • Encapsulation means an object controls its own state and can guarantee it stays valid
  • Inheritance removes duplication by placing shared behaviour in a superclass
  • Polymorphism lets code work with many types through one interface, so new types need no changes to existing code
  • Scales well to large systems and large teams
  • Adds overhead and complexity that is not worth it for a small script
  • Examples: Java, C#, Python, C++

Assembly and low level

  • One assembly instruction generally corresponds to one machine instruction
  • Direct control over registers and memory addresses
  • Highly efficient in both speed and memory
  • Difficult to write, read and maintain, and specific to one processor architecture
  • Used for device drivers, embedded systems with severe constraints, and performance critical routines

Declarative and functional

The programmer specifies what is wanted rather than how to compute it. Functional languages avoid changing state, which makes concurrency safer and behaviour more predictable. SQL is the declarative language you will meet most often.

Justifying a paradigm

Link it to the scenario. A large system built by a team, with many similar entities and likely future extension, points to object oriented. A short data processing script points to procedural. A device driver with tight timing points to assembly.

Four programming paradigms: procedural, object oriented, declarative and assemblyFour paradigms, one card each. Procedural: a main program calls procedures in order, get scores, average and show, passing data between them. It is good for tasks that are a series of steps. Object oriented: a class, here Pet with a name, an age and a speak method, is used to create objects such as rex and mia. It is good for large systems and teams. Declarative: an SQL query, select name from pupils where year equals 12, says what you want and not how to get it, and returns Aisha, Ben and Chloe. It is good for queries and for safe concurrency. Assembly: each line, such as LDA 5, ADD 6 and STA 7, matches one machine instruction, 505, 106 and 307. It is good for device drivers and tight hardware limits.Procedural1good for: tasks that are a series of stepsmainget_scores()average()show()procedures called in order, data passed between themObject oriented2good for: large systems and teamsPet (class)name age speak()rex: Petage 3 speak()mia: Petage 5 speak()Declarative3good for: queries and safe concurrencySELECT name FROM pupilsWHERE year = 12AishaBenChloesay what you want, not how to get itAssembly4good for: drivers and tight hardware limitsLDA 5505ADD 6106STA 7307assemblymachine codeone assembly instruction, one machine instruction
  1. Procedural: steps grouped into procedures that call each other.
  2. Object oriented: data and methods bundled into objects made from classes.
  3. Declarative: say what is wanted, not how to compute it.
  4. Assembly: one instruction for each machine instruction, with direct control of the hardware.
Justify the paradigm from the scenario: size of the team, how the system will grow, and how close to the hardware it must be.

Little Man Computer and addressing modes

The LMC instruction set

MnemonicCodeMeaning
INP901Input to the accumulator
OUT902Output from the accumulator
LDA5xxLoad the contents of address xx into the accumulator
STA3xxStore the accumulator at address xx
ADD1xxAdd the contents of address xx to the accumulator
SUB2xxSubtract the contents of address xx from the accumulator
BRA6xxBranch always to xx
BRZ7xxBranch to xx if the accumulator is zero
BRP8xxBranch to xx if the accumulator is zero or positive
HLT000Halt
DATDeclare a data location

Reading two numbers and outputting the larger

Assembly
        INP
        STA first
        INP
        STA second
        SUB first        // second minus first
        BRP secondbig    // if zero or positive, second is larger or equal
        LDA first
        OUT
        HLT
secondbig LDA second
        OUT
        HLT
first   DAT
second  DAT

Counting down from a number

Assembly
loop    LDA count
        BRZ done
        OUT
        SUB one
        STA count
        BRA loop
done    HLT
count   DAT 5
one     DAT 1
Tracing LMC

Keep a table with the accumulator, each data location and the output. Work one instruction at a time and never skip a pass of a loop.

Addressing modes

The addressing mode determines how the operand should be interpreted.

ModeMeaningExample
ImmediateThe operand is the valueADD #5 adds the literal 5
DirectThe operand is the address of the valueADD 60 adds the contents of address 60
IndirectThe operand is the address of a location holding the address of the valueADD (60) follows the pointer stored at 60
IndexedThe value's address is the operand plus the contents of an index registerUsed for stepping through arrays

Immediate is fastest since no memory access is needed, but the value is fixed at compile time. Indirect allows pointers and dynamic data structures. Indexed makes array traversal efficient, because only the index register changes between elements.

Little Man Computer

Assemble and run LMC programs and watch the fetch decode execute cycle happen. This tool needs JavaScript, so turn it on to use it. Everything you need to answer the exam question is explained in the text above.

The Little Man Computer: mailboxes, the instruction format, the registers and the input and output traysThe Little Man Computer is a simple model of a processor and memory. The memory is a set of numbered mailboxes, each holding a three digit number that is either an instruction or data. In this program mailbox 00 holds 901, which is INP, mailbox 01 holds 107, which is ADD 07, mailbox 02 holds 902, which is OUT, mailbox 03 holds 000, which is HLT, and mailbox 07 holds the data 5. An instruction is a number split in two: the first digit is the opcode, which says what to do, and the last two digits are the operand, which says which mailbox, so 107 is 1, ADD, with mailbox 07. The processor has a program counter, here 01, which holds the next address, an instruction register, here 107, holding the instruction being carried out, and an accumulator, here 12, which holds the working number. There is an input tray, here 7, and an output tray, here 12.Mailboxes: the memory100901INP01107ADD 0702902OUT03000HLT04050607005DAT 50809programdataA mailbox holds a three digit number: an instruction or data.2Reading an instruction107the number1opcode07operand1 = ADD07 = mailbox 07Registers and trays3Program counter01next addressInstruction register107what it is doingAccumulator12the working numberInput7Output12
  1. Memory is a row of numbered mailboxes. Each holds a three digit number, an instruction or data.
  2. An instruction is an opcode, the first digit, and an operand, the last two digits. 107 means ADD with mailbox 07.
  3. The program counter, instruction register and accumulator do the work, and the input and output trays connect to the outside.
The little man does fetch, decode, execute: read the program counter, fetch that mailbox, then carry out the instruction.
Four addressing modes loading from memory: immediate, direct, indirect and indexedFour ways an instruction can find its data. In immediate addressing, LDA hash 5, the operand is the data itself, so the accumulator gets 5. In direct addressing, LDA 5, the operand is the address of the data: memory address 5 holds 42, so the accumulator gets 42. In indirect addressing, LDA in brackets 5, the operand is the address of the address: memory address 5 holds 8, which is the address of the data, and memory address 8 holds 42. In indexed addressing, LDA 5 comma X, the address is the operand plus the index register: with X equal to 3 the address is 8, which holds 42.Immediate1LDA #5the operand is the data5ACC = 5Direct2LDA 5the operand is theaddress of the data45426789ACC = 42Indirect3LDA (5)the operand is theaddress of the address458678429ACC = 42Indexed4LDA 5,Xaddress = operand +index register (X = 3)45678429X = 35 + 3 = 8
  1. Immediate: the operand is the data.
  2. Direct: the operand is the address of the data.
  3. Indirect: the operand is the address of the address of the data.
  4. Indexed: add the index register to the operand to get the address, which is useful for arrays.
Say what the operand means in each mode: the value, an address, an address of an address, or a base address plus an index.

Key terms you must be able to define

Paradigm
An approach to structuring and thinking about a program, such as procedural or object oriented.
Procedural
A paradigm structuring programs as sequences of instructions grouped into procedures.
Object oriented
A paradigm bundling data with the methods that operate on it, into objects created from classes.
Declarative
A paradigm specifying what is required rather than how to compute it.
Assembly language
A low level language with a near one to one correspondence to machine code.
Little Man Computer
A simplified model processor and instruction set used to teach assembly programming.
Immediate addressing
The operand is the value itself.
Direct addressing
The operand is the address of the value.
Indirect addressing
The operand is the address of a location holding the address of the value.
Indexed addressing
The address is the operand plus the contents of an index register.

Flashcards

Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.

DefineParadigm
AnswerAn approach to structuring and thinking about a program, such as procedural or object oriented.
DefineProcedural
AnswerA paradigm structuring programs as sequences of instructions grouped into procedures.
DefineObject oriented
AnswerA paradigm bundling data with the methods that operate on it, into objects created from classes.
DefineDeclarative
AnswerA paradigm specifying what is required rather than how to compute it.
DefineAssembly language
AnswerA low level language with a near one to one correspondence to machine code.
DefineLittle Man Computer
AnswerA simplified model processor and instruction set used to teach assembly programming.
DefineImmediate addressing
AnswerThe operand is the value itself.
DefineDirect addressing
AnswerThe operand is the address of the value.

Reach the top grade on this topic

  • Justify a paradigm against the scenario, naming a specific feature such as inheritance removing duplication
  • Trace LMC code with a full table showing the accumulator, memory and output
  • Distinguish all four addressing modes and give a use for each
  • Explain indexed addressing in terms of array traversal, which is what it exists for
  • Say why assembly is used despite being hard, rather than treating it as obsolete

Mistakes that cost marks here

Avoid these
  • Saying object oriented is always better. For a fifty line data script it is unnecessary overhead.
  • Confusing direct and indirect addressing. Indirect follows a pointer to a second address.
  • Tracing LMC in your head instead of keeping a table.
  • Forgetting that BRP branches when the accumulator is zero or positive, not strictly positive.
  • Saying assembly is obsolete. It remains essential for drivers and tightly constrained embedded work.
Fun fact

Assembly language has a near one to one relationship with machine code, but that one instruction can still be modified by the addressing mode, which changes whether the operand is a value, an address, or an address of an address.

Knowledge check

0 of 10 answered
1What does immediate addressing mean?
2Which addressing mode is most useful for stepping through an array?
3In LMC, what does BRP do?
4Which paradigm bundles data with the methods that operate on it?
5Which is an advantage of the procedural paradigm for a short data processing script?
6What does indirect addressing allow?
7Why is assembly language still used for device drivers?
8In LMC, what does STA 42 do?
9Which is a disadvantage of the object oriented paradigm?
10SQL is an example of which paradigm?
Score 0 / 10

Exam-style questions

Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.

20 marks
1Explain the difference between immediate and direct addressing.3 marks
Explain

Mark scheme0 / 3
  • LIn immediate addressing the operand is the value to be used
  • LIn direct addressing the operand is the address where the value is stored
  • LImmediate needs no memory access so is faster, but the value is fixed
Model answer

In immediate addressing the operand contained in the instruction is the actual value to be operated on, so an instruction to add five carries the number five within it. In direct addressing the operand is instead a memory address, and the value to be used is whatever is currently stored at that address, so the processor must perform a memory read before it can carry out the operation. Immediate addressing is therefore faster, since no memory access is required, but the value is fixed at the moment the program is written and cannot change while it runs, whereas direct addressing works with data that varies during execution.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
2Explain why indexed addressing is useful when working with arrays.3 marks
Explain

Mark scheme0 / 3
  • LThe address used is the operand plus the contents of an index register
  • LThe base address of the array stays fixed within the instruction
  • LOnly the index register needs to change to move to the next element
Model answer

In indexed addressing the effective address is calculated by adding the operand in the instruction to the contents of a dedicated index register. This maps directly onto how an array works: the operand holds the base address of the array and the index register holds the position within it. To move from one element to the next, only the index register has to be incremented, and the same instruction can then be executed repeatedly inside a loop without any modification. This is far more efficient than the alternative of generating a different instruction for every element or performing separate arithmetic to calculate each address.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
3A company is developing a large system that will be maintained by a team over many years and will need new types of user account added regularly. Justify the use of the object oriented paradigm.6 marks
Justify

Mark scheme0 / 6
  • LRecommends object oriented
  • LEncapsulation keeps data with the methods that act on it and protects its validity
  • LInheritance places shared behaviour in a superclass so it is written once
  • LA new account type is added as a subclass without changing existing code
  • LPolymorphism lets existing code work with new types through the same interface
  • LThese properties make a large system maintainable by a team over time
Model answer

The object oriented paradigm is the right choice here, principally because of how the system will change over its lifetime. Encapsulation bundles each account's data together with the methods that operate on it and restricts direct access from outside, which means the rules governing what a valid account looks like live in one place and are enforced automatically, rather than being scattered through the codebase where a single developer forgetting a check could corrupt the data. Inheritance addresses the requirement for regularly added account types directly: everything common to all accounts is written once in a base Account class, and each specific type becomes a subclass that inherits that behaviour and overrides only what differs. This eliminates duplication, so a correction to shared logic is made in one place and applies everywhere. Polymorphism then means that existing code which processes accounts continues to work unchanged when a new subclass appears, because it calls the same methods and each object supplies its own implementation. Adding a new account type therefore involves writing one new class rather than finding and editing every conditional statement in the system, which is exactly what a procedural design would require. Over a system maintained by a team for many years, these properties are what keep the cost of change roughly constant instead of rising steeply as the codebase grows.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
4The following Little Man Computer program is executed with inputs 7 and 3. State the output and explain how it is produced.INP, STA 90, INP, STA 91, LDA 90, SUB 91, OUT, HLT4 marks
State

Mark scheme0 / 4
  • LThe first input 7 is stored at address 90
  • LThe second input 3 is stored at address 91
  • LThe accumulator is loaded with 7 and 3 is subtracted
  • LThe output is 4
Model answer

The first INP instruction places the value 7 into the accumulator, and STA 90 stores it at address 90. The second INP replaces the accumulator contents with 3, and STA 91 stores that at address 91. LDA 90 then loads the contents of address 90, which is 7, back into the accumulator, and SUB 91 subtracts the contents of address 91, which is 3, leaving 4 in the accumulator. OUT displays the accumulator, so the output is 4, and HLT stops the program.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
5Explain one situation in which assembly language would be preferred over a high level language, and one disadvantage of that choice.4 marks
Explain

Mark scheme0 / 4
  • LGives a valid situation such as a device driver or a tightly constrained embedded system
  • LAssembly gives direct control over registers, memory and timing
  • LDisadvantage: it is far harder to write, read and maintain
  • LDisadvantage: it is specific to one processor architecture so it is not portable
Model answer

Assembly language would be preferred when writing a device driver or the control software for an embedded system with severe constraints. In those situations the programmer needs direct control over specific hardware registers and memory locations, and needs to know exactly how many cycles a sequence of instructions will take, neither of which a high level language and its compiler can guarantee. Assembly also allows the code to be made extremely compact, which matters when the whole program must fit into a few kilobytes of ROM. The disadvantages are substantial. Assembly is far harder to write, read and maintain than a high level language, because a single statement in a high level language may correspond to a dozen assembly instructions with no self documenting structure, so development takes much longer and errors are both more likely and harder to find. It is also written against one specific processor's instruction set, so the code is not portable and moving to different hardware means rewriting it entirely rather than recompiling.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
Machine mark 0 / 20