Most of the students who are learning arm commands are confused about the ADR and LDR commands. What are the differences between them?
In fact, both are pseudo commands: ADR is a small range of address read pseudo commands, LDR is a large range of read address pseudo commands. In fact, ADR uses the address value based on the relative offset of the PC or the instruction based on the relative address value of the Register, and LDR is used to load 32 as the immediate number or an address to the specified register. Here we will see the difference. If you want to load a function in a program or specify an address during connection, use ADR, for example, the address to be located in the LDS. Use LDR when loading the 32-bit immediate number or external address.
Let me give you an example:
Area test, code, readonly
Entry
LDR r0, _ start
ADR r0, _ start
LDR r0, = _ start
NOP
_ Start
NOP
End
This code has no practical significance, just for convenience. Let's take a look at the disassembly:
4: LDR r0, _ start
0x00000000 e59f0008 LDR r0, [PC, #0x0008]
5: ADR r0, _ start
0x00000004 e28f0004 add r0, PC, #0x00000004
6: LDR r0, = _ start
0x00000008 e59f0004 LDR r0, [PC, #0x0004]
7: NOP
8:
9:
10: _ start
0x0000000c e1a00000 NOP
11: NOP
LDR r0, _ start
Read the value from the memory address _ start. After executing this command, R0 = 0xe1a00000
ADR r0, _ start
Get the address of _ start to r0, but please refer to The Decompilation result, it is irrelevant to location. In fact, the relative position obtained. For example, if this code is run in 0x00000000, R0 = 0x00000010 is obtained for ADR R0 and _ start;
LDR r0, = _ start
Obtain the absolute address of _ start. This absolute address is determined during link. It seems that this is only an instruction, but it takes up two 32bit spaces. One is the instruction and the other is the data of _ start (because the value of _ start cannot be determined during compilation, in addition, the mov command cannot be used to assign a 32bit constant to R0. Therefore, an extra space is required to store the real data of _ start. Here it is 0x0000000c ).
Therefore, we can see that this is an absolute addressing. No matter where the code runs, the result is R0 = 0x0000000c.