0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- A 4-byte branch at 2000 jumps to 1960. PC-relative offset? from Lesson 1.2.2
- Data memory accesses for indirect mode? from Lesson 1.2.1
- How many instructions does an expression need on a zero-address machine (A + B) × (C − D)? from Lesson 1.1.2
1 · The Hook
🎣
GATE often lists C statements on one side and addressing modes on the other and asks you to match them. The trick is to ask two questions of every operand: where is the address known (at compile time, in a register, in memory?) and does it change as the program runs?
2 · Predict First
🤔
while (*p++ != 0) count++; walks through a string using pointer p. Which addressing mode lets a single instruction both read the character and move p forward?
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
💡
Predict First — the answer
Auto-increment (Rp)+: use M[Rp], then add the element size to Rp.
Matching modes to program constructs
| Construct | Where the address is | Best mode |
|---|---|---|
Constant x = 5 | no address: the value is known | Immediate |
Global variable g | fixed address known at compile time | Direct |
| Variable already in a register | none | Register |
*p, p in a register | in a register | Register indirect |
*p, p in memory | in memory | (Memory) indirect |
A[i], A global, i in a register | A in instruction + i | Indexed |
Record field r.f, address of r in a register | register + fixed offset | Base / displacement |
| Local variables in a block whose start is in a register | register + small offset | Base / displacement |
*p++, stepping through an array | register, then step | Auto-increment |
| Push onto a stack growing downward | register, step first | Auto-decrement |
| Branch within code that may be loaded anywhere | PC + offset | PC-relative |
Data memory accesses per mode (to get one operand)
| Mode | Accesses |
|---|---|
| Immediate, register | 0 |
| Direct, register indirect, indexed, base, PC-relative, auto-inc/dec | 1 |
| Indirect | 2 |
An EA that needs an addition (indexed, base, PC-relative) costs adder time but no extra memory access.
The running example: the array-sum loop
sum = 0; for (i = 0; i < n; i++) sum = sum + A[i]; with A a global array of 4-byte integers at address 1000.
Indexed version (R2 holds 4i):
LOOP: LOAD R4, 1000(R2) ; R4 ← A[i]
ADD R1, R1, R4 ; sum
ADD R2, R2, #4 ; next element
Auto-increment version (R2 holds the address of A[i]):
LOOP: LOAD R4, (R2)+ ; R4 ← A[i], R2 ← R2 + 4
ADD R1, R1, R4
The auto-increment version saves one instruction per iteration.
Address of an array element
A[i] with element size s starting at base B: address = B + i × s. A[25] of 4-byte integers at 1000 → 1000 + 100 = 1100.
4 · Visual
Pick the mode by asking: where is the address, and does it move?
value known now ............................ immediate
address fixed, known now ................... direct
address in a register ...................... register indirect
address in memory .......................... indirect
fixed start + changing index ............... indexed
register start + fixed offset .............. base / displacement
address moves by one element each use ...... auto-increment / auto-decrement
relative to where the code is .............. PC-relative
5 · Worked Examples
Example 1 Easy
total = total + 7: which mode supplies the 7?
Solution
Immediate.
Example 2 Medium
How many data memory accesses does this sequence make? LOAD R1, @X · ADD R1, R1, #3 · STORE R1, 8(R5).
Solution
Indirect load 2 + immediate 0 + displacement store 1 = 3 data accesses (plus 3 instruction fetches if counting all references).
Example 3 GATE
Match: (P) A[i] = B[j], (Q) while (*a++), (R) int t = *x where x is a pointer stored in memory. Modes: (1) indirect, (2) indexed, (3) auto-increment.
Solution
P–indexed (2), Q–auto-increment (3), R–indirect (1).
6 · GATE Traps
⚠️
- A pointer in memory needs indirect; a pointer in a register needs register indirect.
- Relocation is served by PC-relative (and base-register) modes, not direct mode.
- Auto-decrement suits push on a downward-growing stack; auto-increment suits pop.
- An address addition is not a memory access.
7 · Memory Hook
🧠
"Where is the address, and does it move?" Two questions pick every mode.
8 · Connect the Dots
Chapter 1.2 is complete: every way an operand field leads to data. Chapter 1.3 runs whole programs: tracing values, counting instructions, memory references and cycles, then subroutines and the stack.
9 · Self-Test
GATE format. Try every question before opening the vault.
Q1MCQ2 marksMatching
Match: (P) A[i] = B[j]; (Q) while (*p++); (R) t = *x with pointer x stored in memory; (S) k = 5. Modes: (1) immediate, (2) indirect, (3) auto-increment, (4) indexed.
- (A) P-4, Q-3, R-1, S-2
- (B) P-4, Q-3, R-2, S-1
- (C) P-3, Q-4, R-2, S-1
- (D) P-2, Q-3, R-4, S-1
Q2MCQ1 markRelocation
Which mode best supports code that runs correctly wherever it is loaded in memory?
- (A) PC-relative
- (B) Indirect
- (C) Direct
- (D) Immediate
Q3NAT2 marksCounting accesses
Count all memory accesses (one-word instructions; fetches included): LOAD R1, @X · LOAD R2, 20(R3) · ADD R1, R1, R2 · STORE R1, (R4).
Numerical answer: ________
Q4MSQ2 marksEA with addition
Which modes compute the effective address with an addition?
- (A) Register indirect
- (B) Base (displacement)
- (C) Indexed
- (D) PC-relative
Q5NAT1 markElement address
A global array of 8-byte doubles starts at address 4000. What is the address of element D[12]?
Numerical answer: ________
10 · Rule Card
- Constant → immediate; fixed global → direct; pointer in register → register indirect; pointer in memory → indirect.
- A[i] → indexed; record field / locals → base + displacement; *p++ → auto-increment; push → auto-decrement.
- Relocatable code → PC-relative (and base register).
- Data accesses: immediate/register 0; indirect 2; all others 1.
- Element address = base + i × size.
11 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
mode-to-construct matchingarray element address B + i·sarray-sum loop (indexed and auto-increment forms)
12 · Answer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Recall Warm-up
- −44.
-
-
Self-Test
Q1 Answer: (B)2 marks
Arrays with an index: indexed. Pointer stepping: auto-increment. Pointer in memory: indirect. Constant: immediate.
Re-read: Lesson 1.2.3 — Choosing modes: arrays, pointers, constants, relocatable code; memory references per mode
Q2 Answer: (A)1 mark
Targets are relative to the current PC, so moving the code moves the targets with it.
- (B) is wrong: The stored addresses would need updating.
- (C) is wrong: Direct addresses are fixed.
- (D) is wrong: Immediate holds constants, not code addresses.
Re-read: Lesson 1.2.3 — Choosing modes: arrays, pointers, constants, relocatable code; memory references per mode
Q3 Answer: 82 marks
Fetches 4; data: 2 + 1 + 0 + 1 = 4; total 8.
Re-read: Lesson 1.2.3 — Choosing modes: arrays, pointers, constants, relocatable code; memory references per mode
Q4 Answer: (B) (C) (D)2 marks
Register indirect uses the register as is.
- (A) is wrong: No addition.
Re-read: Lesson 1.2.3 — Choosing modes: arrays, pointers, constants, relocatable code; memory references per mode
Q5 Answer: 40961 mark
4000 + 12 × 8 = 4096.
Re-read: Lesson 1.2.3 — Choosing modes: arrays, pointers, constants, relocatable code; memory references per mode