tailieunhanh - ARM System Developer’s Guide phần 4

Có hai cách để thực hiện cấu trúc này một cách hiệu quả trong lắp ráp ARM. Phương pháp đầu tiên sử dụng một bảng các địa chỉ chức năng. Chúng tôi tải máy từ các bảng được lập chỉ mục của x. Ví dụ mã switch_absolute thực hiện một chuyển đổi bằng cách sử dụng một bảng sắp xếp theo các con trỏ chức năng: | 198 Chapter 6 Writing and Optimizing ARM Assembly Code case 1 return method_1 case 2 return method_2 case 3 return method_3 case 4 return method_4 case 5 return method_5 case 6 return method_6 case 7 return method_7 default return method_d There are two ways to implement this structure efficiently in ARM assembly. The first method uses a table of function addresses. We load pc from the table indexed by x. Example The switch_absolute code performs a switch using an inlined table of function pointers x RN 0 int switch_absolute int x switchabsolute CMP x 8 LDRLT pc pc x LSL 2 B method_d DCD method_0 DCD method_1 DCD method_2 DCD method_3 DCD method_4 DCD method_5 DCD method_6 DCD method7 The code works because the pc register is pipelined. The pc points to the method_0 word when the ARM executes the LDR instruction. The method above is very fast but has one drawback The code is not position independent since it stores absolute addresses to the method functions in memory. Positionindependent code is often used in modules that are installed into a system at run time. The next example shows how to solve this problem. Example The code switch_relative is slightly slower compared to switch_absolute but it is position independent int switch_relative int x switchrelative Efficient Switches 199 CMP x 8 ADDLT pc pc x LSL 2 B method_d B method_0 B method_1 B method_2 B method_3 B method_4 B method_5 B method_6 B method7 There is one final optimization you can make. If the method functions are short then you can inline the instructions in place of the branch instructions. Example Suppose each nondefault method has a four-instruction implementation. Then you can use code of the form CMP x 8 ADDLT pc pc x LSL 4 each method is 16 bytes long B method_d method_0 the four instructions for method_0 go here method_1 the four instructions for method_1 go here . continue in this way . Switches on a General Value x Now suppose that x does not lie in some convenient

TỪ KHÓA LIÊN QUAN