Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use byte addressing instead of word addressing?

So I'm just starting off in my Organization and Architecture class, and right now we're studying memory addressing in MIPS. I just read about how memory is addressed in bytes, which is fine, and therefore how when we load a word to a register we have to use multiples of 4 to access the memory, which is also fine.

What confuses me, though, is why we even bother to allow ourselves to access bytes within individual words, if what we eventually load into the register has to be a complete word anyway. Why don't we just do word addressing and save ourselves the trouble of multiplying by 4? Is there a reason we might want to get at individual bytes?

like image 319
Edge of the Prairie Avatar asked Jan 06 '18 16:01

Edge of the Prairie


1 Answers

Word-addressed machines used to be very popular up until the 70s. Back then, you had byte (where a byte had between 5 and 10 bits depending on machine) machines for commercial data processing (think banks and insurance companies) whereas scientific machines were word (where a word had between 12 and 60 bits) oriented.

Why did this change though? If all you can do is address words of memory, processing text is rather difficult. To not waste memory, word machines would typically store a bunch of characters in each word. To work on strings, you couldn't just use pointers as strings could begin or end in the middle of a word. This necessitates some rather complicated programming to do text processing.

This was okay back when computers were used to do arithmetic with very little text processing to read the input and print output, but text processing became gradually more important with the onset of interactive computers, completely pushing word machines out of the market.

Word machines are still common in special applications like DSPs (digital signal processors), but they have the significant disadvantage of being unable to be programmed with normal languages without some significant changes in the way you write programs.

As for the question why a CPU needs dedicated byte loads and stores: It doesn't directly need them. For example, early Alpha prcoessors didn't have these instructions. If you wanted to load a byte, you would load a word and use bit shifts and masking operations to fetch the byte you wanted. Similarly, to store a byte, you would fetch a word, mask out the byte you want, or in the byte you want to store and then write the word back to memory. This works, even though it is a bit slower than a dedicated byte load/store instruction.

However, problems appear when you need to do atomic operations on memory. Because you need to load and then store to write a single byte to memory, writing a byte is not an atomic operation. You can fix this by providing load link/store exclusive instructions, but it's easier to just provide (atomic) byte load and stores as these are needed quite often anyway.

like image 153
fuz Avatar answered Oct 22 '22 08:10

fuz