In word-addressable machines, fixed point numbers are naturally handled word-wise by the CPU. This means that any integer is stored in word-sized memory cells and that you have to access a whole word to read or write a single integer. But how about smaller data units as characters, commonly encoded as 8-bits data pieces? Since a word is made of multiple bytes, is the CPU capable of storing multiple characters in a single word while handling them as individual data pieces? For instance, if I store 4 characters (encoded with an 8-bits scheme) in a 32-bits long word, can the CPU understand them as a set with 4 elements? Or it is necessary to store a single character into a whole word? Is there a standard or different architectures deal with this matter differently?
-
\$\begingroup\$ Is there a specific chip/architecture in mind? Otherwise, this doesn't seem to be on-topic here. \$\endgroup\$– Brian CarltonCommented Mar 4, 2017 at 3:12
-
3\$\begingroup\$ I'm voting to close this question as off-topic because not about electronics design. \$\endgroup\$– Brian CarltonCommented Mar 4, 2017 at 3:12
-
\$\begingroup\$ Brian, I posted this question here because I referred to microprocessors in general. It's a question about computers architecture; i.e., hardware. Should I move the question to the CS site? \$\endgroup\$– Humberto Fioravante FerroCommented Mar 4, 2017 at 3:18
-
1\$\begingroup\$ From the help center: "If you ask a vague question, you’ll get a vague answer. But if you give us details and context, we can provide a useful answer." The SE moderation system is geared to shut down broad questions, if you take it to another site the same result may happen, if you do feel the need to post on another site, do not cross post. Please be specific, edit your question and try again. Since this is not a discussion site questions are kept to a Q&A format \$\endgroup\$– Voltage Spike ♦Commented Mar 4, 2017 at 5:49
-
\$\begingroup\$ @Humberto Fioravante Ferro, yes, this is mostly a CS question. However I think the answers below answer your question. Please just accept one. \$\endgroup\$– Brian CarltonCommented Mar 4, 2017 at 15:52
4 Answers
Yes you can. For example, the Texas Instruments MSP430 line are 16 bit processors, and have 16 bit words. A variety of registers are 16 bits long but can be addressed as 8 bit registers as well. It's memory bus is 16 bits wide though. You could add 8 bit bytes to memory without issue. And use both bytes of a memory location independently or together if you want.
-
1\$\begingroup\$ Now that you have mentioned that, I remembered that the multimedia extensions for the x86 processors (MMX and SSE) treat entire words as sets of smaller units. You made me also remember that the first Intel's 16 bits microprocessor allowed to treat a single 16-bits register (say, AX) as two 8-bits pieces (e.g., AH and AL). Therefore, even if the data bus is 16 bits wide (or wider), some CPUs allows you to handle each byte separately. The catch is that you can not do that directly in the main memory, you need first load the word into a CPU's register. Doubt solved. Thank you, @Passerby! \$\endgroup\$ Commented Mar 4, 2017 at 17:16
If you're programming in C, a character is normally the same size as the smallest addressable unit of memory. If the smallest thing you can address is a 16-bit or 32-bit word, then that's what a char will be. In most modern architectures, chars are 8 bits and can be packed into a 32-bit word in memory. But in a few (some DSPs) larger bytes are used.
It's always possible to work on a piece of a word that's smaller than a byte using AND, OR, and shift operations. In that way, you could pack ASCII values into 8 bits regardless of how the addressing works. That wouldn't be very portable or easy to work with, though.
Sounds a lot like you're talking about c style unions. Combined with some struct magic, this lets you interpret the same collection of bytes in different ways while maintaining the types. The compiler does the magic of bitshifting/bitmasking for you.
union data{
struct{
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
uint8_t byte4;
} bytes_individual;
uint32_t bytes_all;
}
union storage;
storage.bytes_all = 0x04030201;
storage.bytes_individual.byte1 == 0x04; // true
storage.bytes_individual.byte2 == 0x03; // true
// etc
I assumed a little-endian cpu here, so the least significant address is stored first, or "LSB first".
I referenced this link if you want to research a bit more: https://www.tutorialspoint.com/cprogramming/c_unions.htm
If you are looking to compress data like this, that is up to you. The Micro will do what you tell it to do. It may even have instructions for manipulating 8 bit slices of data. There may already be libraries out there for doing so. It may be that if you are using a C compiler/lib that is written to do such with char size data and you don't even have to handle it with low level routines.
But if you are working with a Cortex, you probably have enough RAM that this is not a concern.