Skip to content

9618 · Unit 4 · Paper 1: Theory fundamentals

Processor fundamentals.

9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

The Von Neumann model

§4.1 · CPU architecture · 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.

Processor (CPU)System clocktiming signalsControl unit (CU)decodes instructions,sends control signalsALUarithmetic andlogic operationsACCcalculation resultsPCCIRIXSRMARMDRspecial purposeregistersMain memory (IAS)200LDD 230201ADD 231202STO 232203END…230923142320program(instructions)dataInput andoutputdevicesconnectedthrough portsAddress busData busControl buscontrol and timing signals
Processor (CPU)System clocktiming signalsControl unit (CU)decodes; coordinatesALUarithmetic / logicACCcalculation resultsPCCIRIXSRSpecial purpose registersMARMDRAddress bus →Data bus ↔Control bus ↔Main memory (IAS)200LDD 230201ADD 231202STO 232203END…230923142320Input / outputdevicesthrough portsInstructions and data share the same memory.

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.

ComponentRole
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 clockproduces 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.
Your turn

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.

  1. 1
    both in main memory (the IAS)
  2. 2
    the control unit (CU) decodes; the ALU calculates
  3. 3
    the address, data and control buses
Tick what you can do now
Exam focusStored program concept: a program’s instructions and its data share main memory. For the CU, make two points: it sends control signals over the control bus, and it coordinates the other components.
Practise with w24/11 Q3(a); s25/12 Q1(a); w22/11 Q5(a) (older paper: stored program); w23/13 Q9(a)(i) (older paper: components); w23/11 Q5(b) (older paper: IAS)
1 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Registers

§4.1 · CPU architecture · general and special purpose 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.

RegisterWhat 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 purposefree for any value the program needs while it computes, such as a running total
Special purposedoes 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
In the assembly instruction table, ACC is called the general purpose register available to the program. In architectural classification, the accumulator is a named special purpose register. Its holding role stays the same: program values and intermediate results.

Worked example: flags

Add the two bytes column by column, then read each flag from the result.

01100100+100+00110010+5010010110= −106Status register flags after the ADD1N negative0Z zero0C carry1V overflow

+150 does not fit in 8 bits as a signed number, so the sign bit comes out wrong and the overflow flag is set.

Your turn

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?

  1. 1
    the Program Counter (PC)
  2. 2
    300 + 4 = 304
  3. 3
    the Memory Data Register (MDR); the MAR holds its address
Tick what you can do now
Exam focusSay what is held: the PC stores an address: the location of the next instruction, never the instruction itself. The MAR holds an address; the MDR holds the data or instruction found there.
Practise with w24/12 Q3(a); w24/11 Q8(a), (b); w25/11 Q6(a); s25/12 Q1(a)
2 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Buses: moving data between components

§4.1 · CPU architecture · address, data and control buses

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.

Read: memory → CPUCPUMAR240MDR→ 63CUreadMemory23912240632415address bus1data bus3control bus2“read” signal63 to the MDRaddress 240Write: CPU → memoryCPUMAR240MDR17CUwriteMemory23912240172415address bus1data bus3control bus2“write” signal17 to memoryaddress 240
Read: memory → CPUCPUMAR240MDR→ 63CUreadMemory23912240632415address bus1data bus3control bus2“read” signal63 to the MDRaddress 240Write: CPU → memoryCPUMAR240MDR17CUwriteMemory23912240172415address bus1data bus3control bus2“write” signal17 to memoryaddress 240

Address, data and control buses

For each bus, be able to say what it carries and which way.

BusCarriesDirection
Address busthe address, from the MAR, of the location or I/O device to read or writeone-way: from the CPU to memory and I/O
Data busthe data or instructions being transferredtwo-way: to and from the CPU
Control buscontrol and timing signals, e.g. memory read, memory write, clock pulses, interruptstwo-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

CauseTransfer it enables
Clock emits regular timing signalssignals on the control bus keep CPU and memory synchronised
CU sends a read signalmemory uses the address bus value 240 to select the price
Memory responds at the timed stageprice 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.
Your turn

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?

  1. 1
    the address bus
  2. 2
    the address bus: from the CPU to memory and I/O only
  3. 3
    17 travels on the data bus; the write signal travels on the control bus
