Byte Alignment


Byte Alignment, actually is designed for realizing the convenience of memory addressing, to improve the speed of addressing. To understand it, we gonna talk about the difference between using Byte Alignment and non-Byte Alignment:

struct A { 
    char c; 
    int i; 
}; 
struct A a;

We use the structure above to explain the function of Byte Alignment. Without Byte Alignemnt, the address of variable c is 0x00, and the address of i is 0x01. If the addressing scope  of cpu is 4 bytes, we will need two read cycles to get i. Variable c need a read cycle to be read, however variable i need two read cycle to be read because there are three bytes of i in 0x01, 0x02, 0x03 which are read in the variable c read cycle. So when the cpu read the 0x04, it has to connect the three bytes 0x01, 0x02, 0x03. As we see, we need two read bytes to get i, it is low efficient.
However, if we set the byte alignment=4, problem will be solved, because by doing this, we make 0x01, 0x02, 0x03 in vain and set 0x04 as the address of variable i. And variable i can be read for just one cycle. Although three bytes have been wasted, we improve the efficience of addressing.

Comments

Popular posts from this blog

Basic understanding of TLS-PSK protocol

Differences between ASIC, ASSP and ASIP

Orthogonal instruction set