2

I have a question about the offset units used in MIPS32 instructions. I just noticed that memory access instructions like lw $t0, offset($s1) has offset of unit byte, while branching instructions like beq $r1, $r2, L1 and jump j SOMEWHERE both have unit word.

IIRC, MIPS requires that every data/instructions should be stored aligned in memory at address of multiple of 4, i.e. 0x0, 0x4, 0x8, ... etc. If this is true, then why not let offset in lw/sw of unit word, so the range is larger? Is this because of lb/sb which uses a byte unit? So Special cases aren't special enough to break the rules ["] Good design demands good compromises.[."] applies here?

I also wonder what will happen if I use lw to load data that is at address that is not a multiple of 4?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Kindred
  • 1,229
  • 14
  • 41
  • 1
    The second part of your question is answered in the reference from MIPS / Imagination: _"If either of the 2 least-significant bits of the address is non-zero, an Address Error exception occurs."_ – Michael Nov 29 '18 at 14:41
  • 1
    Dynamically-typed languages often use "must be zero" bits as tags for runtime type checking. For example, you might say "If it's a string, then add 2 to the pointer." To access the string, you do `lw $s0,-2($t0)`. If `$t0` really is a string, then the `-2` in the offset cancels the `+2` in the pointer and the load succeeds. If not, you get a processor exception, and you can convert that to a runtime error. Basically, you let the hardware do the type checking for you. – Raymond Chen Nov 29 '18 at 15:14
  • @RaymondChen: That's really interesting! – Kindred Nov 29 '18 at 15:37
  • I never would have thought to use alignment errors as a form of type checking. That's very clever! – puppydrum64 Dec 14 '22 at 12:04

0 Answers0