Memory Location, Access & Addressing Modes — Theory Recap
Memory as an Array
Main memory = array of adjacent cells. Each cell stores 1 bit (0 or 1). Cells are grouped into words — the smallest addressable entity (16–64 bits). Memory size is expressed in bytes.
Example: 256 MB = 256 × 2²⁰ = 2²⁸ bytes.
Memory Write Operation — 3 Steps
- CPU loads the word to be stored into the Memory Data Register (MDR).
- CPU loads the destination address into the Memory Address Register (MAR).
- CPU issues a WRITE signal → word in MDR is stored at address in MAR.
Note: MDR and MAR are CPU-internal registers, not accessible to the programmer.
Memory Read Operation — 3 Steps
- CPU loads the source address into the MAR.
- CPU issues a READ signal → memory will place the word at that address into MDR.
- After memory access delay, the word is loaded into MDR, ready for CPU use.
| Class | Form | Example | Meaning |
|---|---|---|---|
| Three-address | OP src1, src2, dst | ADD R1,R2,R3 | R3 ← R1 + R2 |
| Two-address | OP src, dst | ADD R1,R2 | R2 ← R1 + R2 |
| One-and-half-address | OP mem, reg | ADD B,R1 | R1 ← M[B] + R1 |
| One-address | OP src | ADD R1 | Racc ← Racc + R1 |
| Zero-address | Uses stack | ADD (SP)+,(SP) | Pop two, push sum |
Memory, Instruction Classification & Addressing Modes
✔ Model Answer
Definitions:
- Bit: The smallest unit of information in a computer, capable of storing a binary value of 0 or 1. [1 mark]
- Byte: A group of 8 bits that can be treated as an atomic unit. It is the standard unit for expressing memory size. [1 mark]
- Word: The smallest addressable entity in memory, consisting of n bits that can be stored or retrieved in a single memory operation. Typical sizes range from 16 to 64 bits. [1 mark]
Address bits for 512 MB:
512 MB = 512 × 2²⁰ = 2⁹ × 2²⁰ = 2²⁹ bytes
Maximum capacity with 32-bit address bus:
✔ Model Answer
A memory write operation stores a word from the CPU into a specified memory location. The two key registers involved are the Memory Data Register (MDR) and the Memory Address Register (MAR).
Three Steps of a Memory Write:
- Load MDR: The word to be written is loaded by the CPU into the Memory Data Register (MDR). The MDR acts as a temporary buffer holding the data to be stored. [2 marks]
- Load MAR: The address of the memory location where the word is to be stored is loaded into the Memory Address Register (MAR). The MAR holds the destination address. [2 marks]
- Issue WRITE Signal: The CPU issues a WRITE control signal. This triggers the memory system to store the word currently in the MDR into the memory location whose address is held in the MAR. [2 marks]
Register Roles (Table):
| Register | Full Name | Role in Write Operation |
|---|---|---|
| MDR | Memory Data Register | Holds the data word to be written to memory |
| MAR | Memory Address Register | Holds the address of the target memory location |
✔ Model Answer
Three Steps of a Memory Read:
- Load MAR: The address of the memory location from which the word is to be read is loaded into the MAR. [1 mark]
- Issue READ Signal: The CPU issues a READ control signal, indicating that the word at the address held in MAR should be transferred into the MDR. [1 mark]
- Data Available in MDR: After a period equal to the memory access delay, the memory system places the requested word into the MDR, where it is ready for CPU processing. [2 marks]
Key Difference between READ and WRITE:
| Aspect | Write Operation | Read Operation |
|---|---|---|
| Direction | CPU → Memory | Memory → CPU |
| MDR Role | Holds data to BE stored | Receives data READ from memory |
| Signal Issued | WRITE signal | READ signal |
| MDR loaded by | CPU (before operation) | Memory (after access delay) |
✔ Model Answer
| Class | Example | Meaning | Equivalent 3-address |
|---|---|---|---|
| Three-address | ADD R1,R2,R3 | R3 ← R1 + R2; R1, R2 unchanged | Itself |
| Two-address | ADD R1,R2 | R2 ← R1 + R2; R2 is overwritten | ADD R1,R2,R2 |
| One-and-half | ADD B,R1 | R1 ← M[B] + R1 | ADD B,R1,R1 |
| One-address | ADD R1 | Racc ← Racc + R1 (accumulator is implicit) | ADD R1,Racc,Racc |
| Zero-address | ADD (SP)+,(SP) | Pop two values from stack, push their sum | Uses stack mechanism |
Why "One-and-half" Address?
The instruction uses a memory location (full address bits needed) AND a register (fewer bits needed). Since the memory operand requires a full address while the register needs only a small field, the combined addressing requirement is described as "one-and-a-half" addresses.
Stack Operation (Zero-address) — Push & Pop:
- PUSH: Store a value at address pointed to by Stack Pointer (SP), then increment SP.
- POP: Decrement SP first, then retrieve the value at that address. LIFO — last in, first out.
✔ Model Answer
Addressing mode: The method by which the address of an operand is specified in an instruction. Different modes offer trade-offs between instruction size, flexibility, and speed.
| # | Mode | Definition | Example | Effective Address / Operation |
|---|---|---|---|---|
| 1 | Immediate | Operand value IS the instruction field. No memory access needed. | LOAD #1000, Ri | Ri ← 1000 (no EA, value is direct) |
| 2 | Direct (Absolute) | Instruction field contains the memory address of the operand. | LOAD 1000, Ri | EA = 1000; Ri ← M[1000] |
| 3a | Register Indirect | A named register holds the effective address. Notation: (Rj) | LOAD (Rj), Ri | EA = Rj; Ri ← M[Rj] |
| 3b | Memory Indirect | A memory location holds the effective address. Two memory accesses. | LOAD (1000), Ri | EA = M[1000]; Ri ← M[M[1000]] |
| 4 | Indexed | EA = constant X added to content of index register Rind. | LOAD X(Rind), Ri | EA = Rind + X; Ri ← M[Rind+X] |
| 5 | Relative | Same as indexed but the Program Counter (PC) is used as the base. | LOAD X(PC), Ri | EA = PC + X; Ri ← M[PC+X] |
Key Comparison — Indirect vs. Indexed:
- Indirect: One extra memory access to get the address. Flexible for dynamic pointer-like access.
- Indexed: Uses arithmetic (addition) to compute the address. Ideal for array traversal — increment X to access successive elements.
✔ Model Answer
| Instruction | Mode | Effective Address | Result |
|---|---|---|---|
| LOAD #500, R3 | Immediate | No EA — value is #500 | R3 ← 500 (constant) |
| LOAD 500, R3 | Direct/Absolute | EA = 500 | R3 ← M[500] |
| LOAD (R5), R3 | Register Indirect | EA = R5 = 2000 | R3 ← M[2000] |
| LOAD (500), R3 | Memory Indirect | EA = M[500] = 3000 | R3 ← M[3000] |
| LOAD 20(R6), R3 | Indexed | EA = R6 + 20 = 1020 | R3 ← M[1020] |
✔ Model Answer
| Aspect | Register Indirect | Memory Indirect |
|---|---|---|
| Address stored in | A CPU register (e.g. Rj) | A memory location (e.g. address 1000) |
| Memory accesses | 1 (to get operand) | 2 (to get address, then operand) |
| Notation | LOAD (Rj), Ri | LOAD (1000), Ri |
| Speed | Faster (register access is fast) | Slower (extra memory access) |
Advantage of Memory Indirect:
Allows access to a larger address space — the pointer can be stored anywhere in memory, unlike register indirect which is limited by the number of registers available.
Disadvantage of Memory Indirect:
Requires an extra memory read cycle to fetch the effective address, making it significantly slower than register indirect addressing.
Instruction Set Architecture & Assembly Programming — Theory Recap
| Type | Purpose | Examples |
|---|---|---|
| Data Movement | Transfer data between registers, memory, stack | MOV Ri,Rj LOAD STORE PUSH POP |
| Arithmetic & Logical | Computation on register/memory contents | ADD R1,R2,R0 SUB AND OR SHIFT ROTATE |
| Sequencing (Control) | Alter the program counter — change execution flow | JUMP addr BRANCH-IF-Z CALL RETURN |
| Input/Output | Transfer data between CPU and peripheral devices | INPUT 1000 OUTPUT 2000 or memory-mapped |
| Flag | Name | Set to 1 when... |
|---|---|---|
| N | Negative | Result of most recent operation is negative |
| Z | Zero | Result of most recent operation is zero |
| V | Overflow | Most recent operation caused arithmetic overflow |
| C | Carry | Most recent operation generated a carry out |
JUMP (Unconditional Branch)
Loads PC with a new address. The previous PC value is LOST. Execution continues at the new address with no way to return automatically.
CALL (Subroutine Call)
Same as JUMP but FIRST pushes the incremented PC (return address) onto the stack. When the subroutine executes RETURN, it pops the return address from the stack back into PC — resuming at the instruction after the CALL.
Simple Processor Registers
| Register | Full Name | Function |
|---|---|---|
| PC | Program Counter | Address of the NEXT instruction to execute |
| IR | Instruction Register | Holds the OPCODE of the current instruction |
| AR | Address Register | Holds the ADDRESS portion of current instruction |
| AC | Accumulator | Implicit source and destination for data operations |
| DR | Data Register | Temporary data holder |
Intel x86 Registers (as covered in course)
| Register | Function |
|---|---|
| AX / AL / AH | Accumulator — main data register (AL = low byte, AH = high byte) |
| BX | Base register — used for memory addressing |
| CX | Counter register — used for loop counting |
| DX | Data register — data operations and I/O |
Assembly Program Structure (8086 DOS)
ISA, Instruction Types & Assembly Programming
✔ Model Answer
1. Data Movement Instructions [3 marks]
Transfer data between registers, between registers and memory, or to/from the stack. They do NOT modify the data.
- MOV Ri, Rj — Copies content of Ri into Rj. Rj is overwritten; Ri unchanged.
- LOAD 25838, Rj — Loads content of memory address 25838 into register Rj. Memory unchanged.
- STORE Ri, 1024 — Stores content of Ri into memory location 1024. Ri unchanged.
2. Arithmetic & Logical Instructions [3 marks]
Perform computation or logical manipulation on register/memory contents.
- ADD R1,R2,R0 — R0 ← R1 + R2. Source registers unchanged.
- SHIFT R1 — Performs logical left or right shift on R1.
- AND R1,R2 — Bitwise AND of R1 and R2, stored in R2.
3. Sequencing / Control Instructions [3 marks]
Change the order of instruction execution by modifying the Program Counter (PC).
- JUMP NEW-ADDRESS — Unconditional: PC ← NEW-ADDRESS. Previous PC value is lost.
- BRANCH-IF-ZERO Loop — Conditional: if Z flag = 1, PC ← Loop address.
- CALL SubroutineX — Pushes return address, PC ← SubroutineX.
4. Input/Output Instructions [3 marks]
Transfer data between the processor and external peripheral devices through dedicated ports.
- INPUT 1000 — Reads data from the input device at port address 1000 into the accumulator.
- OUTPUT 2000 — Sends data from the accumulator to the output device at port address 2000.
✔ Model Answer
The Condition Code (CC) Register is a special-purpose register whose individual bits (flags) are set or cleared based on the results of executing various instructions. It is used by conditional branch instructions to decide whether to take a branch.
| Flag | Meaning | Set to 1 when |
|---|---|---|
| N | Negative | Most recent operation result is negative |
| Z | Zero | Most recent operation result is zero |
| V | Overflow | Most recent operation caused an arithmetic overflow |
| C | Carry | Most recent operation generated a carry-out |
Example: Assembly Loop Using Conditional Branching
The DECREMENT instruction updates the Z flag. If R1 is not yet zero (Z=0, meaning result > 0), the branch is taken and the loop continues. When R1 reaches 0, Z=1, the branch is NOT taken, and execution falls through.
✔ Model Answer
| Feature | JUMP | CALL |
|---|---|---|
| Effect on PC | PC ← NEW-ADDRESS | PC ← Subroutine address |
| Saves return address? | NO — previous PC lost | YES — pushes (PC+1) onto stack |
| Can return? | No automatic return | Yes — via RETURN instruction |
| Use case | One-way unconditional branch | Subroutine / function call |
Why RETURN is needed after CALL:
When CALL executes, the incremented PC value (pointing to the instruction AFTER the CALL) is pushed onto the stack. The RETURN instruction pops this saved address from the stack back into the PC, thereby resuming execution exactly where the program left off before the subroutine was called.
Without RETURN, execution would continue inside the subroutine indefinitely or fall into incorrect memory. CALL + RETURN together implement the function call mechanism.
✔ Model Answer
Dedicated I/O: The CPU uses special I/O instructions (INPUT, OUTPUT) with dedicated port addresses that are separate from the memory address space.
Memory-mapped I/O: I/O device registers are mapped into the same address space as regular memory. The CPU interacts with I/O devices using standard data movement instructions (MOVE, LOAD, STORE) — no separate INPUT/OUTPUT instructions needed.
| Feature | Dedicated I/O | Memory-Mapped I/O |
|---|---|---|
| Special instructions? | Yes (INPUT, OUTPUT) | No — uses MOVE/LOAD/STORE |
| Address space | Separate I/O address space | Shared with memory |
| Complexity | Simpler hardware | Simpler instruction set |
Advantage of memory-mapped I/O: Any instruction that can reference memory can be used to access I/O devices, giving the programmer the full power of the instruction set for I/O operations without needing special I/O instructions.
✔ Model Answer
| Directive | Meaning |
|---|---|
| .MODEL SMALL | Uses "Small" memory model: 1 code segment + 1 data segment (max 64KB each). Other models: TINY, MEDIUM, LARGE. |
| .STACK 100H | Reserves 256 bytes (100 hex) for the stack. Stores temporary data, function return addresses. |
| .DATA | Marks the beginning of the data segment. Variables declared here with DB (Define Byte), DW (Define Word), etc. |
| .CODE | Marks the beginning of the code segment where executable instructions are written. |
| PROC / ENDP | Marks the start and end of a procedure (subroutine / function). |
| MOV AH, 4CH + INT 21H | Terminates the program. 4CH = DOS terminate function. INT 21H = invokes DOS interrupt handler which checks AH and exits cleanly. |
✔ Model Answer
(a) Addition — Two numbers from memory [5 marks]
(b) Subtraction — 9 minus 4 [5 marks]
✔ Model Answer
How CMP works internally:
CMP performs a subtraction (AL − BL) but does NOT store the result. It ONLY updates the Condition Code flags (N, Z, V, C) based on the outcome. These flags are then examined by a subsequent conditional jump instruction.
| CMP Result | Flags Set | Branch to use |
|---|---|---|
| AL > BL | Z=0, N=0 | JG (Jump if Greater) |
| AL = BL | Z=1 | JE (Jump if Equal) |
| AL < BL | N=1 | JL (Jump if Less) |
✔ Model Answer
Assembly language is a low-level programming language that uses mnemonic instructions (e.g., MOV, ADD, SUB) that map directly to machine code instructions. An assembler translates assembly code into binary machine code.
Advantages [3 marks]:
- Efficiency: Programs run faster and use less memory since the programmer has direct control over hardware resources with no abstraction overhead.
- Direct hardware control: Allows precise manipulation of registers, memory addresses, and I/O ports — essential for OS kernels, device drivers, and embedded systems.
- Size optimisation: Code can be tuned for minimal footprint, critical in microcontrollers and ROM-based systems.
Disadvantages [2 marks]:
- Low portability: Code written for one CPU architecture (e.g., x86) must be completely rewritten for another (e.g., ARM).
- Harder to maintain: Programs are longer, more complex, and harder to read/debug compared to equivalent high-level language code.
Common Use Cases:
Operating systems, embedded systems, device drivers, microcontrollers, and game engines (performance-critical sections).
Cross-Unit Exam Questions & Calculation Problems
✔ Model Answer
(a) Number of addressable words:
(b) Total capacity in bytes:
(c) Capacity in MB:
✔ Model Answer
(a) Instruction class: One-and-half-address instruction. [2 marks]
It is called "one-and-half" because it uses a memory operand (B — requires full address bits) and a register operand (R1 — requires fewer bits for a register field).
(b) Addressing modes: [2 marks]
- Operand B: Direct (Absolute) addressing — B is the address of a memory location.
- Operand R1: Register addressing — R1 is a CPU register.
(c) Equivalent three-address instruction: [2 marks]
(d) Effect on operands: [2 marks]
- Contents of memory location B: UNCHANGED — B is only read, not written to.
- Contents of register R1: OVERWRITTEN — R1 receives the result (M[B] + R1). The original value of R1 is lost.
✔ Model Answer
Strategy: The simple processor uses AC as accumulator and DR as data register. ADD always does: AC ← AC + DR.
The result A+B+C is stored in memory location "Result". This demonstrates why one-address (accumulator-based) architectures require more instructions than three-address architectures for the same computation.
✔ Model Answer
A stack is a LIFO (Last-In, First-Out) data structure. The Stack Pointer (SP) always points to the top of the stack — the next usable location.
PUSH Operation (storing value 5A):
| Step | Action | SP value | Memory |
|---|---|---|---|
| Before PUSH | SP points to location 1023 | 1023 | 1023: empty |
| During PUSH | Store 5A at address SP (1023) | 1023 | 1023: 5A |
| After PUSH | Increment SP | 1024 | 1023: 5A ✓ |
POP Operation (retrieving value DD from 1021):
| Step | Action | SP value | Result |
|---|---|---|---|
| Before POP | SP = 1022 | 1022 | — |
| Step 1 | DECREMENT SP first | 1021 | — |
| Step 2 | Read value at M[1021] = DD | 1021 | Register ← DD |
✔ Model Answer
CISC — Complex Instruction Set Computer:
CISC architectures support many instruction classes (including one-and-half and three-address forms) and rich addressing modes (including memory indirect). A single instruction can perform multiple memory accesses. This reduces the number of instructions per program but each instruction may take multiple clock cycles.
RISC — Reduced Instruction Set Computer:
RISC architectures limit instructions to simple, single-cycle operations. Only LOAD and STORE access memory; all arithmetic uses register operands only (three-address register-to-register). Addressing modes are minimal (typically immediate and register indirect only).
| Feature | CISC | RISC |
|---|---|---|
| Addressing modes | Many (all 6 in this course) | Few (immediate + register indirect) |
| Instruction classes | All 5 (0 to 3 address) | Primarily 3-address (register) |
| Memory access | Many instructions access memory | Only LOAD/STORE touch memory |
| Code density | Compact (fewer instructions) | More instructions, but simpler each |
Relevance to Course Material:
The course material covers a range of addressing modes and instruction classes consistent with a CISC-like instructional model (especially memory indirect, indexed, and one-and-half address formats). However, the simple processor mnemonics (LD, ST, ADD) and the emphasis on the accumulator model lean toward an early RISC/simple processor philosophy. Understanding both is essential because modern CPUs blend both approaches.
Master Summary Tables — Bring This to Mind During the Exam
| Mode | Instruction Form | Effective Address | Operation |
|---|---|---|---|
| Immediate | LOAD #K, Ri | No EA | Ri ← K |
| Direct | LOAD A, Ri | EA = A | Ri ← M[A] |
| Register Indirect | LOAD (Rj), Ri | EA = Rj | Ri ← M[Rj] |
| Memory Indirect | LOAD (A), Ri | EA = M[A] | Ri ← M[M[A]] |
| Indexed | LOAD X(Rind), Ri | EA = Rind + X | Ri ← M[Rind+X] |
| Relative | LOAD X(PC), Ri | EA = PC + X | Ri ← M[PC+X] |
| Mnemonic | Operand | Meaning |
|---|---|---|
STOP | — | Stop execution |
LD | x | AC ← M[x] (load from memory) |
ST | x | M[x] ← AC (store AC to memory) |
MOVAC | — | DR ← AC |
MOV | — | AC ← DR |
ADD | — | AC ← AC + DR |
SUB | — | AC ← AC − DR |
AND | — | AC ← AC AND DR |
NOT | — | AC ← complement of AC |
BRA | adr | Jump to address adr (unconditional) |
BZ | adr | Jump to adr if AC = 0 (conditional) |
| Instruction | Meaning |
|---|---|
BRANCH-IF-CONDITION | Transfer to new address if condition flag is true |
JUMP | Unconditional transfer — old PC is lost |
CALL | Transfer to subroutine — return address saved on stack |
RETURN | Pop return address from stack back into PC |