MIPS Addressing Modes

“Draw all MIPS addressing modes” (Q3(a)) — there are five — plus the branch/jump address arithmetic they’re built on.

The five modes

#ModeExampleWhere the operand is
1Register (R-type)add $s1, $s2, $s3in a register
2Base / displacementlw $s1, 8($s3)memory at (register + offset)
3Immediateaddi $s1, $s2, 7a constant inside the instruction
4PC-relativebeq $s1, $s2, LabelPC+4+4×offset\text{PC} + 4 + 4 \times \text{offset}
5Pseudodirectj Labeltarget from the 26-bit field

Mnemonic: Register, Base, Immediate, PC-relative, Pseudodirect → “RBI-PP”.

How each is drawn

op6 bitsrs5 bitsrt5 bitsimmediate16 bits
1. Immediate — operand = the field itself
op6 bitsrs5 bitsrt5 bitsrd5 bits5 bitsfunct6 bits
2. Register — Registers[rd/rs/rt]
op6 bitsrs5 bitsrt5 bitsoffset16 bits
3. Base — + Register(rs) → Memory[reg + offset]
op6 bitsrs5 bitsrt5 bitsoffset16 bits
4. PC-relative — + PC → Memory[PC + offset]
op6 bitsaddress26 bits
5. Pseudodirect — : PC → Memory[(PC⁰⁴)::addr::00]

Branch address arithmetic

beq/bne $rs, $rt, Label are I-type. The 16-bit offset counts instructions (words), relative to the next instruction.
branch destination=(PC+4)+4×offset\text{branch destination} = (\text{PC} + 4) + 4 \times \text{offset}
offset=branch destinationPC44\text{offset} = \frac{\text{branch destination} - \text{PC} - 4}{4}

PC is the 32-bit Program Counter — it holds the address of the instruction currently executing. Normally it advances by PC=PC+4\text{PC} = \text{PC} + 4; if the branch is taken, PC=(PC+4)+4×offset\text{PC} = (\text{PC}+4) + 4 \times \text{offset}.

Worked value (from the slides)

Given PC = 200, $s0 = 8, $s1 = 8, and beq $s0,$s1,Label with offset = 1: $s0 == $s1 → branch taken.

New PC=(PC+4)+4×offset=(200+4)+4×1=208\text{New PC} = (\text{PC} + 4) + 4 \times \text{offset} = (200 + 4) + 4 \times 1 = 208
If the condition were false, PC=PC+4=204\text{PC} = \text{PC} + 4 = 204 instead. The offset field is the number of instructions between the branch and the target.

Encoding a branch (from the slides)

beq $s0, $s1, Label ($s0 = 16 = 10000, $s1 = 17 = 10001, offset = 1):

000100op6 bits10000rs5 bits10001rt5 bits0000000000000001offset (16)16 bits
beq $s0, $s1, Label

Jump (J-type)

j Label uses word-relative addressing: the 26-bit field is a word address. Example j Label with Label at 100 bytes = 25 words → field = 25, op = 000010:

000010op6 bits0000000000000000000001100126-bit word address (25)26 bits
j Label (target word 25)