views:

124

answers:

4

I am having problems with memory addressing in MIPS. It says that the addressing is word aligned... in the text below I don't understand why it's looking at the 2 least significant bits of the address? why? can someone give me an example to clarify/illustrate the point made here... so is it saying that a valid halfword address are all whose 2 least significant bits are either 00 or 10?

so what if I want to load a byte from a memory which is word aligned?? how can I do this? it is said that I need to shift left by 2, i.e make the least 2 significant bits 0... then extract the bits...

alt text

A: 

A half-word aligned address ends in binary 0 (divisible by decimal 2, the number of bytes in a half-word). A word-aligned address ends in binary 00 (divisible by decimal 4, the number of bytes in a word).

Yes, the wording and chart are a bit obtuse.

Cylon Cat
+1  A: 

'Word aligned' means that the CPU will always read 4 bytes.

However, if the operation is actually on a 2-byte short, it is legitimate to have an address ending 0b00 or 0b10 (so the address is even) and the CPU will poke the correct 2 bytes into the register when loading into a register or write the correct information.

Similarly, when reading a character, the two least significant bits can take any value, and the correct byte is loaded into the relevant register or the relevant part (usually the LSB) of the register is written to the correct part of the memory.

However, if you attempt to read a (4-byte) int with an address that does not end 0b00, then you will get a SEGV or thereabouts.

Jonathan Leffler
A: 

I remember running into this issue when working with MIPS. My suggestion would be to load the whole word first, then look at the two least significant bits and use them to determine which 8 bits out of the 32 you actually want to load.

Question, what exactly are you attempting to do? Are you writing MIPS assembly code? Or are you trying to implement a MIPS processor in hardware?

If you're just writing assembly code for MIPS, it's my understanding that you can use lb with any memory address and it will handle it properly. The only time you need to worry about proper alignment is when you use the lw instruction.

Michael Patterson
A: 

If you want to read a single byte you can read from every location in the memory. Nothing to worry. The word alignment only plays it role when you want to read more than one byte at once.

If you want to read a 4-byte value that crosses the word boundary you can do it with 4 single-byte reading operations, shifting and ORing. It is much slower than simple read.

Muxecoid