Tick what you can do now
Exam focusName the bus, then what it carries: address bus, the address to read or write; data bus, the data; control bus, signals such as read, write, timing and interrupt.
Practise with s25/13 Q2(a)(i); s25/12 Q1(a); s23/13 Q7(b) (older paper: timed data transfer); w22/11 Q5(b)(ii) (older paper: bus matching)
3 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Performance of the computer system

§4.1 · CPU architecture · performance factors

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.

Data bus width: moving one 16-bit item8-bit data bus: 2 transferstransfer 1transfer 216-bit data bus: 1 transferCache: fast memory close to the CPUCPUneeds data oran instructionCachesmall, fast(SRAM)RAMlarger, slower(DRAM)requesthit: fastmisscopy inMore cache keeps more frequently used items close to the CPU,so it waits less often for the slower RAM.
Moving the same 16-bit item8-bit bus: two transfers10110110transfer 100101101transfer 216-bit bus: one transfer1011011000101101Cache: request, hit or missCPUCachesmall, fast SRAMRAMlarger, slower DRAMrequesthit: fastmisscopy inMore cache can reduce waits for slower RAM.

Factors that affect performance

Link each factor to how it makes the computer faster.

FactorWhy increasing it can improve performance
Clock speedmore 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 coresevery 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 widthmore bits move in each transfer, so a large item needs fewer transfers and the processor waits less for data
Address bus widthmore 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 memorya 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 upgradeWhat limits the gain
3 GHz dual-core → 3 GHz quad-coresame clock rate; more cores can share parallel work
One-core programextra cores stay idle for that task
Results shared between corescoordination adds overhead
Unchanged memory supplyextra 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.
Your turn

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?

  1. 1
    4 transfers then 2
  2. 2
    2²⁰ = 1 048 576 locations
  3. 3
    the other cores have no work from that program, so they stay idle
Tick what you can do now
Exam focusLink each factor to its mechanism: clock speed → more F-E cycles each second; more cores → instructions run in parallel; wider data bus → more bits per transfer; bigger cache → fewer waits for RAM.
Practise with w24/11 Q3(b)(i); s25/11 Q6(a)(iii); w25/12 Q3(c); w25/11 Q6(b)
4 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Ports: connecting peripheral devices

§4.1 · CPU architecture · ports

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: what happens when a device is plugged inPlug a device intothe USB portThe port’s voltagechangesThe OS reads thedevice’s IDThe ID matches astored driverThe OS loadsthat driverSending a picture and sound to a hall projectorComputerdigital dataComputerdigital dataconvert toanalogueVGA cable: analogue video onlyProjectorconverts backSpeakersseparate cablesound needs a second cableHDMI cable: digital video and audio togetherProjector with a built-in speakerpicture and sound, no conversion
USB: plug and playPlug into the USB portPort voltage changesOS reads the device's IDMatch the ID to a stored driverOS loads that driverVGA: picture and sound take separate pathsComputerdigital dataConvert videoto analogueProjectorconverts backVGA cable:analogue videoProjector speakerseparate audio cableaudioHDMI: picture and sound travel togetherComputerdigital dataOne HDMI cable:digital video + audioProjector with built-in speakerPicture and soundNo analogue conversionSame hall projector: 3840 × 2160 pixels.

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.

PortHow it provides the connectionTypical 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 driverkeyboard, mouse, printer, flash drive
HDMI (High Definition Multimedia Interface)uncompressed digital video and audio on one cable; high bandwidth; less prone to interferencemonitor, TV, projector
VGA (Video Graphics Array)analogue; video only, so sound needs a separate cable; a lower transfer rate than HDMIolder 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.
“Newer” identifies no relevant feature. Link the interface’s property to the projector’s need.

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.

Your turn

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?

  1. 1
    HDMI
  2. 2
    the operating system detects the device and loads its driver automatically
  3. 3
    a separate audio cable: VGA carries analogue video only
Tick what you can do now
Exam focusJustify a port with a feature that matters for the device: HDMI carries audio and video on one cable; USB is plug and play (the driver loads automatically). Name the feature: “HDMI is better” on its own explains nothing.
Practise with w24/11 Q3(b)(ii); w24/12 Q3(b); s25/13 Q2(c)
5 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

The fetch-execute cycle

§4.1 · CPU architecture · F-E cycle and register transfer notation

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.

