The Von Neumann model
Early computers had to be rewired for each new job. In a computer built on the Von Neumann model, the memory holds the program and the data it works on, so a new job only needs a new program loaded. It has one processor (CPU), a main memory and input/output devices, linked by three buses.
Find the program and its data together in main memory: addresses 200 to 203 hold the instructions, 230 to 232 the numbers they work on. Then find the three buses that join every part.
Components of the processor and memory
Every step of the fetch–execute cycle uses these parts, so a trace only makes sense once you know each one. Be able to say what each part does, in one line.
| Component | Role |
|---|---|
| Control unit (CU) | decodes instructions and sends control signals to coordinate the other components |
| Arithmetic and logic unit (ALU) | carries out arithmetic (add, subtract) and logic operations (AND, OR, compare) |
| System clock | produces regular timing signals; the control bus takes them to every component so they work in step (synchronise) |
| Immediate access store (IAS) | the main memory the processor reads directly: holds the programs and data being run; fast access; volatile |
Stored program concept
- A program’s instructions and the data they work on are both held in main memory.
- The processor fetches each instruction from memory and executes it, one at a time, in sequence.
- To run another program, load it into memory.
Do not confuse
- The IAS is the memory the processor addresses directly (main memory); registers are inside the processor.
- A bus carries signals; nothing stays on it.
- The CU decodes and coordinates; the ALU calculates.
1 Where are a program’s instructions and its data stored?
2 Which component decodes instructions, and which one carries out calculations?
3 Name the three buses that link the processor, memory and input/output devices.
- 1both in main memory (the IAS)
- 2the control unit (CU) decodes; the ALU calculates
- 3the address, data and control buses
Registers
Main memory is too slow to visit at every step, so the processor keeps the values it is using close at hand. A register is a very small, very fast store inside the processor. Most hold one value, such as an address, an instruction or a number; the SR holds separate flag bits.
The registers in the processor
Learn each register by the kind of thing it holds: an address, a value, or the instruction itself. Mixing up an address with the value stored there is the commonest slip.
| Register | What it holds |
|---|---|
| Program Counter (PC) | the address where the instruction to fetch next is stored |
| Memory Address Register (MAR) | the address of the location about to be read or written: in memory or an I/O device |
| Memory Data Register (MDR) | the data or instruction just read from memory, or about to be written there, at the MAR’s address |
| Current Instruction Register (CIR) | the instruction that is now being decoded or executed |
| Accumulator (ACC) | the values a program works on and each intermediate result of a calculation |
| Index Register (IX) | a number added to the operand of an indexed instruction (LDX); the sum is the address used |
| Status Register (SR) | single-bit flags, set or cleared by an arithmetic or logic result or by an interrupt; testing a flag can change which instruction runs next |
General purpose or special purpose?
| General purpose | free for any value the program needs while it computes, such as a running total |
| Special purpose | does one fixed job for the processor itself, such as holding the next address or the flags (PC, MAR, MDR, CIR, IX, SR); the named accumulator is also special purpose in architectural classification |
Worked example: flags
Add the two bytes column by column, then read each flag from the result.
+150 does not fit in 8 bits as a signed number, so the sign bit comes out wrong and the overflow flag is set.
1 Which register holds the address of the next instruction to fetch?
2 LDX 300 runs while IX holds 4. Which address does it read?
3 After a memory read, which register holds the value that was read?
- 1the Program Counter (PC)
- 2300 + 4 = 304
- 3the Memory Data Register (MDR); the MAR holds its address
Buses: moving data between components
The processor and the memory are separate parts, so every value has to travel between them. The system bus is the address, data and control wires that link the processor, main memory and I/O devices. Every memory access uses all three: an address, the data and a control signal.
Follow the numbers in each panel: the address goes out first, the control signal says read or write, then the value moves on the data bus.
Address, data and control buses
For each bus, be able to say what it carries and which way.
| Bus | Carries | Direction |
|---|---|---|
| Address bus | the address, from the MAR, of the location or I/O device to read or write | one-way: from the CPU to memory and I/O |
| Data bus | the data or instructions being transferred | two-way: to and from the CPU |
| Control bus | control and timing signals, e.g. memory read, memory write, clock pulses, interrupts | two-way: e.g. read/write out from the CU, interrupts in |
One read needs all three buses at once, kept in step by the clock. Trace the ticket price at address 240 from each cause to the transfer it allows.
Reading the ticket price at address 240
| Cause | Transfer it enables |
|---|---|
| Clock emits regular timing signals | signals on the control bus keep CPU and memory synchronised |
| CU sends a read signal | memory uses the address bus value 240 to select the price |
| Memory responds at the timed stage | price 63 travels on the data bus into the MDR |
Do not confuse
- The MAR drives the address bus; the MDR sends and receives on the data bus; the CU and clock use the control bus.
- A read or write signal travels on the control bus; the value it moves travels on the data bus.
- The address bus is one-way: CPU to memory or I/O, never back. An address can still travel on the data bus as data.
1 Which bus carries the address 240 from the MAR to memory?
2 Which of the three buses is one-way?
3 The CPU writes 17 to address 240. Which bus carries 17, and which carries the write signal?
- 1the address bus
- 2the address bus: from the CPU to memory and I/O only
- 317 travels on the data bus; the write signal travels on the control bus
Performance of the computer system
Two computers can run the same program at very different speeds. Performance is how much processing a computer completes in a given time; each factor raises it through one mechanism.
Bus width and cache, drawn. Count the transfers each bus width needs for one 16-bit item, then follow a request to the cache: it only goes on to the slower RAM on a miss.
Factors that affect performance
Link each factor to how it makes the computer faster.
| Factor | Why increasing it can improve performance |
|---|---|
| Clock speed | more timing pulses per second can support more F-E cycles each second: 1.5 GHz → 3 GHz is 3 000 000 000 instead of 1 500 000 000 pulses a second |
| Number of cores | every core runs its own instructions at the same time as the others (in parallel), so suitable work can be split between cores; speed-up depends on the program |
| Data bus width | more bits move in each transfer, so a large item needs fewer transfers and the processor waits less for data |
| Address bus width | more address bits, so more memory locations can be addressed directly and more programs and data fit in memory: 16 bits give 2¹⁶ = 65 536, 32 bits give 2³² = 4 294 967 296 |
| Cache memory | a larger cache holds more of the frequently used instructions and data in fast memory beside the CPU, so it reads from the slower RAM less often |
Four cores sounds like four times the speed, but it rarely is. The school’s upgrade shows what holds the gain back.
Why four cores need not halve the time
| School's upgrade | What limits the gain |
|---|---|
| 3 GHz dual-core → 3 GHz quad-core | same clock rate; more cores can share parallel work |
| One-core program | extra cores stay idle for that task |
| Results shared between cores | coordination adds overhead |
| Unchanged memory supply | extra cores can still wait for data |
Do not confuse
- A wider data bus moves more bits per transfer; a wider address bus reaches more locations.
- Cache is smaller but faster than RAM; it holds copies of frequently used items.
1 A 64-bit item is moved over a 16-bit data bus, then over a 32-bit one. How many transfers does each need?
2 An address bus has 20 lines. How many memory locations can it address?
3 A program runs on one core only. Why does a quad-core processor not make it faster?
- 14 transfers then 2
- 22²⁰ = 1 048 576 locations
- 3the other cores have no work from that program, so they stay idle
Ports: connecting peripheral devices
A keyboard, a projector and a memory stick all plug into the same computer, each into its own socket. A port is the physical interface on the computer that a peripheral device connects to. Each port standard defines the connector and the signals it carries.
Follow each device from plug to result: USB finds the right driver by itself, and the two projector routes show why HDMI needs one cable where VGA needs two.
USB, HDMI and VGA
For each port, know how it connects and one device it suits. The comparison that matters most is HDMI against VGA.
| Port | How it provides the connection | Typical use |
|---|---|---|
| USB (Universal Serial Bus) | serial: one bit at a time; a universal standard connector; plug and play: the OS detects the device and loads its driver | keyboard, mouse, printer, flash drive |
| HDMI (High Definition Multimedia Interface) | uncompressed digital video and audio on one cable; high bandwidth; less prone to interference | monitor, TV, projector |
| VGA (Video Graphics Array) | analogue; video only, so sound needs a separate cable; a lower transfer rate than HDMI | older monitor or projector |
Matching the projector’s picture and sound
A hall projector shows 3840 × 2160 pixels and has a built-in speaker.
- Bandwidth: HDMI’s higher transfer rate suits the 8 294 400-pixel frames.
- Picture and sound: one HDMI cable carries both; VGA needs a separate sound cable.
- Digital signal: no analogue conversion, avoiding that source of quality loss.
Do not confuse
USB, HDMI and VGA are ports (interfaces), not devices. A flash drive is the storage; the USB port is only how it connects.
1 Which port carries video and audio on one cable?
2 What does plug and play mean for a USB device?
3 A projector has only a VGA port. What else does it need to play the sound?
- 1HDMI
- 2the operating system detects the device and loads its driver automatically
- 3a separate audio cable: VGA carries analogue video only
The fetch-execute cycle
A processor works one instruction at a time, billions a second. For each, it repeats one cycle: fetch it from memory, decode it, execute it. Take the PC holding 200, with LDD 230 stored at 200.
Follow the black numbers: 1 to 4 fetch LDD 230 into the CIR, 5 decodes it and 6 executes it, bringing 9 into the ACC.
The fetch stage in register transfer notation
Learn the four fetch steps in this order, written exactly in this notation.
| Step | Register transfer notation | What happens |
|---|---|---|
| 1 | MAR ← [PC] | copy the PC’s address into the MAR |
| 2 | PC ← [PC] + 1 | add 1 to the PC (it is incremented), ready for the next fetch |
| 3 | MDR ← [[MAR]] | copy what is stored at the MAR’s address into the MDR |
| 4 | CIR ← [MDR] | pass the MDR’s contents, the instruction, to the CIR |
- Decode: the CU interprets the instruction in the CIR: its opcode and its operand.
- Execute: the instruction is carried out. For
LDD 230, address 230 goes to the MAR, 9 is read into the MDR, then copied to the ACC. - A jump instruction puts a new address into the PC, replacing the incremented value.
Decode: the CU interprets the instruction in the CIR (its opcode and operand). Execute: the instruction is carried out. For LDD 230 the address 230 goes to the MAR, the value 9 is read into the MDR and copied to the ACC. A jump instruction puts a new address into the PC, replacing the incremented value.
Reading the notation
←: the value on the right is copied into the register on the left.[PC]: what the PC holds.[[MAR]]: what is stored in the location whose address is in the MAR.- A semicolon joins two transfers made at the same time, such as steps 2 and 3.
Spot the error
| MAR ← [[PC]] | [PC] is already the address; double brackets would read memory |
| CIR ← [[MDR]] | the MDR already holds the instruction: use [MDR] |
| CIR ← [MAR] | the CIR is loaded from the MDR |
1 Write the fetch stage in register transfer notation, in order.
2 The PC holds 350 at the start of a fetch. What does it hold at the end of the fetch?
3 Correct this step: CIR ← [MAR].
- 1
MAR ← [PC];PC ← [PC] + 1;MDR ← [[MAR]];CIR ← [MDR] - 2351: it is incremented once
- 3
CIR ← [MDR]: the instruction is copied from the MDR
[[MAR]], what is stored at that address); the PC is incremented.Interrupts: purpose and causes
A printer runs out of paper halfway through a job, while the processor is busy with something else. An interrupt is a signal from a device or a program seeking the attention of the processor. The processor can then deal with an event when it happens, instead of waiting for it or checking for it.
Causes
Sort each cause by where it starts: a device or a program.
Hardware interrupts: from a device
- a disk or other device finishes a transfer
- a timer signal
- a key is pressed, or a device is plugged in
- a device’s buffer is empty or full (below)
- a hardware fault, such as a power failure
Software interrupts: from a program
- division by zero
- a program asks the OS for a service, such as input or output
- a program reads past the end of an array
- a program tries to use memory it has not been given
An interrupt in use: playing sound through a buffer
The sound card plays at a steady rate, but the processor sends data in bursts. Follow the orange loop: the buffer itself raises each interrupt, so the processor never has to keep checking it.
Applications
- Input: a key press requests processing without continual polling.
- Output: the sound card’s buffer is empty or full (above).
- Errors: division by zero transfers control to an error handler; the program may be stopped safely.
- Timer: the OS regains control at regular intervals.
Interrupt flags
An interrupt sets a flag in the interrupt register. Each flag stands for one type of interrupt, so the processor can identify the source. Interrupt types can have different priorities.
1 Hardware or software interrupt: a printer runs out of paper?
2 Hardware or software interrupt: a program tries to divide by zero?
3 Why is an interrupt better than the processor checking a device over and over?
- 1hardware: it comes from a device
- 2software: it comes from the program
- 3the processor can do other work until the event happens, instead of wasting time checking
Detecting and handling an interrupt
When the greenhouse door opens, the processor must leave its temperature logging, deal with the door, then carry on exactly where it stopped. An interrupt service routine (ISR), or interrupt handler, is the program that deals with one type of interrupt. The processor only checks for interrupts between instructions.
Start at the top box and follow the arrows round. The door sensor sets a flag while a logging instruction runs; the PC and ACC boxes show what the stack keeps safe until logging resumes.
Why temperature logging can resume
| Preserved state | Why it matters after the door sensor's ISR |
|---|---|
| PC | restores the address of the next logging instruction |
| Other registers | restore intermediate values and flags the logging program was using |
| Stack copy | keeps the interrupted state while the ISR records that the greenhouse door is open |
1 When does the processor check whether an interrupt flag is set?
2 Why are the PC and the other registers saved on the stack?
3 A new interrupt has a lower priority than the current task. What happens?
- 1once every cycle, after the execute stage and before the next fetch
- 2so the interrupted program can resume where it stopped, with its own values
- 3the current task carries on; the interrupt waits
The instruction set and its groups
Each instruction a processor can follow is tiny: load a number, add, compare, jump. An instruction is an opcode, what to do, and usually an operand, what to do it with. The syllabus sorts its instructions into five groups by what they do.
Bit manipulation instructions come later, in §4.3. In this set the ACC is the only general purpose register; # is denary, B binary, & hexadecimal.
Read the table one grey band at a time: each band is a group, with what its instructions are for. Within a group, check which operand each opcode takes.
| Opcode | Operand | Explanation |
|---|---|---|
| Data movement · copy values between memory and registers | ||
| LDM | #n | Immediate addressing. Load the number n to ACC |
| LDD | <address> | Direct addressing. Load the contents of the location at the given address to ACC |
| LDI | <address> | Indirect addressing. The address to be used is at the given address. Load the contents of this second address to ACC |
| LDX | <address> | Indexed addressing. Form the address from <address> + the contents of the index register. Copy the contents of this calculated address to ACC |
| LDR | #n | Immediate addressing. Load the number n to IX |
| MOV | <register> | Move the contents of the accumulator to the given register (IX) |
| STO | <address> | Store the contents of ACC at the given address |
| Input and output of data · read a character in; show a character | ||
| IN | Key in a character and store its ASCII value in ACC | |
| OUT | Output to the screen the character whose ASCII value is stored in ACC | |
| Arithmetic operations · add or subtract; INC and DEC change a register by 1 | ||
| ADD | <address> | Add the contents of the given address to the ACC |
| ADD | #n/Bn/&n | Add the number n to the ACC |
| SUB | <address> | Subtract the contents of the given address from the ACC |
| SUB | #n/Bn/&n | Subtract the number n from the ACC |
| INC | <register> | Add 1 to the contents of the register (ACC or IX) |
| DEC | <register> | Subtract 1 from the contents of the register (ACC or IX) |
| Unconditional and conditional instructions · choose the next instruction: always (JMP) or after a compare (JPE, JPN) | ||
| JMP | <address> | Jump to the given address |
| JPE | <address> | Following a compare instruction, jump to <address> if the compare was True |
| JPN | <address> | Following a compare instruction, jump to <address> if the compare was False |
| Compare instructions · test the ACC against a value and set a flag | ||
| CMP | <address> | Compare the contents of ACC with the contents of <address> |
| CMP | #n | Compare the contents of ACC with number n |
| CMI | <address> | Indirect addressing. The address to be used is at the given address. Compare the contents of ACC with the contents of this second address |
| and END, which ends the program | ||
| END | Return control to the operating system | |
<address> is absolute (230) or symbolic (COUNT). <label>: <opcode> <operand> labels an instruction; <label>: <data> gives a symbolic address to the memory location with contents <data>.CMP leaves the ACC unchanged: it sets a flag that JPE / JPN test. JPN jumps when the compare was False: the values differ.1 Which group is CMP in?
2 Write one instruction that loads the number 25 into the ACC.
3 Write one instruction that jumps to address 120 when the last compare was True.
- 1compare instructions
- 2
LDM #25 - 3
JPE 120
JPN 150.The two-pass assembler
Nobody wants to write a program as long rows of 0s and 1s. Machine code is the binary the processor executes directly. Assembly language gives each opcode a mnemonic and each operand a number or label. One assembly instruction becomes one machine code instruction, and each processor has its own instruction set. An assembler translates it.
Follow one short program through the assembler: the source code, the address pass 1 gives each line, and the symbol table it builds. Pass 2 then swaps each highlighted label for its address.
// 1 if the key is Y, else 0 IN CMP YES JPE SETONE LDM #0 JMP SAVE SETONE: LDM #1 SAVE: STO ANSWER END YES: 89 ANSWER: 0
Pass 1 gives addresses · pass 2 replaces labels
| ADDR | SOURCE | LABELS REPLACED | |
|---|---|---|---|
| 100 | IN | IN | |
| 101 | CMP YES | CMP 108 | |
| 102 | JPE SETONE | JPE 105 | |
| 103 | LDM #0 | LDM #0 | |
| 104 | JMP SAVE | JMP 106 | |
| 105 | SETONE: | LDM #1 | LDM #1 |
| 106 | SAVE: | STO ANSWER | STO 109 |
| 107 | END | END | |
| 108 | YES: | 89 | 89 |
| 109 | ANSWER: | 0 | 0 |
Symbol table
| Label | Address |
|---|---|
| SETONE | 105 |
| SAVE | 106 |
| YES | 108 |
| ANSWER | 109 |
built in pass 1
What each pass does
The assembler reads the program twice, and each reading has its own jobs. Be able to say which pass does each task.
| Task | First pass | Second pass |
|---|---|---|
| Read the source code line by line, from the top | ✓ | ✓ |
| Strip out comments and extra spaces | ✓ | |
| Look up each mnemonic to check it is a valid opcode | ✓ | |
| Keep a count of addresses; record each label with its address in the symbol table | ✓ | |
| Swap each mnemonic for its binary opcode | ✓ | |
Swap each label for its address from the symbol table: ANSWER → 109 | ✓ | |
Output the object code in binary; an address operand, e.g. YES → 108 → 0110 1100, is only part of an instruction | ✓ |
JPE SETONE and STO ANSWER use labels defined later (forward references): when the assembler first reads them, their addresses are not yet known, so pass 1 records every label’s address before pass 2 uses them.1 What does the first pass of the assembler build?
2 A program starts at address 300. A label is on its fourth line. What address does the symbol table give the label?
3 Why can a one-pass assembler not translate JPE SETONE when it first reads it?
- 1the symbol table: each label with its address
- 2303 (300, 301, 302, then 303)
- 3SETONE is defined later in the program, so its address is not yet known (a forward reference)
Addressing modes
The operand 81 might mean the number 81, the value stored at address 81, or somewhere else again. An addressing mode says how the operand leads to the value the instruction uses.
Each column starts from one instruction and follows its operand down to the ACC: one step for immediate, two for direct, three for indirect and indexed.
Five modes of addressing
For each mode, be able to say what the operand is.
| Mode | How the operand is used |
|---|---|
| Immediate | the value itself is the operand: LDM #120 loads 120 |
| Direct | the operand gives the address of the value |
| Indirect | the operand gives the address of a location that holds the value’s address |
| Indexed | operand + IX is the address of the value |
| Relative | the operand is an offset: the address is that many locations away from the address of the current instruction |
Relative addressing: why it is used
- A target is written as current instruction + offset.
- So the program is relocatable: it runs wherever it is loaded.
- At 400, offset +14 reaches 414. Loaded at 700, the same offset reaches 714, with no change to the code.
Not modes: absolute (230) and symbolic (COUNT) are ways of writing an address.
Worked example: ACC after each set
Memory: 50 → 53, 51 → 4, 52 → 50, 53 → 12, 54 → 7. IX = 1 before each set.
| LDM #52 | 52 | LDX 52 | 12 |
| LDD 52 | 50 | LDR #4 · LDX 50 | 7 |
| LDI 52 | 53 | LDD 51 · ADD #6 | 10 |
1 Memory: 80 holds 82, 81 holds 5, 82 holds 9, 83 holds 4. IX holds 1. Give the ACC after LDD 81.
2 Give the ACC after LDI 80.
3 Give the ACC after LDX 82.
4 Give the ACC after LDM #81.
- 15: the value stored at 81
- 29: 80 holds 82, and 82 holds 9
- 34: 82 + 1 = 83, which holds 4
- 481: the operand itself
Tracing a program
An assembly program is hard to understand by reading it; you have to run it in your head. A trace table records what a program does as it runs. The first row holds the starting contents of memory; each later row is one executed instruction.
Read the program and the starting memory first: 300 holds the running total, 301 the 7 to add and 302 the number of passes left.
200LDD 300 201ADD 301 202STO 300 203LDD 302 204DEC ACC 205STO 302 206CMP #0 207JPN 200 208END
What the program does
Each pass adds 7 (address 301) to the total in 300 and counts 302 down by 1. When 302 reaches 0, JPN 200 does not jump, so the loop ends with 7 × 2 = 14 in 300.
If address 200 changes to LDM #300, each pass reloads the literal 300. Each stores 300 + 7 = 307: the final total is 307, rather than 14.
Memory at the start
| Address | 300 | 301 | 302 |
|---|---|---|---|
| CONTENTS | 0 | 7 | 2 |
Follow one pass, line by line
Run each instruction in turn and write the value it sets in its own column. Follow the ACC down its column: it becomes 7, is stored in 300, then is reloaded with the count from 302 and counted down.
| Address | Statement that runs | ACC | 300 | 301 | 302 | Writes | What happens |
|---|---|---|---|---|---|---|---|
| 200 | LDD 300 | 0 | ACC 0 | load the value stored at 300: 0 | |||
| load the value stored at 300: 0 | |||||||
| 201 | ADD 301 | 7 | ACC 7 | 0 + 7 = 7 | |||
| 0 + 7 = 7 | |||||||
| 202 | STO 300 | 7 | 300 7 | copy the ACC (7) into 300; the ACC keeps 7 | |||
| copy the ACC (7) into 300; the ACC keeps 7 | |||||||
| 203 | LDD 302 | 2 | ACC 2 | load the value stored at 302: 2 | |||
| load the value stored at 302: 2 | |||||||
| 204 | DEC ACC | 1 | ACC 1 | 2 − 1 = 1 | |||
| 2 − 1 = 1 | |||||||
| 205 | STO 302 | 1 | 302 1 | copy the ACC (1) into 302; the ACC keeps 1 | |||
| copy the ACC (1) into 302; the ACC keeps 1 | |||||||
| 206 | CMP #0 | test is 1 equal to 0? False | |||||
| test is 1 equal to 0? False | |||||||
| 207 | JPN 200 | jump the compare was False, so jump to 200 | |||||
| jump the compare was False, so jump to 200 | |||||||
The whole run, pass by pass
Each row lists the values written in one pass, in order. The last column says why the loop goes round again or stops.
| ACC | 300 | 301 | 302 | What decided it | |
|---|---|---|---|---|---|
| start | 0 | 7 | 2 | memory before the program runs | |
| memory before the program runs | |||||
| pass 1 | 0721 | 7 | 1 | 302 is now 1: not 0, so JPN 200 jumps back | |
302 is now 1: not 0, so JPN 200 jumps back | |||||
| pass 2 | 71410 | 14 | 0 | 302 is now 0: 0, so JPN does not jump and END stops the program | |
302 is now 0: 0, so JPN does not jump and END stops the program | |||||
Write it in the exam’s layout
An exam trace table has one row for every instruction executed. Leave a cell blank when it does not change.
Trace table violet address: jump taken · shaded: value stored
| Instruction address | ACC | Memory address | ||
|---|---|---|---|---|
| 300 | 301 | 302 | ||
| 0 | 7 | 2 | ||
| 200 | 0 | |||
| 201 | 7 | |||
| 202 | 7 | |||
| 203 | 2 | |||
| 204 | 1 | |||
| 205 | 1 | |||
| 206 | ||||
| 207 | ||||
| 200 | 7 | |||
| 201 | 14 | |||
| 202 | 14 | |||
| 203 | 1 | |||
| 204 | 0 | |||
| 205 | 0 | |||
| 206 | ||||
| 207 | ||||
| 208 | ||||
How to fill a trace table
CMP and jumps leave the ACC and these memory locations unchanged, so their rows hold only the instruction address.STO changes a memory column, not the ACC; LDD changes the ACC, not memory.END, or where the question tells you to stop.200LDD 300 201ADD 301 202STO 300 203LDD 302 204DEC ACC 205STO 302 206CMP #0 207JPN 200 208END
Memory at the start
| Address | 300 | 301 | 302 |
|---|---|---|---|
| CONTENTS | 0 | 7 | 2 |
What the program does
Each pass adds 7 (address 301) to the total in 300 and counts 302 down by 1. When 302 reaches 0, JPN 200 does not jump, so the loop ends with 7 × 2 = 14 in 300.
If address 200 changes to LDM #300, each pass reloads the literal 300. Each stores 300 + 7 = 307: the final total is 307, rather than 14.
Trace table violet address: jump taken · shaded: value stored
| Instruction address | ACC | Memory address | ||
|---|---|---|---|---|
| 300 | 301 | 302 | ||
| 0 | 7 | 2 | ||
| 200 | 0 | |||
| 201 | 7 | |||
| 202 | 7 | |||
| 203 | 2 | |||
| 204 | 1 | |||
| 205 | 1 | |||
| 206 | ||||
| 207 | ||||
| 200 | 7 | |||
| 201 | 14 | |||
| 202 | 14 | |||
| 203 | 1 | |||
| 204 | 0 | |||
| 205 | 0 | |||
| 206 | ||||
| 207 | ||||
| 208 | ||||
How to fill a trace table
CMP and jumps leave the ACC and these memory locations unchanged, so their rows hold only the instruction address.STO changes a memory column, not the ACC; LDD changes the ACC, not memory.END, or where the question tells you to stop.1 Address 302 starts at 3 instead of 2. How many times does the loop run?
2 What is stored in 300 when the program ends?
3 How many rows does the exam trace table need below the starting row?
- 13 times: 302 counts 3, 2, 1, 0
- 221: 7 + 7 + 7
- 325: 8 instructions in each of 3 passes, then
END
STO sets a memory column, a load sets the ACC.Tracing with the index register
Many programs work along a list in memory, one location at a time. Counting the index register down steps back through the list, and the loop outputs a character each time round, so the trace needs IX and Output columns.
The list sits at 500 to 502. IX starts at 2, so the first read is 502, and each pass steps one place back.
100LDR #2 101LDX 500 102CMP #0 103JPE 108 104ADD #48 105OUT 106DEC IX 107JMP 101 108END
Output
The program outputs 96: the numbers stored at 502 and 501, last first, written as characters. The 0 at 500 ends the loop.
Memory at the start
| Address | 500 | 501 | 502 |
|---|---|---|---|
| CONTENTS | 0 | 6 | 9 |
Follow one pass, line by line
Add IX to the operand before reading memory, then follow the ACC down its column: 9 is read, and adding 48 makes the code 57. Write the character, not its code, under Output.
| Address | Statement that runs | ACC | IX | Output | Writes | What happens |
|---|---|---|---|---|---|---|
| 101 | LDX 500 | 9 | ACC 9 | read address 500 + IX = 500 + 2 = 502: 9 | ||
| read address 500 + IX = 500 + 2 = 502: 9 | ||||||
| 102 | CMP #0 | test is 9 equal to 0? False | ||||
| test is 9 equal to 0? False | ||||||
| 103 | JPE 108 | jump the compare was False, so no jump: go on to 104 | ||||
| jump the compare was False, so no jump: go on to 104 | ||||||
| 104 | ADD #48 | 57 | ACC 57 | 9 + 48 = 57 | ||
| 9 + 48 = 57 | ||||||
| 105 | OUT | 9 | Output 9 | output the character whose code is 57: 9 | ||
| output the character whose code is 57: 9 | ||||||
| 106 | DEC IX | 1 | IX 1 | 2 − 1 = 1 | ||
| 2 − 1 = 1 | ||||||
| 107 | JMP 101 | jump always jump to 101 | ||||
| jump always jump to 101 | ||||||
The whole run, pass by pass
Each row lists the values written in one pass, in order. The last column says what the loaded value decided.
| ACC | 500 | 501 | 502 | IX | Output | What decided it | |
|---|---|---|---|---|---|---|---|
| start | 0 | 6 | 9 | memory before the program runs | |||
| memory before the program runs | |||||||
| set up | 2 | LDR #2 puts 2 in IX | |||||
LDR #2 puts 2 in IX | |||||||
| pass 1 | 957 | 1 | 9 | reads 500 + 2 = 502: 9 is not 0, so add 48 and output 9 | |||
| reads 500 + 2 = 502: 9 is not 0, so add 48 and output 9 | |||||||
| pass 2 | 654 | 0 | 6 | reads 500 + 1 = 501: 6 is not 0, so add 48 and output 6 | |||
| reads 500 + 1 = 501: 6 is not 0, so add 48 and output 6 | |||||||
| pass 3 | 0 | reads 500 + 0 = 500: 0, so JPE 108 jumps to END | |||||
reads 500 + 0 = 500: 0, so JPE 108 jumps to END | |||||||
Write it in the exam’s layout
One row for every instruction executed. Memory columns 500 to 502 only hold their starting values: this program never stores.
Trace table violet address: jump taken · shaded: value stored
| Instruction address | ACC | Memory address | IX | Output | ||
|---|---|---|---|---|---|---|
| 500 | 501 | 502 | ||||
| 0 | 6 | 9 | ||||
| 100 | 2 | |||||
| 101 | 9 | |||||
| 102 | ||||||
| 103 | ||||||
| 104 | 57 | |||||
| 105 | 9 | |||||
| 106 | 1 | |||||
| 107 | ||||||
| 101 | 6 | |||||
| 102 | ||||||
| 103 | ||||||
| 104 | 54 | |||||
| 105 | 6 | |||||
| 106 | 0 | |||||
| 107 | ||||||
| 101 | 0 | |||||
| 102 | ||||||
| 103 | ||||||
| 108 | ||||||
Points to watch
LDX 500reads address 500 + IX: 502, then 501, then 500.DEC IXchanges the IX column, not the ACC.ADD #48turns the number 9 into 57, the ASCII code of the character9.OUToutputs the character whose code is in the ACC: 57 gives9.- When 0 is loaded from 500,
CMP #0is True, soJPE 108jumps toEND.
100LDR #2 101LDX 500 102CMP #0 103JPE 108 104ADD #48 105OUT 106DEC IX 107JMP 101 108END
Memory at the start
| Address | 500 | 501 | 502 |
|---|---|---|---|
| CONTENTS | 0 | 6 | 9 |
Output
The program outputs 96: the numbers stored at 502 and 501, last first, written as characters. The 0 at 500 ends the loop.
Trace table violet address: jump taken · shaded: value stored
| Instruction address | ACC | Memory address | IX | Output | ||
|---|---|---|---|---|---|---|
| 500 | 501 | 502 | ||||
| 0 | 6 | 9 | ||||
| 100 | 2 | |||||
| 101 | 9 | |||||
| 102 | ||||||
| 103 | ||||||
| 104 | 57 | |||||
| 105 | 9 | |||||
| 106 | 1 | |||||
| 107 | ||||||
| 101 | 6 | |||||
| 102 | ||||||
| 103 | ||||||
| 104 | 54 | |||||
| 105 | 6 | |||||
| 106 | 0 | |||||
| 107 | ||||||
| 101 | 0 | |||||
| 102 | ||||||
| 103 | ||||||
| 108 | ||||||
Points to watch
LDX 500reads address 500 + IX: 502, then 501, then 500.DEC IXchanges the IX column, not the ACC.ADD #48turns the number 9 into 57, the ASCII code of the character9.OUToutputs the character whose code is in the ACC: 57 gives9.- When 0 is loaded from 500,
CMP #0is True, soJPE 108jumps toEND.
1 Memory 501 now holds 4 and 502 holds 1; 500 still holds 0. What does the program output?
2 What does IX hold when the program ends?
3 What is in the ACC just after ADD #48 in pass 1?
- 114: 502 first, then 501
- 20
- 349: 1 + 48, the code of the character 1
OUT with 57 in the ACC outputs 9. With indexed addressing, add IX to the operand before you read memory.Logical shifts
Move every digit of a denary number one place left and it is ten times bigger; in binary it doubles. A binary shift moves every bit in a register n places left or right. In a logical shift the bits that move off the end are lost and zeros fill the empty places.
Follow each bit along its arrow: on LSL #2 the two highest bits fall off, two orange 0s enter, and 45 becomes 180.
| Opcode | Operand | Explanation |
|---|---|---|
| LSL | #n | Bits in ACC are shifted logically n places to the left. Zeros are introduced on the right hand end |
| LSR | #n | Bits in ACC are shifted logically n places to the right. Zeros are introduced on the left hand end |
Worked results: 8-bit ACC
Shift the bits, then check the effect on the number. The last row is the warning: a 1 falls off the end, so doubling 150 fails.
| ACC before | Instruction | ACC after | Effect on an unsigned integer |
|---|---|---|---|
| 0011 0110 | LSL #2 | 1101 1000 | 54 × 4 = 216 |
| 1010 1101 | LSR #3 | 0001 0101 | 173 ÷ 8 = 21 remainder 5 lost |
| 0111 0001 | LSR #4 | 0000 0111 | 113 ÷ 16 = 7 remainder 1 lost |
| 0000 1011 | LSL #3 | 0101 1000 | 11 × 8 = 88 |
| 1001 0110 | LSL #1 | 0010 1100 | 150 × 2 should be 300: a 1 was lost, so 44 |
Multiply and divide
- Logical shift left n places: the unsigned value × 2ⁿ, as long as no 1 is shifted out.
- Logical shift right n places: the unsigned value ÷ 2ⁿ, keeping only the whole number.
- A logical shift ignores the sign, so it can change a two’s complement number’s sign.
Which instruction?
To change 1110 0000 into 0001 1100, the bits move 3 places right: LSR #3. Give the opcode and the operand.
1 Give the ACC after LSL #3 when it holds 0001 0110.
2 Give the ACC after LSR #2 when it holds 1011 0100.
3 Which instruction changes 0000 1101 into 0110 1000?
- 1
1011 0000: 22 × 8 = 176 - 2
0010 1101: 180 ÷ 4 = 45 - 3
LSL #3: the bits move 3 places left (13 × 8 = 104)
LSL brings 0s in on the right; LSR brings them in on the left. Bits pushed off the end they move towards are lost. Write all eight bits. LSR #3 divides an unsigned integer by 8 (whole number only).Arithmetic and cyclic shifts
A logical shift can turn a negative number positive. An arithmetic shift treats the register as a signed (two’s complement) number and keeps its sign. A cyclic shift rotates the bits: a bit leaving one end enters at the other.
- Arithmetic right: the sign bit is repeated into the gap.
- Arithmetic left: 0s enter on the right, and the sign bit must not change.
- Work on the bits. If a question gives its own instructions, such as ASR or CSL, follow its table.
An arithmetic shift treats the register as a signed (two’s complement) number and keeps the sign. Right: the sign bit is repeated into the gap. Left: 0s enter, and the sign bit must not change. A cyclic shift rotates the bits: a bit leaving one end enters at the other. Work these results directly on the bits; if a question supplies extra instructions such as ASR or CSL, follow its instruction table.
Read each drawing from its top row down to its bottom row. Orange cells show what fills the gap; violet cells are bits that wrap round to the other end.
Compare the three shifts by what fills the gap and what happens to the bits shifted out.
| Shift | Right shift fills the left with… | Bits shifted out | Used for |
|---|---|---|---|
| Logical | zeros | lost | unsigned × / ÷ 2ⁿ |
| Arithmetic | the sign bit (MSB), repeated | lost | signed × / ÷ 2ⁿ |
| Cyclic | the bits shifted out of the right | re-enter at the other end | rotating bits; none lost |
Right shifts: alike and different
- Alike: both move every bit right, so the lowest bits fall off the end.
- Different: logical fills the top bits with 0s, so a negative number turns positive; arithmetic fills them with copies of the sign bit, so the sign stays.
Same number, two right shifts
1011 0100 (−76) shifted right 2 places:
logical → 0010 1101 = +45
arithmetic → 1110 1101 = −19
1 Arithmetic shift 1111 0100 (−12) right 1 place. Give the result and its value.
2 Cyclic shift 1100 0011 left 2 places.
3 Logical shift 1111 0100 right 1 place. Why does the number become positive?
- 1
1111 1010= −6 - 2
0000 1111: the two 1s leaving the left re-enter on the right - 3
0111 1010: a 0 enters the sign bit, so the result is +122
Bit masking: test, set, clear and toggle
Often only one bit of a byte matters, such as a single on/off switch. A mask is a bit pattern combined with the ACC by AND, OR or XOR. It tests or changes chosen bits.
Set, clear and toggle masks keep the other bits; a test AND clears them. Bits are numbered 7 to 0; target bits are orange.
| Opcode | Operand | Explanation |
|---|---|---|
| AND | #n / Bn / &n | Bitwise AND operation of the contents of ACC with the operand |
| AND | <address> | Bitwise AND operation of the contents of ACC with the contents of <address> |
| XOR | #n / Bn / &n | Bitwise XOR operation of the contents of ACC with the operand |
| XOR | <address> | Bitwise XOR operation of the contents of ACC with the contents of <address> |
| OR | #n / Bn / &n | Bitwise OR operation of the contents of ACC with the operand |
| OR | <address> | Bitwise OR operation of the contents of ACC with the contents of <address> |
Work each example one column at a time: ACC, then mask, then result. Orange marks the bits the mask is aimed at; the other bits come through unchanged, except in the test.
What each operation does to one ACC bit. Choose the operation from the job.
| Operation | Mask bit 1 | Mask bit 0 | Use it to |
|---|---|---|---|
| AND | keeps the ACC bit | clears it to 0 | test a bit; clear bits |
| OR | sets it to 1 | keeps the ACC bit | set bits |
| XOR | flips it | keeps the ACC bit | toggle bits |
Worked results: convert the operand first
| ACC before | Instruction | Value used, in binary | ACC after |
|---|---|---|---|
| 1011 0011 | AND #60 | #60 = 0011 1100 | 0011 0000 |
| 0101 1010 | OR &81 | &81 = 1000 0001 | 1101 1011 |
| 0100 1110 | XOR B11110000 | already binary | 1011 1110 |
| 1110 0101 | XOR 120 | contents of 120 = 0000 1111 | 1110 1010 |
AND #0 clears every bit, whatever the ACC held. To force the ACC to 1010 0000: AND #0, then OR B10100000.1 The ACC holds 0110 1001. Give the ACC after OR B10000000.
2 From the same ACC, give the result of AND #15.
3 From the same ACC, give the result of XOR &FF.
4 Which instruction clears bit 0 and keeps every other bit?
- 1
1110 1001 - 2
0000 1001: #15 is0000 1111 - 3
1001 0110: &FF is1111 1111, so every bit flips - 4
AND B11111110
# is denary and & is hexadecimal: convert the operand to 8-bit binary before combining (AND #63 uses 0011 1111). Then work bit by bit and write all eight bits of the result.Monitoring and controlling a device
When the greenhouse gets too hot, the fan must come on without switching off the heater or the pump. Each is one bit of a byte in memory, so each bit has its own meaning: the program reads an input byte to monitor sensors and changes single bits of an output byte to control actuators.
Each byte is eight on/off switches. Find bit 3 of SENSE (too hot) and bit 0 of CONTROL (the fan): the program links those two bits.
Read the program in three moves: test bit 3 of SENSE, set or clear bit 0 of CONTROL, then store CONTROL back. The results show it working both ways.
// too hot (bit 3) turns the fan on LDD SENSE AND B00001000 // keep bit 3 CMP #8 // bit 3 set? JPN FANOFF LDD CONTROL OR B00000001 // fan on STO CONTROL JMP DONE FANOFF: LDD CONTROL AND B11111110 // fan off STO CONTROL DONE: END SENSE: B00101001 CONTROL: B10000100
Results of the program
| SENSE | CONTROL before | CONTROL after |
|---|---|---|
| 0010 1001 | 1000 0100 | 1000 0101 |
| 0010 0001 | 1000 0101 | 1000 0100 |
Read → decide → control
| Operation | Greenhouse effect |
|---|---|
| Read SENSE; AND B00001000 | isolates bit 3 in the ACC; compare with 8 to test “too hot” |
| Reload CONTROL | get the actuator states again: the sensor test changed the ACC |
| OR B00000001 / AND B11111110 | set or clear the fan bit; preserve heater and pump |
| STO CONTROL | write the changed output byte back to drive the actuators |
XOR toggles a chosen actuator bit. AND #0 clears every output bit; store it to turn every actuator off.
Is the number a multiple of 4?
A multiple of 4 has 0 in both of its two lowest bits. Mask the other bits and compare: AND #3, CMP #0, JPE MULT4.
For an odd number, bit 0 is 1: AND #1 clears the other bits; CMP #1 is True exactly when the original number was odd.
1 SENSE holds 0000 1000 and CONTROL holds 0000 0100. What is in CONTROL after the program runs?
2 Which one instruction would switch the heater (bit 2) off and keep every other bit?
3 Why does the program load CONTROL again before OR B00000001?
- 1
0000 0101: bit 3 is set, so the fan bit is turned on - 2
AND B11111011 - 3the test with AND replaced the ACC with the masked SENSE byte, so CONTROL must be reloaded