Document directory
- 3.1 CISC and RISC
- 3.2 align data access
- 3.3 example
- 3.4 Countermeasures
Talking about alignment (bottom) 3 Data Alignment 3.1 CISC and RISC
There are two types of CPU in terms of instruction sets: CISC and RISC. CISC is abbreviated as Complex Instruction Set Computer and Reduced Instruction Set Computer.
The CPU operation can be seen as a loop of the following steps:
- Step 1: Obtain the command
- Step 2: retrieve data
- Step 3: execute commands
- Step 4: output results
The cisc cpu supports many addressing modes, so the time to fetch data is uncertain. The biggest feature of the CPU is that it simplifies the addressing mode of commands. Besides the load/store commands, all other commands adopt register addressing, that is, reading and writing data from registers. This design makes the data retrieval time relatively stable and simplifies the design of the command line.
Generally speaking, the CPU architecture can reduce the complexity of the CPU and allow the generation of more powerful CPUs at the same process level, but there are higher requirements for compiler design.
3.2 align data access
The load/store command of the CPU of the Proteus requires data to be aligned. Data with a length of 4 should be placed on the 4N boundary, and data with a length of 2 should be placed on the 2N boundary. Take the arm cpu load as an example:
LDR R5,[R4]LDRSH R7,[R6]LDRB R9,[R8]
LDR, ldrsh, and ldrb read one word, half word, and byte from the memory and put them in the specified register. For example, "LDR R5, [R4]" is to read a word (4 in length) from the storage unit pointed to by R4 and put it in R5. LDR requires that the data address be on the 4N boundary. Otherwise, an error occurs. Ldrsh requires that the data address be on the 2n boundary. Otherwise, an error occurs.
What are the errors? This is related to the specific CPU. on ARM7TDMI, Non-Aligned access will cause the program to jump to the processing vector with incorrect data access, that is, the address 0x00000010. On ARM920T, The LDR command may return incorrect data.
The CPU of CISC supports non-aligned Data Reading.
3.3 example
Let's look at an example:
// Example 1 void test (void) {char a [] = {1, 2, 3, 4, 5}; int * Pi, I; printf ("& A [1] = % P/N", & A [1]); Pi = (int *) & A [1]; I = * PI; printf ("% 08x/N", I );}
The key is:
i = *pi;
We know that the four bytes that the address PI points to are 0x02,0x03,0x04,0x05. On the CPU at the end, we expect 05040302 output. Let's take a look at the running effect of this Code on different platforms.
3.3.1 PC/Windows
The output result is:
&a[1]=0012FF2505040302
As expected, the CPU of the PC supports non-alignment Data Reading.
3.3.2 PC/Linux
The output result is:
&a[1]=0xbfa0c36c05040302
It is worth noting that the GCC compiler places local variable A on the 1N boundary (0xbfa0c36b. We want pi to be an odd address and modify the test code:
// Example 2 void test (void) {int A [] = {0x04030201, 0x08070605}; int * Pi, I; Pi = (int *) & (char *) & A) [1]; printf ("Pi = % P/N", Pi); I = * PI; printf ("% 08x/N", I );}
The output result is:
pi=0xbfe87fe905040302
In line with our expectations. Data Alignment is a CPU issue and has nothing to do with compilers and operating systems.
3.3.3 ARM920T/Linux
The output result is:
&a[1]=0xbec49e5501040302
Taking into consideration the small tail, the four bytes actually read by the CPU are 0x01 in turn. This result is not what we expected, and the CPU has an error.
3.3.4 ARM7TDMI
The program is executing:
i = *pi;
Directly jump back to the processing vector of Data abort, that is, the address 0x00000010.
3.4 Countermeasures
The compiler automatically generates code that is read by byte when you read a consortium or consortium. We only need to be careful when performing forced pointer conversion. We should not forcibly convert the pointer to narrow data into a pointer to wide data. In the case of Data Alignment problems, read data by byte.
4 Conclusion
I really appreciate a novel called "Rose name. This is a detective novel, but it gives me a lot of programming inspiration. Priest William could not solve the mystery in the maze, but he had no idea about the truth in the maze. I also tend to debug programs in my mind. The debugger is just a last resort. In this situation, the objects to be tested may be changed or confused by the appearance. From the outside, through imagination and reasoning, it is sometimes easier to discover the truth or grasp the focus of debugging. This article discusses some details related to alignment. Understanding more details helps us to form a clearer program image in our minds.