The first is the arm state, where the processor executes 32-bit word-aligned arm instructions
The second is the thumb state, where the processor executes 16-bit half-word aligned thumb instructions.
Switch at any time without affecting the processor working mode and the corresponding register content
-------------------------------------------------- ---
The following content is extracted from http://www.cnblogs.com/yin-jingyu/archive/2011/10/14/2211252.html
1. What is word alignment? Why do we need word alignment?
The reasons for byte alignment are roughly the following two:
1. Platform reasons (porting reasons): Not all hardware platforms can access any data at any address; some hardware platforms can only access certain types of data at certain addresses, otherwise a hardware exception will be thrown.
2. Performance reasons: Data structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that in order to access unaligned memory, the processor needs to make two memory accesses; while aligned memory access only requires one access.
2. Alignment Rules
Rules:
1. Data member alignment rule: The first data member of the data member of the structure (struct) (or union) is placed at offset 0, and the alignment of each subsequent data member is based on the smaller of the value specified by #pragma pack and the length of the data member itself.
2. Overall alignment rule of the structure (or union): After the data members have completed their own alignment, the structure (or union) itself must also be aligned. The alignment will be based on the smaller of the value specified by #pragma pack and the maximum data member length of the structure (or union).
3. Combining 1 and 2, we can infer that: First, if n is greater than or equal to the number of bytes occupied by the variable, then the offset must meet the default alignment; second, if n is less than the number of bytes occupied by the type of the variable, then the offset is a multiple of n and does not need to meet the default alignment.
3. X86 Alignment Experiment
1. The alignment value of the data type itself: For char type data, its alignment value is 1 byte, for short type it is 2 bytes, and for int, float, double type, its alignment value is 4 bytes.
2. The alignment value of the structure itself: the value with the largest alignment value among its members.
3. Specify the alignment value: #pragma
4. The effective alignment value of data members and structures: the smaller value of the alignment value of the data members (data types) and data structures and the specified alignment value. If the data members are aligned, the data structure will naturally be aligned.
With the above four basic concepts, we begin to discuss the members of a specific data structure and its own alignment. The effective alignment value N is the value that is ultimately used to determine the data storage address. Effective alignment N means "aligned on N", that is, the data's "storage start address % N = 0". The data variables in the data structure are arranged in the order of definition. The starting address of the first data variable is the starting address of the data structure. The member variables of the structure must be aligned, and the structure itself must be rounded according to its own effective alignment value (the total length occupied by the member variables of the structure must be an integer multiple of the effective alignment value of the structure). The following is a deeper understanding of the example of the compilation environment in VS2005:
Example B Analysis:
struct B
{
char b;
int a;
short c;
};
Assume that B starts at address space 0x0000. In this example, the alignment value N is not explicitly specified, and the default value of VS2005 is 4.
The member variable b has its own alignment value of 1, which is smaller than the specified or default alignment value of 4, so the effective alignment value is 1, and its storage address 0x0000 complies with 0x0000%1=0, which meets the byte alignment principle.
The member variable a has its own alignment value of 4, which is equal to the specified or default alignment value of 4, so the effective alignment value is also 4. In order to ensure byte alignment, the member variable a can only be stored in four consecutive byte spaces starting at addresses 0x0004 to 0x0007, and 0x0004%4=0 is verified.
The member variable c has its own alignment value of 2, which is smaller than the specified or default alignment value of 4, so the effective alignment value is 2, and it can be stored in two byte spaces from 0x0008 to 0x0009 in sequence, which complies with 0x0008%2=0.
So far, the byte alignment of the data members has been met, and then the alignment of the data structure B is considered. The alignment value of the data structure B is the largest alignment value (that is, the member variable b) 4 among its variables, so the effective alignment value of the structure B is also 4. According to the requirement of structure rounding, 0x0009 to 0x0000 = 10 bytes, (10 + 2) % 4 = 0. Therefore, 0x0000A to 0x000B is also occupied by structure B. Therefore, B from 0x0000 to 0x000B has a total of 12 bytes, sizeof (struct B) = 12.
The reason why 2 bytes are added to variable C is to enable the compiler to quickly and effectively access the structure array. Imagine if the B structure array is defined, the starting address of the first structure is 0, which is no problem, but what about the second structure? According to the definition of the array, all elements in the array are adjacent. If the size of the structure is not added to an integer multiple of the alignment value (4), the starting address of the next structure will be 0x0000A, which obviously cannot meet the address alignment of the structure.
Analysis of Example C:
__align(2) struct C
{
char b;
int a;
short c;
};
4. Alignment issues on ARM platforms
In ARM, there are two types of instructions: ARM and Thumb.
ARM instructions: Each time an instruction is executed, the value of PC increases by 4 bytes (32 bits). To access 4 bytes at a time, the starting address of the byte must be aligned to a 4-byte position, that is, the lower two bits of the address are bits [0b00], which means that the address must be a multiple of 4.
Thumb instruction: Each time an instruction is executed, the value of PC increases by 2 bytes (16 bits). To access 2 bytes at a time, the starting address of the byte must be aligned to 2 bytes, that is, the lower two bits of the address are bits [0b0], that is, the address must be a multiple of 2.
V. ARM platform byte alignment keywords
1. __align(num)
A. When using LDRD or STRD in assembly, this command __align(8) is used to modify the limit. To ensure that the data object is aligned accordingly.
B. The maximum limit of the command to modify the object is 8 bytes, which can make a 2-byte object 4-byte
C. __align is a storage class modification. It only modifies the highest level type object and cannot be used for structure or function objects.
2. __packed
__packed is a one-byte alignment.
A. Packed objects cannot be aligned;
B. All object read and write accesses are unaligned;
C. float and structure unions containing float and objects that are not __packed will not be byte aligned;
D. __packed has no effect on local integer variables;
D. Forcing the conversion from unpacked objects to packed objects is undefined, and integer pointers can be legally defined
as packed __packed int* p; //__packed int is meaningless.
3. __unaligned
VI. How to find problems with byte alignment
If alignment or assignment problems occur, first check:
1. The big little end settings of the compiler;
2. See if the system itself supports non-aligned access;
3. If it supports, see if the alignment is set or not. If not, see if some special modifications are needed to mark the special access operation.
VII. Conclusion
typedef struct tag_T_MSG{
long ParaA;
long ParaB;
short ParaC;
char ParaD;
char Pad;
} T_MSG;
Previous article:7 working modes of arm processor
Next article:Why does arm9 need to change the system clock when it is powered on?
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- MPS gives benefits | Order online at the mall, get a JD card, and 8,000 yuan worth of gifts are waiting for you!
- Description of the implementation process of the 2019 National Undergraduate Electronic Design Competition
- Discussion on the Reasons for the Temperature Drop of MOS Tubes Connected in Parallel
- The easiest way to achieve the LED breathing light gradually brightening and fading effect, without the need for single-chip control
- Why is the IRQ pin of PN532 useless?
- Half the size, twice the power! - Gallium nitride technology revolutionizes robotics, renewable energy, telecommunications and more
- WIFI+Bluetooth chip
- FPGA is too difficult. Too difficult.
- [NXP Rapid IoT Review] Sensor data reading and display
- How to measure the output ripple of a switching power supply.pdf