ProcessorPC200 → 201MAR200, then 230MDRLDD 230, then 9CIRLDD 230CUdecodesACC9Main memory200LDD 230201ADD 231202…2309231412+1address bus: 200data bus: LDD 23033456execute9 copied to the ACC
ProcessorPC200 → 201MAR200, then 230CIRLDD 230MDRLDD 230, then 9CUdecodesACC9Main memory200LDD 230201ADD 231202…2309231412+13address bus: 2003data bus: LDD 230456execute

The fetch stage in register transfer notation

Learn the four fetch steps in this order, written exactly in this notation.

StepRegister transfer notationWhat happens
1MAR ← [PC]copy the PC’s address into the MAR
2PC ← [PC] + 1add 1 to the PC (it is incremented), ready for the next fetch
3MDR ← [[MAR]]copy what is stored at the MAR’s address into the MDR
4CIR ← [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
Your turn

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. 1
    MAR ← [PC]; PC ← [PC] + 1; MDR ← [[MAR]]; CIR ← [MDR]
  2. 2
    351: it is incremented once
  3. 3
    CIR ← [MDR]: the instruction is copied from the MDR
Tick what you can do now
Exam focusFetch in four steps: PC to MAR; add 1 to the PC; memory at the MAR’s address to the MDR; MDR to CIR. Use double brackets for the MAR ([[MAR]], what is stored at that address); the PC is incremented.
Practise with s25/13 Q2(a)(ii); w21/11 Q6(a) (older paper: correct RTN errors)
6 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Interrupts: purpose and causes

§4.1 · CPU architecture · interrupts

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.

ComputerprocessorBufferSound cardplays at a fixed rateblocks sent in burstsdata taken steadilybuffer empty: an interrupt asks the processor for the next blockbuffer full: an interrupt pauses the senderfree to do other workholds data on its way
Computer / processorfree for other workblocks in burstsBuffer: temporary storedata steadilySound cardplays at a fixed rateAmber return: interrupt requests more data whenempty, or pauses the sender when full.

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.

Your turn

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?

  1. 1
    hardware: it comes from a device
  2. 2
    software: it comes from the program
  3. 3
    the processor can do other work until the event happens, instead of wasting time checking
Tick what you can do now
Exam focusPurpose: an interrupt requests processor attention when an event occurs; handling waits for the cycle boundary and priority check. Sort each cause by where it starts: hardware (timer, transfer finished, buffer, fault) or software (division by zero, a request to the OS, reading past an array’s end).
Practise with s24/12 Q2(b); w22/11 Q5(c), (d) (older paper: definition and causes); w21/12 Q7(a) (older paper: hardware or software causes)
7 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Detecting and handling an interrupt

§4.1 · CPU architecture · interrupts and the ISR

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.

Fetch, decode and executeone instructionInterrupt flagset?NoYesHigher priority thanthe current task?No: continue; the interrupt waitsYesSave the PC and other registerson the stackIdentify the source; load the startaddress of its ISR into the PCThe ISR runsand services the interruptAnother interrupt ofhigher priority?YesNoRestore the registers from the stackcontinue the interrupted programchecked once per cycle,after the execute stage:the current instructionalways finishesProcessor registersPC201900904201ACC2323123Stack copyPC201201ACC2323Door sensordoor open · flag setISR recordsthe open doorLogging resumes:PC 201 · ACC 23
Processor registersStack copyPC201900904201ACC2323123PC201201ACC2323Door sensordoor open · flag setFetch, decode, executeone instructionInterrupt flagset?NoYesHigher priority thanthe current task?No: continue;interrupt waitsYesSave PC and other registerson the stackIdentify source; load PCwith its ISR start addressRun the source's ISRdoor sensor: record door openAnother interruptof higher priority?YesNoRestore saved registersfrom the stackLogging resumes at PC 201 with ACC 23.

Why temperature logging can resume

Preserved stateWhy it matters after the door sensor's ISR
PCrestores the address of the next logging instruction
Other registersrestore intermediate values and flags the logging program was using
Stack copykeeps the interrupted state while the ISR records that the greenhouse door is open
Your turn

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?

  1. 1
    once every cycle, after the execute stage and before the next fetch
  2. 2
    so the interrupted program can resume where it stopped, with its own values
  3. 3
    the current task carries on; the interrupt waits
Tick what you can do now
Exam focusWhen: once per cycle, after the execute stage and before the next fetch. How: compare priorities; push the registers onto the stack; run the source’s ISR; look for another higher-priority interrupt; then restore the registers and resume.
Practise with s25/12 Q1(c)
8 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

The instruction set and its groups

§4.2 · Assembly language · instruction 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.

OpcodeOperandExplanation
Data movement · copy values between memory and registers
LDM#nImmediate 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#nImmediate 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
INKey in a character and store its ASCII value in ACC
OUTOutput 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/&nAdd the number n to the ACC
SUB<address>Subtract the contents of the given address from the ACC
SUB#n/Bn/&nSubtract 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#nCompare 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
ENDReturn 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.
Your turn

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.

  1. 1
    compare instructions
  2. 2
    LDM #25
  3. 3
    JPE 120
Tick what you can do now
Exam focusUse the exact group names: data movement, input and output of data, arithmetic operations, unconditional and conditional instructions, compare instructions. Give an instruction with a suitable operand, e.g. JPN 150.
Practise with w25/13 Q6(c); w23/11 Q8(b)(i) (older paper: supply instructions with operands)
9 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

The two-pass assembler

§4.2 · Assembly language · machine code and 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.

Source code
// 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

ADDRSOURCELABELS REPLACED
100ININ
101CMP YESCMP 108
102JPE SETONEJPE 105
103LDM #0LDM #0
104JMP SAVEJMP 106
105SETONE:LDM #1LDM #1
106SAVE:STO ANSWERSTO 109
107ENDEND
108YES:8989
109ANSWER:00

Symbol table

LabelAddress
SETONE105
SAVE106
YES108
ANSWER109

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.

TaskFirst passSecond 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✓
Why two passes? 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.
Your turn

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?

  1. 1
    the symbol table: each label with its address
  2. 2
    303 (300, 301, 302, then 303)
  3. 3
    SETONE is defined later in the program, so its address is not yet known (a forward reference)
Tick what you can do now
Exam focusPass 1 builds the symbol table (each label with its address); pass 2 generates the object code, replacing each label with its address from the symbol table.
Practise with s26/11 Q2(c); s23/12 Q3(a) (older paper: pass matching); w22/11 Q6(c) (older paper: opcode checks); w23/11 Q8(a) (older paper: symbol table purpose)
10 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Addressing modes

§4.2 · Assembly language · modes of addressing

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.

ImmediateLDM #120the operand is the valueACC = 120DirectLDD 120address 12034ACC = 34IndirectLDI 121address 121123address 1237ACC = 7IndexedLDX 120120 + IX (2) = 122address 1229ACC = 9
ImmediateLDM #120Operand is value120ACC = 120DirectLDD 120Memory address 12034ACC = 34IndirectLDI 121Memory address 121123Memory address 1237ACC = 7IndexedLDX 120120 + IX (2)122Memory address 1229ACC = 9

Five modes of addressing

For each mode, be able to say what the operand is.

ModeHow the operand is used
Immediatethe value itself is the operand: LDM #120 loads 120
Directthe operand gives the address of the value
Indirectthe operand gives the address of a location that holds the value’s address
Indexedoperand + IX is the address of the value
Relativethe 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 #5252LDX 5212
LDD 5250LDR #4 · LDX 507
LDI 5253LDD 51 · ADD #610
Your turn

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.

  1. 1
    5: the value stored at 81
  2. 2
    9: 80 holds 82, and 82 holds 9
  3. 3
    4: 82 + 1 = 83, which holds 4
  4. 4
    81: the operand itself
Tick what you can do now
Exam focusDescribe what the operand is: the value (immediate), the value’s address (direct), the address of the value’s address (indirect), an address that has IX added (indexed). Direct and indexed both read a value from memory into the ACC; only indexed adds IX to the operand first.
Practise with s26/13 Q7(a); w25/12 Q3(a)(i), (b); s26/11 Q3(c); s24/12 Q5(a); w22/13 Q6(a)(ii) (older paper: relocation)
11 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Tracing a program

§4.2 · Assembly language · trace tables

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.

Program in memoryaddress · instruction
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

Address300301302
CONTENTS072

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.

Pass 1, one instruction at a time
Each instruction that runs is a row. Its value goes in the column it writes. Together they make the blue row of the whole run below.
AddressStatement that runsACC300301302WritesWhat happens
200LDD 3000ACC 0load the value stored at 300: 0
load the value stored at 300: 0
201ADD 3017ACC 70 + 7 = 7
0 + 7 = 7
202STO 3007300 7copy the ACC (7) into 300; the ACC keeps 7
copy the ACC (7) into 300; the ACC keeps 7
203LDD 3022ACC 2load the value stored at 302: 2
load the value stored at 302: 2
204DEC ACC1ACC 12 − 1 = 1
2 − 1 = 1
205STO 3021302 1copy the ACC (1) into 302; the ACC keeps 1
copy the ACC (1) into 302; the ACC keeps 1
206CMP #0test is 1 equal to 0? False
test is 1 equal to 0? False
207JPN 200jump 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.

ACC300301302What decided it
start072memory before the program runs
memory before the program runs
pass 1072171302 is now 1: not 0, so JPN 200 jumps back
302 is now 1: not 0, so JPN 200 jumps back
pass 271410140302 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
ACCMemory address
300301302
072
2000
2017
2027
2032
2041
2051
206
207
2007
20114
20214
2031
2040
2050
206
207
208

How to fill a trace table

1
Write a row for every instruction executed, in the order it runs. A jump sends you back to an earlier address.
2
Write the value each instruction sets, in its column; leave the other cells blank. CMP and jumps leave the ACC and these memory locations unchanged, so their rows hold only the instruction address.
3
STO changes a memory column, not the ACC; LDD changes the ACC, not memory.
4
Stop at END, or where the question tells you to stop.
Program in memoryaddress · instruction
200LDD 300
201ADD 301
202STO 300
203LDD 302
204DEC ACC
205STO 302
206CMP #0
207JPN 200
208END

Memory at the start

Address300301302
CONTENTS072

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
ACCMemory address
300301302
072
2000
2017
2027
2032
2041
2051
206
207
2007
20114
20214
2031
2040
2050
206
207
208

How to fill a trace table

1
Write a row for every instruction executed, in the order it runs. A jump sends you back to an earlier address.
2
Write the value each instruction sets, in its column; leave the other cells blank. CMP and jumps leave the ACC and these memory locations unchanged, so their rows hold only the instruction address.
3
STO changes a memory column, not the ACC; LDD changes the ACC, not memory.
4
Stop at END, or where the question tells you to stop.
Your turn

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?

  1. 1
    3 times: 302 counts 3, 2, 1, 0
  2. 2
    21: 7 + 7 + 7
  3. 3
    25: 8 instructions in each of 3 passes, then END
Tick what you can do now
Exam focusFollow the instructions in the order they execute, not the order they are written. Write the value each instruction sets in the right column: STO sets a memory column, a load sets the ACC.
Practise with s25/12 Q7(a)(i), (a)(ii); s25/11 Q8(a); s25/13 Q5(a)(i)
12 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Tracing with the index register

§4.2 · Assembly language · trace tables with IX and output

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.

Program in memoryaddress · instruction
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

Address500501502
CONTENTS069

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.

Pass 1, one instruction at a time
Each instruction that runs is a row. Its value goes in the column it writes. Together they make the blue row of the whole run below.
AddressStatement that runsACCIXOutputWritesWhat happens
101LDX 5009ACC 9read address 500 + IX = 500 + 2 = 502: 9
read address 500 + IX = 500 + 2 = 502: 9
102CMP #0test is 9 equal to 0? False
test is 9 equal to 0? False
103JPE 108jump the compare was False, so no jump: go on to 104
jump the compare was False, so no jump: go on to 104
104ADD #4857ACC 579 + 48 = 57
9 + 48 = 57
105OUT9Output 9output the character whose code is 57: 9
output the character whose code is 57: 9
106DEC IX1IX 12 − 1 = 1
2 − 1 = 1
107JMP 101jump 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.

ACC500501502IXOutputWhat decided it
start069memory before the program runs
memory before the program runs
set up2LDR #2 puts 2 in IX
LDR #2 puts 2 in IX
pass 195719reads 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 265406reads 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 30reads 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
ACCMemory addressIXOutput
500501502
069
1002
1019
102
103
10457
1059
1061
107
1016
102
103
10454
1056
1060
107
1010
102
103
108

Points to watch

  • LDX 500 reads address 500 + IX: 502, then 501, then 500.
  • DEC IX changes the IX column, not the ACC.
  • ADD #48 turns the number 9 into 57, the ASCII code of the character 9.
  • OUT outputs the character whose code is in the ACC: 57 gives 9.
  • When 0 is loaded from 500, CMP #0 is True, so JPE 108 jumps to END.
Program in memoryaddress · instruction
100LDR #2
101LDX 500
102CMP #0
103JPE 108
104ADD #48
105OUT
106DEC IX
107JMP 101
108END

Memory at the start

Address500501502
CONTENTS069

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
ACCMemory addressIXOutput
500501502
069
1002
1019
102
103
10457
1059
1061
107
1016
102
103
10454
1056
1060
107
1010
102
103
108

Points to watch

  • LDX 500 reads address 500 + IX: 502, then 501, then 500.
  • DEC IX changes the IX column, not the ACC.
  • ADD #48 turns the number 9 into 57, the ASCII code of the character 9.
  • OUT outputs the character whose code is in the ACC: 57 gives 9.
  • When 0 is loaded from 500, CMP #0 is True, so JPE 108 jumps to END.
Your turn

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?

  1. 1
    14: 502 first, then 501
  2. 2
    0
  3. 3
    49: 1 + 48, the code of the character 1
Tick what you can do now
Exam focusIn the Output column write the character, not the ASCII code: OUT with 57 in the ACC outputs 9. With indexed addressing, add IX to the operand before you read memory.
Practise with s21/11 Q3(b) (older paper: IX and character output); w22/11 Q6(a)(i) (older paper: sentinel loop)
13 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Logical shifts

§4.3 · Bit manipulation · 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.

LSL #2: logical shift left 2 places45 × 2² = 180001011017160514130211000bitdiscardedzeros enterLSR #3: logical shift right 3 places216 ÷ 2³ = 27110110007060504131201101bitdiscardedzeros enter
LSL #2: logical shift left 2 places45 × 2² = 180001011017160514130211000bitdiscardedzeros enterLSR #3: logical shift right 3 places216 ÷ 2³ = 27110110007060504131201101bitdiscardedzeros enter
OpcodeOperandExplanation
LSL#nBits in ACC are shifted logically n places to the left. Zeros are introduced on the right hand end
LSR#nBits 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 beforeInstructionACC afterEffect on an unsigned integer
0011 0110LSL #21101 100054 × 4 = 216
1010 1101LSR #30001 0101173 ÷ 8 = 21 remainder 5 lost
0111 0001LSR #40000 0111113 ÷ 16 = 7 remainder 1 lost
0000 1011LSL #30101 100011 × 8 = 88
1001 0110LSL #10010 1100150 × 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.

Your turn

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. 1
    1011 0000: 22 × 8 = 176
  2. 2
    0010 1101: 180 ÷ 4 = 45
  3. 3
    LSL #3: the bits move 3 places left (13 × 8 = 104)
Tick what you can do now
Exam focusLSL 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).
Practise with s26/11 Q3(b)(ii); w25/11 Q4(a); s25/11 Q8(b)(iii); s26/12 Q5(b)
14 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Arithmetic and cyclic shifts

