Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are memory addresses incremented by 4 in MIPS?

Tags:

memory

mips

If something is stored at 0x1001 0000 the next thing is stored at 0x1001 0004. And if I'm correct the memory pieces in a 32-bit architecture are 32 bits each. So would 0x1001 0002 point to the second half of the 32 bits?

like image 604
shashu10 Avatar asked May 14 '12 02:05

shashu10


People also ask

Why is the PC often incremented by 4 after an instruction is executed?

In the middle of the machine cycle the PC is incremented by four so that it points to the instruction that follows the one just fetched. Then the fetched instruction is executed and the cycle repeats. The machine cycle automatically executes instructions in sequence.

Why is the program counter incremented by 4?

Because the machine is byte addressable as opposed to bit addressable; therefore adding 4 advances the pointer by 32 bits. The address bus wants the location in bytes.

Why is memory address 4 bytes?

And the memory address are linear and each byte will have a unique address. To store a character, the computer will allocate 1 byte of memory which is 1 * 8 = 8 bit. To store an integer or a float, the computer will allocate 4 byte of memory which is 4 * 8 = 32 bit.

How does memory work in MIPS?

MIPS memory is byte-addressable, which means that each memory address references an 8-bit quantity. The MIPS architecture can support up to 32 address lines. ❖ This results in a 232 x 8 RAM, which would be 4 GB of memory. ❖ Not all MIPS machines will actually have that much!


2 Answers

First of all, memory addresses in MIPS architecture are not incremented by 4. MIPS uses byte addressing, so you can address any byte from memory (see e.g. lb and lbu to read a single byte, lh and lhu to read a half-word).

The fact is that if you read words which are 32 bits length (4 bytes, lw), then two consecutive words will be 4 bytes away from each other. In this case, you would add 4 to the address of the first word to get the address of the next word.

Beside this, if you read words you have to align them in multiples of 4, otherwise you will get an alignment exception.

In your example, if the first word is stored in 0x10010000 then the next word will be in 0x10010004 and of course the first half/second half would be in 0x1001000 and 0x1001002 (the ordering will depend on the endianness of the architecture).

like image 123
gusbro Avatar answered Oct 12 '22 13:10

gusbro


You seem to have answered this one yourself! 32 bits make 4 bytes, so if you're e.g. pushing to a stack, where all elements are pushed as the same size, each next item will be 4 bytes ahead (or before) the next.

like image 23
Asherah Avatar answered Oct 12 '22 13:10

Asherah