Chapter 4: Processor Fundamentals – 9618 CS AS Level Notes

4.1 Central Processing Unit (CPU) Architecture


Von Neumann Model and Stored Program Concept

Von Neumann Architecture

Definition: A computer architecture proposed by John von Neumann in 1945 where instructions and data are stored in the same memory and accessed via the same bus.

Key Characteristics:

  • Single memory space: Both program instructions and data stored in the same memory
  • Single bus system: One set of address/data/control buses for both instructions and data (von Neumann bottleneck)
  • Sequential execution: Instructions fetched and executed one at a time in sequence (unless branching)
  • Stored program concept: Program instructions are stored in memory like data and can be modified

Stored Program Concept

Definition: The idea that both program instructions and data are stored in memory and the computer can manipulate them similarly.

Key Principles:

  1. Instructions as data: Program instructions are represented in binary and stored in memory just like data
  2. Fetch-Execute cycle: CPU fetches instructions from memory, decodes them, and executes them
  3. Programmability: Programs can be loaded into memory, modified, and executed
  4. Self-modifying code: Programs can modify their own instructions (rarely done today due to security)

Importance:

  • Enables general-purpose computing
  • Computers can run different programs without hardware changes
  • Operating systems can load and manage multiple programs
  • Forms the basis of virtually all modern computers

Von Neumann Bottleneck

Definition: The limitation on throughput caused by the single bus between CPU and memory.

Problem:

  • Both instructions and data use same bus
  • CPU often waits for data while fetching instructions, or vice versa
  • Memory access speed limits overall performance

Solutions:

  • Cache memory (stores frequently used data closer to CPU)
  • Harvard architecture (separate instruction and data buses – used in some embedded systems)
  • Wider buses
  • Faster memory technologies

Basic Von Neumann Block Diagram

                    ┌─────────────────┐
                    │                 │
                    │      CPU        │
                    │                 │
                    │  ┌───────────┐  │
                    │  │    CU     │  │
                    │  └───────────┘  │
                    │  ┌───────────┐  │
                    │  │    ALU    │  │
                    │  └───────────┘  │
                    │  ┌───────────┐  │
                    │  │ Registers │  │
                    │  └───────────┘  │
                    └────────┬────────┘
                             │
              ┌──────────────┼──────────────┐
              │              │              │
         Address Bus    Data Bus      Control Bus
              │              │              │
              ↓              ↓              ↓
                    ┌─────────────────┐
                    │                 │
                    │     Memory      │
                    │   (Instructions │
                    │     and Data)   │
                    └─────────────────┘

CPU Registers

General Purpose vs Special Purpose Registers

General Purpose Registers:

  • Can be used for any temporary storage by programmer/compiler
  • Hold intermediate results, variables, addresses
  • Number varies by architecture (typically 8-32)
  • Examples: R0, R1, R2… in ARM; EAX, EBX, ECX in x86

Special Purpose Registers:

  • Designed for specific functions
  • Used by CPU automatically during fetch-execute cycle
  • Programmer may access some indirectly
  • Critical for CPU operation

Special Purpose Registers

Program Counter (PC)

Purpose: Holds the memory address of the next instruction to be executed.

Operation:

  • During fetch, contents copied to MAR
  • After instruction fetch, PC increments to point to next instruction
  • For jumps/branches, PC loaded with new address
  • Size determines maximum addressable memory (e.g., 32-bit PC can address up to 4GB)

Register Transfer Notation: PC ← address of next instruction

Memory Address Register (MAR)

Purpose: Holds the address of memory location to be read from or written to.

Operation:

  • Connected directly to address bus
  • Before memory access, address placed here
  • Used for both instruction fetches and data accesses

Register Transfer Notation: MAR ← address

Memory Data Register (MDR) / Memory Buffer Register (MBR)

Purpose: Holds data being transferred to or from memory.

Operation:

  • For read: Receives data from memory via data bus
  • For write: Holds data to be sent to memory
  • Acts as buffer between CPU and memory

Register Transfer Notation: MDR ← data from memory OR MDR ← data from CPU

Accumulator (ACC)

Purpose: Stores results of arithmetic and logic operations.