§4.3 · Bit manipulation · 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.

Arithmetic right 2 places−24 ÷ 4 = −6: the sign bit is repeated111010007161514131201100bitdiscardedsign repeatedArithmetic left 2 places−7 × 4 = −28: the sign bit is kept111110017161514030211000bitdiscardedzeros enter
Arithmetic right 2 places−24 ÷ 4 = −6: the sign bit is repeated111010007161514131201100bitdiscardedsign repeatedArithmetic left 2 places−7 × 4 = −28: the sign bit is kept111110017161514030211000bitdiscardedzeros enter
Cyclic left 3 placesbits leaving the left re-enter on the right1011000110001101Cyclic right 2 placesbits leaving the right re-enter on the left1011000101101100
Cyclic left 3 placesbits leaving the left re-enter on the right1011000110001101Cyclic right 2 placesbits leaving the right re-enter on the left1011000101101100

Compare the three shifts by what fills the gap and what happens to the bits shifted out.

ShiftRight shift fills the left with…Bits shifted outUsed for
Logicalzeroslostunsigned × / ÷ 2ⁿ
Arithmeticthe sign bit (MSB), repeatedlostsigned × / ÷ 2ⁿ
Cyclicthe bits shifted out of the rightre-enter at the other endrotating 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

Your turn

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. 1
    1111 1010 = −6
  2. 2
    0000 1111: the two 1s leaving the left re-enter on the right
  3. 3
    0111 1010: a 0 enters the sign bit, so the result is +122
