Location-independent Code location correlation code

Source: Internet
Author: User

Using location-independent codes in the Assembly

B,bl

Using location-independent codes in the C language

Do not use global variables without static variables

Location Independent code: When the CPU refers to, always relative to the relative address of the execution instruction of this article to fetch the reference. For example, when an add command is used, the PC needs to remove the address of an instruction on the original basis +4. This does not matter where your code is placed in the memory, as long as their relative address does not change, they can execute the program normally. General power-on reset the few statements must be location-independent code directives.

Location Correlation code: In this case, the CPU each time the finger is taken from the absolute position, rather than the relative position above. The absolute address is the relative start address, the zero. In this way, you will be required to store the program, you must give the connection script as stipulated, put the code in the specified location

1.A in the first place, B in the second position.
2.A is in front of B.
In both cases, the positional relationship between A and B is indicated, and the first one is relative to the location, and the second case is independent of the position and belongs to the opposite address.

Location-independent and location-related code is about the ARM program in the jump when the two ways of addressing, in general, both methods can achieve the goal of jumping to the destination address, but in some specific circumstances, the two jump methods to obtain the results of greater correlation.

Location-independent code of the jump can be performed by the "B or BL designator" command, when the implementation of the B or BL command to jump, the actual binary code in the jump is performed: Calculate the executable code in the target address to the current PC value of the distance, then the distance value of the current PC value, and then assign to the PC register to jump, The Jump method because the address field of the instruction is only 26 bits, so it can only forward/backward addressing the 32M bit address, if the target address to the current PC value is greater than this value, can use pseudo-directive "ADR and ADRL pc,= label address" to achieve the jump, the actual execution process is to calculate the target address to the current PC distance , and then the current PC value plus distance value to the target register PC, to achieve jump, ADR and B or BL function similar, but ADRL can achieve a full range of jumps, which by the target address and the current PC distance value stored in a cache word, and then the value of the cache word loaded into the Register PC.

Location-related jumps can also be called absolute position jumps, generally using the pseudo-instruction Ldr Pc,=label implementation to jump to the label of the link address, the absolute address is the link address. The difference between location-related and location-independent is essentially the difference between a link address and a running address.

In bare-metal code with no operating system, if the offset base of the link is 0, the absolute position jumps are the same as the relative position jumps, and for some programs that copy the executable code into RAM, the link address is not the same as the starting run address. A generic link address is a numeric value within a RAM logical address, while the running address is the logical address of a memory device such as Flash, which is written by code, and is typically a 0 address. The offset address of the link can be specified by the link script or makefile at compile time.

After the executable code from flash to ram copy, through the LDR pc,=label instruction to execute the program in memory, and before this code jump must be location-independent, or there will be a run-fly exception.

B label BL label//position independent jump to label

ADR R0,label ADRL R0,=label//Get the label offset distance from the current program

ADR Pc,label ADRL Pc,=label//Position independent jump to label

LDR Pc,=label//position-related absolute position jump

LDR R0,=label//Gets the offset distance of the label to the current program, note that this is not the absolute address to get the label (link address)

LDR r0,=0x500000c0

LDR R1,[r0]

LDR r2,=0x00

STR R2,[r0]

LDR R0,=label loads the label data into the register R0, if the label is greater than 255 the LDR is translated into the above location-independent pseudo-instruction, if less than 255 is directly with the LDR instructions for the immediate number of addressing mode

Reference:

Http://blog.chinaunix.net/uid-10696433-id-2935776.html

http://m.blog.csdn.net/blog/zhaigch/39433601

Location-independent Code location correlation code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.