Operation:

  • Default destination for ALU operations
  • One operand often comes from ACC
  • Results placed back in ACC
  • In simple CPUs, the main working register

Register Transfer Notation: ACC ← result of ALU operation

Index Register (IX)

Purpose: Used for indexed addressing modes; holds offset for array access.

Operation:

  • Contents added to base address to form effective address
  • Useful for accessing arrays and data structures
  • Can be auto-incremented/decremented for sequential access

Register Transfer Notation: Effective Address = base address + IX

Current Instruction Register (CIR)

Purpose: Holds the current instruction being executed.

Operation:

  • During fetch, instruction copied from MDR to CIR
  • Held here while being decoded and executed
  • Contains opcode and operand fields

Structure:

CIR = [Opcode][Operand/Address]
         ↑        ↑
      (to CU)   (to MAR or ALU)

Status Register / Flags Register / Condition Code Register

Purpose: Stores status information about the result of the last operation.

Common Flags:

FlagNameDescription
ZZero FlagSet if result was zero
NNegative FlagSet if result was negative
CCarry FlagSet if operation produced a carry out
VOverflow FlagSet if arithmetic overflow occurred
IInterrupt FlagEnables/disables interrupts

Operation:

  • Updated automatically by ALU after each operation
  • Used by conditional branch instructions
  • Example: “Branch if equal” checks Z flag

CPU Components

Arithmetic and Logic Unit (ALU)

Purpose: Performs all arithmetic and logical operations.

Operations performed:

CategoryExamples
ArithmeticADD, SUBTRACT, MULTIPLY, DIVIDE, INCREMENT, DECREMENT
LogicalAND, OR, NOT, XOR
ComparisonCOMPARE, TEST
Shift/RotateLogical shift, Arithmetic shift, Rotate

Operation:

  1. Receives operands from registers (usually ACC and another register/memory)
  2. Performs operation specified by Control Unit
  3. Stores result in ACC (or other destination)
  4. Updates Status Register flags

Control Unit (CU)

Purpose: Coordinates all activities of the CPU.

Functions:

  • Instruction decoding: Interprets opcode in CIR
  • Control signal generation: Creates timing and control signals for all components
  • Sequencing: Manages fetch-execute cycle
  • Interrupt handling: Responds to interrupts

Operation:

  1. Receives instruction from CIR
  2. Decodes to determine required actions
  3. Generates appropriate control signals
  4. Sends signals to ALU, registers, buses, memory

System Clock

Purpose: Provides timing signals to synchronise CPU operations.

Characteristics:

  • Generates regular pulses
  • Clock speed measured in Hz (typically GHz)
  • One clock cycle = time between two pulses
  • Most instructions take multiple clock cycles

Clock speed factors:

  • Higher speed = potentially more instructions per second
  • Limited by physical constraints (heat, power, signal propagation)

Relationship to performance:

Performance ∝ Clock Speed × Instructions per Cycle (IPC)

Immediate Access Store (IAS)

Purpose: Primary memory that CPU can access directly (RAM).

Characteristics:

  • Holds currently executing programs and their data
  • Directly addressable by CPU
  • Much faster than secondary storage
  • Volatile (lost when power off)
  • Organised as addressable locations (each holding a word/byte)

Relationship with CPU:

  • CPU reads instructions from IAS via MAR/MDR
  • CPU reads/writes data via MAR/MDR
  • Performance limited by IAS speed (von Neumann bottleneck)

Buses

System Buses

Definition: Communication pathways connecting CPU components and memory.

Address Bus

Purpose: Carries memory addresses from CPU to memory/I/O.

Characteristics:

  • Unidirectional: Address flows only from CPU to memory
  • Width determines maximum addressable memory
  • n-bit address bus can address up to 2ⁿ memory locations
  • Example: 32-bit address bus → 4GB maximum

Operation:

CPU (MAR) --address--> Address Bus --> Memory

Data Bus

Purpose: Carries data between CPU, memory, and I/O.

Characteristics:

  • Bidirectional: Data can flow both directions
  • Width affects data transfer rate
  • Wider bus = more bits transferred per cycle
  • Example: 64-bit data bus transfers 8 bytes per cycle