Tick what you can do now
Exam focusFor a difference, give both sides: a logical right shift puts 0s into the MSBs; an arithmetic right shift repeats the sign bit, so a negative number stays negative.
Practise with s26/13 Q7(c); w24/13 Q8(c); s25/13 Q7(a)(i), (a)(ii); w25/13 Q2(c); s26/12 Q5(b)
15 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Bit masking: test, set, clear and toggle

§4.3 · Bit manipulation · masks with AND, OR and XOR

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.

OpcodeOperandExplanation
AND#n / Bn / &nBitwise 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 / &nBitwise 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 / &nBitwise 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.

Test bit 2AND B00000100ACC10100110AND00000100result00000100not 0, so bit 2 is 1; thenCMP #4 and JPE or JPNSet bit 4OR B00010000ACC10100110OR00010000result10110110bit 4 becomes 1;the others are unchangedClear bit 7AND B01111111ACC10100110AND01111111result00100110bit 7 becomes 0;the others are unchangedToggle bits 0 to 3XOR B00001111ACC10100110XOR00001111result10101001bits 0 to 3 flip;bits 4 to 7 are unchanged
Test bit 2AND B00000100ACC10100110AND00000100Result00000100not 0, so bit 2 is 1; thenCMP #4 and JPE or JPNSet bit 4OR B00010000ACC10100110OR00010000Result10110110bit 4 becomes 1;the others are unchangedClear bit 7AND B01111111ACC10100110AND01111111Result00100110bit 7 becomes 0;the others are unchangedToggle bits 0 to 3XOR B00001111ACC10100110XOR00001111Result10101001bits 0 to 3 flip;bits 4 to 7 are unchanged

