5

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?

  • 1
    _"why we even bother to allow ourselves to access bytes within individual words"_. For example, an ASCII string uses 1 byte per character. It would be a waste of memory to require characters to be stored 4 bytes apart. _"what we eventually load into the register has to be a complete word anyway"_ It doesn't. Look up `lb` and `lh` in the instruction set reference. – Michael Jan 06 '18 at 16:48
  • Ah. Gotcha. I somehow assumed that it would be silly to store individual bytes and then load them into 4 byte spaces, but I guess I didn't consider memory efficiency... thanks. :-) – Edge of the Prairie Jan 06 '18 at 17:04
  • 2
    Long ago many machine did use word addressing - the IBM 7094 used 36 bit words (6 x 6 bit characters), the CDC 6400 was 60 bit words (10 x 6 bit characters). Getting at individual characters was messy. – greg-449 Jan 06 '18 at 17:07
  • at the bus level as perhaps you know it is words or double words or wider depending on the architecture and system design. some processors are smart enough to see multiple sequential byte reads and combine them into one. or let the L1 cache catch that, which is not as efficient. But what we dont do is we dont punish the word accesses because we are byte addressable. those usually fit nicely on the bus. – old_timer Jan 06 '18 at 18:06
  • Strings, byte arrays (to reduce cache footprint vs. word arrays), and memory-mapped I/O are all important uses of byte loads/stores. The byte store/load instructions really do architecturally only modify a single byte, which is important for multi-threaded code (loading, modifying a byte in a word, and then storing back the whole word can introduce a correctness problem if another thread was doing something with a different byte in the same word). Related: https://stackoverflow.com/questions/46721075/can-modern-x86-hardware-not-store-a-single-byte-to-memory about how CPUs implement byte store – Peter Cordes Jan 07 '18 at 04:22

1 Answers1

7

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.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • word LL/SC isn't sufficient for memory-mapped I/O to peripherals with adjacent byte registers. [Alpha apparently had a special I/O memory space that mapped words to bytes](http://www.tldp.org/HOWTO/Alpha-HOWTO-8.html), and see https://stackoverflow.com/questions/46721075/can-modern-x86-hardware-not-store-a-single-byte-to-memory. Of course, it only needed that because PCI cards were designed for computers with byte load/store (like x86). If no byte-addressable machines existed, people wouldn't have designed MMIO hardware that needed byte loads/stores, so that's not a real answer. :P – Peter Cordes Jan 07 '18 at 04:26