Operation:

CPU <--data--> Data Bus <--data--> Memory

Control Bus

Purpose: Carries control signals between CPU and other components.

Characteristics:

  • Bidirectional: Various signals in both directions
  • Width varies by architecture (typically 8-16 lines)
  • Each line has specific function

Common control signals:

SignalDirectionPurpose
ReadCPU → MemoryRequest memory read
WriteCPU → MemoryRequest memory write
ClockCPU → AllSynchronisation
ResetCPU → AllReset all components
Interrupt RequestI/O → CPUDevice needs attention
Interrupt AcknowledgeCPU → I/OInterrupt received
Bus RequestDevice → CPURequest bus access
Bus GrantCPU → DeviceBus access granted

Bus Interaction Example: Memory Read

1. CPU places address in MAR
2. CPU sets Read line on Control Bus
3. Address placed on Address Bus
4. Memory locates address
5. Memory places data on Data Bus
6. CPU reads data into MDR
7. CPU clears control signals

Performance Factors

Processor Type and Number of Cores

Single-core processor:

  • Executes one instruction at a time
  • Performance limited by clock speed and efficiency
  • Simple to program for

Multi-core processor:

  • Multiple processing units on one chip
  • Can execute multiple instructions simultaneously
  • Requires parallel programming for full benefit
  • Different types:
  • Dual-core: 2 cores
  • Quad-core: 4 cores
  • Octa-core: 8 cores
  • Many-core: 16+ cores

Performance considerations:

  • Not all tasks can be parallelised
  • Overhead of coordinating cores
  • Amdahl’s Law: Speedup limited by sequential portion
  • Power/heat management

Amdahl’s Law:

Speedup = 1 / ((1-P) + P/N)
where P = parallel portion, N = number of cores

Bus Width

Address bus width:

  • Determines maximum addressable memory
  • Wider = more RAM supported
  • Example: 32-bit → 4GB, 64-bit → 16EB

Data bus width:

  • Determines data transfer per cycle
  • Wider = faster data transfer
  • Common widths: 8, 16, 32, 64 bits
  • Must match word size for optimal performance

Impact on performance:

  • Narrow bus = more cycles to transfer data
  • Bottleneck if bus slower than CPU
  • Modern CPUs use 64-bit data buses

Clock Speed

Definition: Number of clock cycles per second (Hz).

Common speeds:

  • Older CPUs: MHz (millions of cycles/second)
  • Modern CPUs: GHz (billions of cycles/second)
  • Typical: 2-5 GHz

Relationship to performance:

  • Higher clock = potentially more instructions/second
  • But not all instructions complete in one cycle
  • Different instructions take different cycles (CPI)

CPI (Cycles Per Instruction):

CPU Time = Instructions × CPI × Clock Cycle Time

Limitations:

  • Heat generation increases with clock speed
  • Power consumption increases (often exponentially)
  • Physical limits to how fast signals can travel
  • Diminishing returns (pipelining better than raw speed)

Cache Memory

Definition: Small, fast memory between CPU and main memory.

Levels of cache:

LevelLocationSizeSpeedPurpose
L1 CacheInside CPU core16-64KBFastest (1-2 cycles)Instructions & data
L2 CacheBetween CPU and RAM256KB-1MBFast (3-10 cycles)Data from RAM
L3 CacheShared between cores2-32MBModerate (10-30 cycles)Shared data

How cache works:

  1. CPU checks cache first for required data
  2. If present (cache hit) → fast access
  3. If not present (cache miss) → fetch from slower RAM
  4. Frequently used data kept in cache

Cache principles:

  • Temporal locality: Recently accessed data likely to be accessed again
  • Spatial locality: Data near recently accessed data likely to be accessed

Cache performance:

Average Access Time = Hit Time + (Miss Rate × Miss Penalty)

Impact:

  • Good cache design significantly improves performance
  • Larger cache = higher hit rate but slower access
  • Multi-level cache balances speed and size

Ports for Peripheral Devices

Universal Serial Bus (USB)

Purpose: Universal connection standard for peripherals.