What each operation does to one ACC bit. Choose the operation from the job.

OperationMask bit 1Mask bit 0Use it to
ANDkeeps the ACC bitclears it to 0test a bit; clear bits
ORsets it to 1keeps the ACC bitset bits
XORflips itkeeps the ACC bittoggle bits

Worked results: convert the operand first

ACC beforeInstructionValue used, in binaryACC after
1011 0011AND #60#60 = 0011 11000011 0000
0101 1010OR &81&81 = 1000 00011101 1011
0100 1110XOR B11110000already binary1011 1110
1110 0101XOR 120contents of 120 = 0000 11111110 1010
AND #0 clears every bit, whatever the ACC held. To force the ACC to 1010 0000: AND #0, then OR B10100000.
Your turn

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. 1
    1110 1001
  2. 2
    0000 1001: #15 is 0000 1111
  3. 3
    1001 0110: &FF is 1111 1111, so every bit flips
  4. 4
    AND B11111110
Tick what you can do now
Exam focus# 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.
Practise with s24/12 Q5(b); w25/11 Q4(d); s26/11 Q3(b)(i)
16 / 17
9618 · Unit 4 · Processor fundamentals
Revisionbyexamplicity

Monitoring and controlling a device

§4.3 · Bit manipulation · monitor and control 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.

