Working status of arm

Publisher:DelightfulWishLatest update time:2016-04-14 Source: eefocusKeywords:arm Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
From a programming perspective, arm has two working states and can be switched:

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 memory space in modern computers is divided into bytes. Theoretically, it seems that access to any type of variable can start from any address, but the actual situation is that when accessing a specific type of variable, it is often accessed at a specific memory address. This is 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

      The compiler on each specific platform has its own default "alignment coefficient" (also called alignment modulus). Programmers can change this coefficient through the pre-compilation command #pragma pack(n), n=1,2,4,8,16, where n is the "alignment coefficient" you want to specify.
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
      Below we briefly review and explain the above alignment rules, and analyze them with examples:
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   pack(n) to set the variable to n-byte alignment. n-byte alignment means that there are two cases for the offset of the starting address where the variable is stored. 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.
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;
};
  Similarly, in Example C, the member variable b has its own alignment value of 1 and the specified alignment value of 2, so the effective alignment value is 1. Assuming that C starts at 0x0000, b is stored at 0x0000, which complies with 0x0000%1=0 and satisfies the byte alignment principle.
  The member variable a has its own alignment value of 4 and the specified alignment value of 2, so the effective alignment value is 2, which is stored in four consecutive bytes at 0x0002, 0x0003, 0x0004, and
  0x0005, which complies with 0x0002%2=0 and satisfies the byte alignment principle. The member variable c has its own alignment value of 2, which is equal to the specified alignment value, so the effective alignment value is 2, which is stored in 0x0006 and 0x0007, which complies with 0x0006%2=0 and satisfies the byte alignment principle.
  The eight bytes from 0x0000 to 0x00007 store the variables of structure C. The alignment value of structure C itself is 4, which is larger than the specified alignment value of 2, so the effective alignment value of C is 2. Since 8%2=0, C only occupies eight bytes from 0x0000 to 0x0007. Therefore, sizeof(struct C)=8, which fully meets the byte alignment principle.
  In addition to the different specified alignment values ​​that can cause different addresses of data structures to be stored, different compilers may also store structures differently.

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.

    Following the above method is called aligned, and not following this method is called unaligned storage access operation.

 

V. ARM platform byte alignment keywords
1. __align(num)
  is used to modify the byte boundary of the highest level object.
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
  aligned, but cannot make a 4-byte object 2-byte aligned.
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
  is used to modify the variable so that it can be accessed in an unaligned manner.


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
  For the data structures used locally by 32-bit processors, four-byte alignment is used to improve memory access efficiency; at the same time, in order to reduce memory overhead, the positions of structure members are reasonably arranged to reduce the gaps between members caused by four-byte alignment and reduce memory overhead.
  For data structures between processors, it is necessary to ensure that the length of the message does not change due to different compilation platforms and different processors. The message structure is compacted using one-byte alignment; to ensure the memory access efficiency of the data structure of the message between processors, byte padding is used to align the members in the message by four bytes.
  The position of the members of the data structure should take into account the relationship between members, data access efficiency and space utilization. The principle of sequence arrangement is: four bytes are placed at the front, two bytes are next to the last four-byte member, one byte is next to the last two-byte member, and padding bytes are placed at the end. For example:
typedef struct tag_T_MSG{
long ParaA;
long ParaB;
short ParaC;
char ParaD;
char Pad;
} T_MSG; 


Keywords:arm Reference address:Working status of arm

Previous article:7 working modes of arm processor
Next article:Why does arm9 need to change the system clock when it is powered on?

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号