Characteristics:

  • Hot-swappable (connect/disconnect without power off)
  • Plug and play (automatic driver installation)
  • Power delivery (up to 100W with USB-C)
  • Data transfer + power in one cable

USB versions:

VersionSpeedYearNotes
USB 1.112 Mbps1998Low Speed/Full Speed
USB 2.0480 Mbps2000Hi-Speed
USB 3.05 Gbps2008SuperSpeed (blue ports)
USB 3.110 Gbps2013SuperSpeed+
USB 3.220 Gbps2017Dual-lane
USB440 Gbps2019Based on Thunderbolt 3

Connector types:

  • USB-A (traditional rectangular)
  • USB-B (printer/square)
  • USB-C (reversible, modern)
  • Micro-USB (older phones/devices)
  • Mini-USB (older devices)

High Definition Multimedia Interface (HDMI)

Purpose: Digital video and audio interface.

Characteristics:

  • Transmits uncompressed video and compressed/uncompressed audio
  • Single cable for both video and audio
  • HDCP copy protection support
  • Consumer electronics standard

HDMI versions:

VersionMax ResolutionFeatures
HDMI 1.44K @ 30HzEthernet channel, 3D
HDMI 2.04K @ 60Hz32 audio channels
HDMI 2.18K @ 60Hz, 4K @ 120HzDynamic HDR, eARC

Connector types:

  • Type A (Standard) – TVs, monitors
  • Type C (Mini) – Cameras, tablets
  • Type D (Micro) – Smartphones

Video Graphics Array (VGA)

Purpose: Analogue video connector (legacy standard).

Characteristics:

  • Analogue signal (unlike digital HDMI/DVI)
  • 15-pin DE-15 connector
  • Susceptible to interference and signal degradation
  • Being phased out in favour of digital connections
  • No audio support

Limitations:

  • Lower maximum resolution than digital
  • Analogue signal conversion quality issues
  • Bulky connector
  • No hot-plug support (theoretically)

Common uses:

  • Legacy monitors and projectors
  • Older computer systems
  • Backup connector on some equipment

Port Comparison

FeatureUSBHDMIVGA
Signal typeDigitalDigitalAnalogue
Audio supportNo (separate)YesNo
Video qualityN/AExcellentGood (limited)
Hot-pluggableYesYesNo
Max resolutionN/A8K+2048×1536
Power deliveryYes (USB-C)NoNo
Primary usePeripheralsVideo/audioLegacy video

Fetch-Execute Cycle

Basic Stages

The fetch-execute cycle is the fundamental operation of a CPU, repeated for each instruction.

Stage 1: Fetch

Purpose: Get the next instruction from memory.

Steps:

  1. MARPC (Address of next instruction sent to memory)
  2. Control bus signals memory read
  3. Memory places instruction on data bus
  4. MDR ← instruction from data bus
  5. CIRMDR (Instruction transferred to Current Instruction Register)
  6. PCPC + 1 (Increment to point to next instruction)

Register Transfer Notation:

MAR ← PC
[Control bus: Read]
MDR ← [[MAR]]
CIR ← MDR
PC ← PC + 1

Stage 2: Decode

Purpose: Interpret the instruction to determine what needs to be done.

Steps:

  1. Control Unit examines opcode in CIR
  2. Identifies instruction type (ADD, LOAD, etc.)
  3. Identifies addressing mode
  4. Determines what operands are needed
  5. Generates control signals for execute stage

Register Transfer Notation:

[Control Unit decodes CIR]
[Control signals generated]

Stage 3: Execute

Purpose: Perform the actual operation.

Steps (vary by instruction type):

For ADD instruction (direct addressing):

  1. MAR ← address from CIR operand
  2. MDR ← [[MAR]] (fetch operand from memory)
  3. ACCACC + MDR (ALU performs addition)

For LOAD instruction (immediate):

  1. ACC ← operand from CIR

For STORE instruction:

  1. MAR ← address from CIR
  2. MDRACC
  3. Write to memory

Register Transfer Notation (example ADD):

MAR ← [address part of CIR]
MDR ← [[MAR]]
ACC ← ACC + MDR

Complete Cycle Example

Instruction: ADD 100 (Add contents of memory address 100 to ACC)