SENSE (input byte)read to monitor the sensors7060514031201001bit 0: window openbit 3: too hotbit 5: soil dryCONTROL (output byte)written to control the actuators7160504030211000bit 0: fanbit 2: heaterbit 7: water pump
SENSE: input byte7060514031201001bit 0: window openbit 3: too hotbit 5: soil dryCONTROL: output byte7160504030211000bit 0: fanbit 2: heaterbit 7: water pump

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.

Assembly language
// 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

SENSECONTROL beforeCONTROL after
0010 10011000 01001000 0101
0010 00011000 01011000 0100

Read → decide → control

OperationGreenhouse effect
Read SENSE; AND B00001000isolates bit 3 in the ACC; compare with 8 to test “too hot”
Reload CONTROLget the actuator states again: the sensor test changed the ACC
OR B00000001 / AND B11111110set or clear the fan bit; preserve heater and pump
STO CONTROLwrite 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.

Your turn

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. 1
    0000 0101: bit 3 is set, so the fan bit is turned on
  2. 2
    AND B11111011
  3. 3
    the test with AND replaced the ACC with the masked SENSE byte, so CONTROL must be reloaded
Tick what you can do now
Exam focusSay what the bit operation does to the device: testing a bit shows whether a sensor is on (monitoring); setting, clearing or toggling a bit switches an actuator (control). Then store the byte back.
Practise with s26/13 Q7(b); w24/12 Q8(b)(ii); s25/13 Q5(b); w24/13 Q7(b); s25/11 Q8(b)(i)
17 / 17