Learning Outcomes
SLO | After this lecture you can… | CLO | Bloom |
|---|---|---|---|
SLO 8.1 | Explain byte addressing, alignment and byte order (big- and little-endian), and check whether an access is aligned. | CLO3 | C2 Understand |
SLO 8.2 | Identify the common addressing modes and compute the effective address an instruction uses. | CLO3 | C3 Apply |
SLO 8.3 | Use usage statistics to justify which modes an ISA should support, and map them to the MIPS modes. | CLO3 | C2 Understand |
§1 Memory Addressing
§1.1 Byte addressing and data sizes
Slide L08-S06
Almost every ISA designed since 1980 is byte addressed: every byte (8 bits) has its own address. Bigger objects occupy several consecutive bytes and are named by their lowest address. Real-world: seats in a Daewoo bus are numbered one by one; a family of four in seats 12-15 is booked as 'seat 12'.
Size | Bits | Bytes | MIPS load / store |
|---|---|---|---|
Byte | 8 | 1 | lb, lbu / sb |
Half word | 16 | 2 | lh, lhu / sh |
Word | 32 | 4 | lw / sw |
Double word | 64 | 8 | ldc1 / sdc1 (floating point) |
Because addresses count bytes, element i of a word array is at base + 4i, and element i of a string (byte array) is at base + i (Practical 6).
§1.2 Alignment
Slides L08-S07, S09
An object of s bytes at address A is aligned if A mod s = 0. Memory delivers aligned words in one access; a misaligned word may lie across two memory words, which needs two accesses and extra hardware to join the pieces. MIPS does not allow misaligned lw/sw; x86 allows it but it is slower. Real-world: a car parked across two painted parking spaces blocks both.
Size s | Aligned addresses | Example of misaligned |
|---|---|---|
1 byte | every address | never |
2 bytes (half word) | multiples of 2 | 0x...1, 0x...3 |
4 bytes (word) | multiples of 4 (last hex digit 0, 4, 8, C) | 0x...2, 0x...6 |
8 bytes (double word) | multiples of 8 (last hex digit 0 or 8) | 0x...4, 0x...C |
Real MARS message for lw $t1, 2($t0) with $t0 = 0x10010000:
Runtime exception at 0x00400008: fetch address not aligned on word boundary 0x10010002 |
Access | Address | A mod s | Aligned? |
|---|---|---|---|
Word (s = 4) | 0x10010006 | 2 | No |
Half word (s = 2) | 0x10010006 | 0 | Yes |
Double word (s = 8) | 0x10010008 | 0 | Yes |
Word (s = 4) | 0x1001000C | 0 | Yes |
§1.3 Byte order: little-endian and big-endian
Slide L08-S08
A word has four bytes and four addresses. Little-endian machines put the least significant byte (the 'little end') at the lowest address: x86, MARS and most ARM phones. Big-endian machines put the most significant byte there: IBM mainframes and network byte order in TCP/IP. Real-world: the date 22-10-2026 puts the smallest unit first (little-endian); 2026-10-22 puts the biggest first (big-endian).
0x12345678 stored at 0x100 | 0x100 | 0x101 | 0x102 | 0x103 |
|---|---|---|---|---|
Little-endian | 78 | 56 | 34 | 12 |
Big-endian | 12 | 34 | 56 | 78 |
Byte order matters only when bytes are exchanged between machines, or when a program reads a word as separate bytes. In Practical 6, storing 168496141 (0x0A0B0C0D) and reading offsets 0-3 printed 13 12 11 10 - proof that MARS is little-endian. Example: the half word 0xCAFE at 0x200 on a little-endian machine has FE at 0x200 and CA at 0x201.
§2 Addressing Modes
§2.1 Effective address and the basic modes
Slide L08-S11
An addressing mode is the way an instruction specifies where its operand is. When the operand is in memory, the address actually used is called the effective address (EA).
Mode | Example | Meaning | Typical use | Real-world |
|---|---|---|---|---|
Register | Add R4, R3 | R4 ← R4 + R3 | values in registers | Item already in your hand |
Immediate | Add R4, #3 | R4 ← R4 + 3 | constants | Price printed on the packet |
Displacement | Add R4, 100(R1) | R4 ← R4 + Mem[100 + R1] | local variables, struct fields | Third house after the mosque |
Register indirect | Add R4, (R1) | R4 ← R4 + Mem[R1] | pointers | Note in your pocket with the house number |
Indexed | Add R3, (R1 + R2) | R3 ← R3 + Mem[R1 + R2] | arrays: base + index | Row plus seat in a cinema |
Direct (absolute) | Add R1, (1001) | R1 ← R1 + Mem[1001] | static data | Complete postal address |
§2.2 More complex modes
Slide L08-S12
Mode | Example | Meaning | Typical use | Real-world |
|---|---|---|---|---|
Memory indirect | Add R1, @(R3) | R1 ← R1 + Mem[Mem[R3]] | pointer to a pointer | A letter at house 5 sends you to house 12 |
Autoincrement | Add R1, (R2)+ | R1 ← R1 + Mem[R2]; R2 ← R2 + d | walking through an array | Ticket checker moves to the next seat |
Autodecrement | Add R1, -(R2) | R2 ← R2 - d; R1 ← R1 + Mem[R2] | stack push / walking backwards | Step back, then read |
Scaled | Add R1, 100(R2)[R3] | R1 ← R1 + Mem[100 + R2 + R3 x d] | a[i] for any element size | Row x seats-per-row + seat |
PC-relative | beq R1, R2, label | target = PC + displacement | branches | Two shops ahead of where you stand |
d is the size of the data item (4 for a word). Complex modes reduce the instruction count, but make instructions harder to decode and to pipeline (Lecture 7 and Unit 3).
§2.3 Worked example: effective addresses
Slide L08-S13
Given: R1 = 0x1000 R2 = 0x20 R3 = 4 Mem[0x1000] = 0x2000 d = 4 |
Mode | Operand | Working | EA |
|---|---|---|---|
Displacement | 8(R1) | 0x1000 + 8 | 0x1008 |
Register indirect | (R1) | R1 | 0x1000 |
Indexed | (R1 + R2) | 0x1000 + 0x20 | 0x1020 |
Direct | (0x1004) | address in the instruction | 0x1004 |
Memory indirect | @(R1) | read Mem[0x1000] = 0x2000 (two memory reads) | 0x2000 |
Autoincrement | (R1)+ | use R1, then R1 = 0x1004 | 0x1000 |
Autodecrement | -(R1) | R1 = 0x1000 - 4 = 0x0FFC first | 0x0FFC |
Scaled | 0x10(R1)[R3] | 0x10 + 0x1000 + 4 x 4 | 0x1020 |
Register and immediate modes do not access memory, so they have no effective address.
§3 Which Modes Should an ISA Support?
§3.1 Usage statistics
Slide L08-S15
Hennessy and Patterson (Figure A.7) measured memory addressing modes on a VAX, which has all the modes, for three programs. Register mode (about half of all operand references) is not counted.
Mode | TeX | spice | gcc |
|---|---|---|---|
Memory indirect | 1% | 6% | 1% |
Scaled | 0% | 16% | 6% |
Register indirect | 24% | 3% | 11% |
Immediate | 43% | 17% | 39% |
Displacement | 32% | 55% | 40% |
Displacement + immediate + register indirect | 99% | 75% | 90% |
Three simple modes do almost all the work. This is 'focus on the common case' (Lecture 6) applied to ISA design.
§3.2 Field sizes
Slide L08-S16
Hennessy and Patterson also measured the size of the constants. A displacement field of 12 to 16 bits captures about 75% to 99% of the displacements; an immediate field of 8 to 16 bits captures about 50% to 80% of the immediates. Their recommendation for a new ISA: support displacement, immediate and register indirect, with a displacement field of at least 12-16 bits and an immediate field of at least 8-16 bits. MIPS uses 16 bits for both (range -32768 to +32767); larger constants are built with lui (load upper immediate) plus ori.
§3.3 The MIPS addressing modes
Slides L08-S17, S18
MIPS mode | Example | Used for |
|---|---|---|
Immediate | addi $t1, $t0, 25 | constants |
Register | add $t3, $t0, $t1 | ALU operations |
Base + displacement | lw $t2, 16($s1) | every load and store |
PC-relative | beq $t0, $t1, label | conditional branches |
Pseudo-direct | j label | jumps |
Register indirect is displacement 0: lw $t0, 0($s1). 'lw $t0, x' is a pseudo-instruction. Real MARS Basic column for it:
lui $at, 0x1001 # upper half of the address of x |
PC-relative: a beq at 0x00400008 with offset field 2 branches to (PC + 4) + 2 x 4 = 0x0040000C + 8 = 0x00400014. Pseudo-direct: j with 26-bit field 0x100007 jumps to 0x100007 x 4 = 0x0040001C (the upper 4 bits come from the PC). Both encodings were read from MARS.
Complex modes are built from simple MIPS instructions (tested in MARS with arr = 10, 20, 30, 40, 50 and ptr = address of arr[2]):
# scaled / indexed a[i], i = 3 in $s2, base in $s1 |
§4 Common Mistakes
Slide L08-S20
Wrong idea | Correct idea |
|---|---|
Little-endian reverses all the bits of a word. | Only the order of the bytes changes; the bits inside each byte stay the same. |
Any address is fine for lw. | lw needs A mod 4 = 0; MIPS raises a runtime exception otherwise. |
lw $t0, x is direct addressing in MIPS hardware. | It is a pseudo-instruction: lui $at, then lw with a displacement. |
Memory indirect needs one memory access. | It needs two: one to fetch the address, one to fetch the operand. |
Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Byte addressing | Every byte has its own address | Every bus seat has a number |
Alignment | Address is a multiple of the object size | Car parked inside one painted space |
Endianness | Order of the bytes of a word in memory | Writing a date day-first or year-first |
Effective address (EA) | The memory address an instruction actually uses | The house you finally reach |
Displacement | A constant added to a register | 'Three houses after the mosque' |
Indirect | Using an address stored somewhere to find the data | A note that tells you where to go |
Autoincrement | Use the address, then move it to the next item | Checker moving seat by seat |
Scaled | Index multiplied by the item size | Row number x seats per row |
PC-relative | Address counted from the current instruction | 'Two shops ahead of you' |
Pseudo-instruction | Convenient instruction the assembler turns into real ones | Short form on a form that clerks expand |
Self-Check Questions
- Why is element i of a word array at base + 4i? (§1.1)
- Is a word at 0x1001000A aligned? A half word? Show A mod s. (§1.2)
- Write the bytes of 0xA1B2C3D4 at 0x40 in both byte orders. (§1.3)
- Define effective address. Which modes have none? (§2.1, §2.3)
- R1 = 0x1000, Mem[0x1000] = 0x2000: find the EA of @(R1). How many memory reads? (§2.3)
- R1 = 0x2000, R2 = 3, d = 4: EA of 8(R1)[R2]? (§2.2)
- Which three modes cover 75-99% of memory-mode uses? (§3.1)
- Why did MIPS choose 16-bit displacement and immediate fields? (§3.2)
- Write MIPS code for the autoincrement mode Add R1, (R2)+. (§3.3)
Answers: 0x1001000A: word no (2), half word yes (0); 0x2000, two reads; 0x2014.
Further Reading
- Hennessy and Patterson (2012): Appendix A, section A.3 (memory addressing).
- Patterson and Hennessy, Computer Organization and Design: Chapter 2, section 2.10 (MIPS addressing for 32-bit immediates and addresses).