Initial state:

  • PC = 200 (instruction at address 200)
  • ACC = 5
  • Memory[100] = 3
  • Memory[200] = ADD 100

Fetch:

MAR ← PC (200)
MDR ← [[200]] (ADD 100)
CIR ← MDR (ADD 100)
PC ← PC + 1 (201)

Decode:

CU decodes opcode = ADD
Operand = 100 (direct addressing)
Control signals for ADD

Execute:

MAR ← 100 (from CIR)
MDR ← [[100]] (3)
ACC ← ACC + MDR (5 + 3 = 8)

Result: ACC = 8


Interrupts

Purpose of Interrupts

Definition: A signal to the CPU that an event needs immediate attention.

Why interrupts are needed:

  • Allow CPU to respond to urgent events
  • Enable multitasking
  • Handle I/O efficiently (without polling)
  • Manage errors and exceptions
  • Support real-time processing

Possible Causes of Interrupts

TypeExamplesDescription
Hardware I/OKeyboard press, mouse move, disk readyExternal device needs attention
TimerTime slice expired, real-time clockScheduled event
Program errorDivision by zero, invalid addressSoftware error
Hardware failurePower failure, memory errorSystem problem
Software interruptSystem call, breakpointProgram requests OS service

Applications of Interrupts

Multitasking:

  • Timer interrupt switches between processes
  • Each process gets time slice (timeslicing)

I/O handling:

  • CPU starts I/O operation
  • Device works independently
  • Device interrupts when done
  • CPU handles result without wasting cycles polling

Real-time systems:

  • Guaranteed response to critical events
  • Priority-based interrupt handling

Error handling:

  • Trap errors immediately
  • Prevent system crash
  • Log diagnostic information

Interrupt Detection During Fetch-Execute Cycle

When CPU checks for interrupts:

  • At the end of each fetch-execute cycle
  • Between instruction execution
  • Before fetching next instruction

Process:

  1. Complete current instruction
  2. Check interrupt line on control bus
  3. If interrupt pending, handle it
  4. Otherwise, fetch next instruction

Interrupt Handling Process

Step 1: Interrupt occurs

  • Device sends signal on interrupt request line
  • CPU detects interrupt at cycle end

Step 2: CPU saves current state

  • Completes current instruction (if possible)
  • Saves PC and status register to stack (or known location)
  • This saved state allows resumption later

Step 3: Identify interrupt source

  • CPU checks interrupt vector table
  • Each device has unique interrupt number
  • Vector table contains addresses of ISRs

Step 4: Load Interrupt Service Routine (ISR) address

  • PC ← address of appropriate ISR
  • This is a jump to interrupt handler

Step 5: Execute ISR

  • CPU runs special program to handle the interrupt
  • May disable other interrupts (or allow prioritised ones)
  • Communicates with interrupting device
  • Performs necessary processing

Step 6: Return from interrupt

  • ISR executes special return instruction (IRET)
  • CPU restores saved state (PC, status register)
  • Execution resumes where it left off

Interrupt Service Routine (ISR)

Definition: Special program that handles a specific interrupt type.

ISR characteristics:

  • Short and fast (should not block other interrupts)
  • Saves any registers it uses
  • Re-enables interrupts when safe
  • Ends with return-from-interrupt instruction

Multiple Interrupts

Priority handling:

  • Some interrupts more important than others
  • Higher priority can interrupt lower priority ISRs
  • Example priorities: Power failure > hardware error > I/O > timer

Nested interrupts:

  1. Interrupt A (low priority) starts
  2. Interrupt B (high priority) occurs
  3. CPU saves A’s state
  4. CPU handles B completely
  5. CPU returns to A

Interrupt Vectors and Vector Table

Interrupt Vector Table:

  • Array of ISR addresses
  • Located in low memory
  • Indexed by interrupt number
Address   Contents
0000      ISR0 address (division by zero)
0004      ISR1 address (timer)
0008      ISR2 address (keyboard)
...

Interrupts vs Polling

