Array address in MIPS

In Section 2.14 (pp 141) in Patterson and Hennessy (5th Ed), the authors write that "to set array[i] to 0, we must first get its address. Start by multiplying i by 4 to get the byte address: loop1: sll $t1, $t0, 2".

I do not understand why we must multiply by 4 to get the byte address. Could it be because 1 word = 32 bits, which is 4 bytes?

1 answer

  • answered 2018-02-13 02:36 Ped7g

    Yes, memory [chip] is addressable by individual bytes, so the very first word occupies four addresses: 0, 1, 2, 3. Each is 8 bits = 1 byte, together they form 32 bit value = 1 word (in MIPS world word = 32 bits). The order of them can be configured IIRC, but usually little-endian is assumed, so when loading whole word, the four bytes 0x12 0x34 0x56 0x78 form a word value 0x78563412.

    So to read consecutively by-words you have to adjust your address by +-4. If you have byte array, then the address can be adjusted by +-1, and half-words occupy 2 bytes, so +-2 addressing would do.