AspectInterruptsPolling
CPU usageEfficient (only act when needed)Wasted (constant checking)
Response timeFast (immediate attention)Variable (depends on poll frequency)
ComplexityMore complex (hardware + software)Simple
Multiple devicesEasy (priorities, vectors)Must poll each in turn
Real-timeGood (predictable response)Poor (worst-case long)

4.2 Assembly Language

Assembly Language vs Machine Code

Machine Code

  • Binary representation of instructions
  • Directly executable by CPU
  • Difficult for humans to read/write
  • Architecture-specific

Example: 10110000 01100001 (may mean “load 97 into AL”)

Assembly Language

  • Human-readable mnemonics for machine instructions
  • One-to-one mapping with machine code typically
  • Requires assembler to convert to machine code
  • Still architecture-specific

Example: MOV AL, 61h (same instruction)

Relationship

Assembly → [Assembler] → Machine Code → [CPU] → Execution

MOV AL, 61h          10110000 01100001      Loads 97 into AL

Advantages of Assembly over Machine Code:

  • Easier to remember mnemonics (MOV vs 10110000)
  • Labels for memory addresses
  • Comments can be added
  • Less error-prone

Advantages of Assembly over High-Level Languages:

  • Direct hardware control
  • More efficient (if written well)
  • Access to special instructions
  • Required for some system programming

Two-Pass Assembler

Why Two Passes?

Problem: Forward references (using a label before it’s defined)

Example:

      JMP DONE   ; Jump to DONE (not defined yet)
      ...
DONE: MOV AL, 0  ; DONE defined here

The assembler doesn’t know the address of DONE when processing the JMP instruction.

Pass 1: Build Symbol Table

Purpose: Assign addresses to all labels.

Process:

  1. Initialize location counter to 0 (or start address)
  2. Read each line of source code
  3. If line has a label, add to symbol table with current location counter value
  4. Calculate instruction size and add to location counter
  5. Ignore operands (only need to know label definitions)

Example:

LineLabelOpcodeOperandLocation CounterSymbol Table
1STARTLDM#50START=0
2ADDCOUNT1
3LOOPDECACC3LOOP=3
4JPNDONE4
5JMPLOOP5
6DONEOUT6DONE=6
7END7

Pass 2: Generate Machine Code

Purpose: Generate actual machine code using symbol table.

Process:

  1. Reset location counter to 0
  2. Read each line again
  3. Look up any label operands in symbol table
  4. Convert mnemonics to opcodes
  5. Generate machine code
  6. Output object code

Example (continuing from above):

LabelOpcodeOperandMachine CodeNotes
STARTLDM#500000101Immediate value 5
ADDCOUNT00100010COUNT address known
LOOPDECACC10000001
JPNDONE01100110DONE=6 resolved
JMPLOOP01100011LOOP=3 resolved
DONEOUT11110000
ENDNo code generated

Symbol Table Example

LabelAddress
START0
LOOP3
DONE6

Assembly Language Instruction Groups

Data Movement Instructions

Purpose: Move data between registers, memory, and immediate values.

InstructionExampleDescription
LDMLDM #5Load immediate 5 to ACC
LDDLDD 100Load from memory address 100
LDILDI 200Indirect: Use address at 200 to find data
LDXLDX 300Indexed: 300 + IX = effective address
LDRLDR #10Load immediate 10 to Index Register
MOVMOV IXMove ACC to IX
STOSTO 400Store ACC to address 400

Input/Output Instructions

Purpose: Communicate with peripheral devices.

InstructionExampleDescription
ININRead character from keyboard to ACC
OUTOUTOutput character in ACC to screen

Arithmetic Operations

Purpose: Perform mathematical calculations.

InstructionExampleDescription
ADDADD 100Add memory[100] to ACC
ADD #nADD #5Add immediate 5 to ACC
SUBSUB 100Subtract memory[100] from ACC
SUB #nSUB #5Subtract immediate 5 from ACC
INCINC ACCIncrement ACC by 1
DECDEC IXDecrement IX by 1

Compare Instructions

Purpose: Compare values and set flags for conditional jumps.

InstructionExampleDescription
CMPCMP 100Compare ACC with memory[100]
CMP #nCMP #5Compare ACC with immediate 5
CMICMI 200Indirect: Use address at 200 to find compare value

Unconditional Jump

Purpose: Always transfer control to another address.

InstructionExampleDescription
JMPJMP 500Jump to address 500

Conditional Jump Instructions

Purpose: Transfer control only if previous compare was true/false.

InstructionExampleDescription
JPEJPE 500Jump if compare was equal/true
JPNJPN 500Jump if compare was not equal/false

Other Instructions

InstructionExampleDescription
ENDENDReturn control to OS

Addressing Modes

Immediate Addressing

Definition: Operand is the actual value to be used.

Syntax: LDM #5

Operation: ACC ← 5

Advantages:

  • No memory access (fast)
  • Simple

Disadvantages:

  • Constant only (cannot use variables)
  • Limited by instruction size

Use case: Loading constants, initialising registers

Direct Addressing

Definition: Operand is the memory address where the data is located.

Syntax: LDD 100

Operation: ACC ← [100] (contents of address 100)

Advantages:

  • Can access any memory location
  • Simple addressing

Disadvantages:

  • Address fixed at compile time
  • Limited address range (if address field small)

Use case: Accessing global variables, fixed data structures

Indirect Addressing

Definition: Operand is the address of a memory location that contains the actual address of the data.

Syntax: LDI 200

Operation:

temp ← [200]   (get address from 200)
ACC ← [temp]   (get data from that address)

Advantages:

  • Can implement pointers
  • Dynamic addressing

Disadvantages:

  • Two memory accesses (slower)
  • More complex

Use case: Pointers, dynamic data structures, arrays

Indexed Addressing

Definition: Effective address = base address + contents of index register.

Syntax: LDX 300 (assume IX = 10)

Operation: Effective address = 300 + 10 = 310

ACC ← [310]

Advantages:

  • Efficient for arrays
  • Can access sequential memory locations by incrementing IX

Disadvantages:

  • Requires index register management
  • One extra calculation

Use case: Arrays, tables, buffers

Relative Addressing

Definition: Effective address = current PC + offset.

Syntax: (Often used for jumps) JMP 5 (relative to PC)

Operation: PC ← PC + 5

Advantages:

  • Position-independent code
  • Short offsets save space

Disadvantages:

  • Limited range
  • Must know current PC

Use case: Branches, jumps within same code section

Addressing Mode Comparison

ModeEffective AddressMemory AccessesFlexibility
ImmediateN/A (value in instruction)0Low
DirectAddress in instruction1Medium
IndirectAddress from memory2High
IndexedBase + IX1High
RelativePC + offset0-1Medium

Tracing Assembly Programs

Example Program 1: Simple Addition

      LDM #5     ; Load 5 into ACC
      STO 100    ; Store ACC at address 100
      LDM #3     ; Load 3 into ACC
      ADD 100    ; Add contents of address 100 to ACC
      OUT        ; Output result (should be 8)
      END

Trace:

StepPCACCIXMemory[100]Action
Start0?0?Initial state
LDM #5150?Load 5 to ACC
STO 1002505Store ACC to 100
LDM #33305Load 3 to ACC
ADD 1004805ACC ← 3 + 5
OUT5805Output ‘8’
END805Program ends

Example Program 2: Loop

      LDM #0     ; Initialise counter to 0
      STO 200    ; Store at address 200 (counter)
LOOP: LDD 200    ; Load counter
      ADD #1     ; Increment
      STO 200    ; Store back
      CMP #5     ; Compare with 5
      JPN LOOP   ; If not 5, loop again
      OUT        ; Output final count (should be 5)
      END

Partial Trace:

StepPCACCMemory[200]Action
0??Start
LDM #010?
STO 200200
LOOP: LDD 200300
ADD #1410
STO 200511
CMP #5611Compare (not equal)
JPN LOOP311Jump to LOOP
… repeats …
(when ACC=5)55
JPN LOOPNot taken (equal)
OUT55Output ‘5’

4.3 Bit Manipulation

Binary Shifts

Logical Shifts

Logical Shift Left (LSL)

Operation: Shift bits left, zeros introduced on right.

Example: LSL #2 (shift left 2 places)
“`
Before: ACC = 00010111 (23)
After:

Scroll to Top