Position independent and related jump

Important concept

1. Link address: the address specified when linking (designation method: use -Text, or link script)

   Run address: the actual run-time address of the program (designation method: determined by where the actual run-time is loaded into the memory)

   Link address and running address: may be the same or different

 Load address: the address of the program in the memory (NAND, NOR, etc.)

2. Position-independent code: depending on the current PC value of the program, a relative jump (the compiler automatically finds the offset of the target address relative to the current PC value and jumps relative to the current PC value ) The result is that no matter where the code is, the normal purpose of the instruction can always be achieved, so it is position-independent

   Position related code: It does not depend on the current PC value, it is an absolute jump. Only when the program runs at the link address (the compiler obtains the absolute address of the jump destination according to the link script ) can the normal purpose of the instruction be achieved, so it is the position related

 

Important instructions

1. Position-independent jump: B instruction-B instruction accepts a relative address. When using B to jump to a label in assembly, the actual compilation result is a relative jump. The jump range cannot be too far, offset must in 32 Mbit

        BL instruction--BL instruction is used to call a function, it is a relative jump, same as B

        ADR instruction--ADR instruction gets the label address, and it will use the PC + offset method to get the address of the location during compilation

        LDR instruction--When LDR instruction does not add "=", it means position-independent jump

2. Position related jump: LDR instruction, LDR PC, = LABLE, relative jump, related to code position and link address, jump to absolute address

 

Important notes on the LDR instruction

The LDR instruction has the following three methods of use, described as follows:

  1.LDR r1, # 100 Load immediate data to the specified register

  2.LDR r1, = label stores the address value of the label label in the specified register

  3.LDR r1, label stores the value in the label address into the specified register

 

Examples

1. Note: ARM9 is a 3-stage pipeline, that is, the processor is executing the first instruction while decoding the second instruction, and the third instruction is taken out of the memory, as shown in the figure below, PC The register always points to the value of the third instruction (that is, the processor is executing instruction 1 and the value of the PC register is the address value of instruction 3)

 

 

2.makefile

 

 

 

Source code

 

Disassembly

 

 

 

  (1) ldr r0, on_sdram, disassembly is ldr r0, [PC, # 4], because the current instruction address is 4, the PC register value is 4 + 8, the offset offset is 4, all instructions will be 4 + 8 The value of the address +4 (that is, the machine code e3a0d30d) is stored in r0 (note the disassembled [] brackets). In general, the use case of ldr r0, label is 

  ldr r0,label

  label:

    .word 0x10000000

  Put a value in a register

  (2) ldr r0, = on_sdram, disassembly is ldr r0, [PC, # 24], ldr r0, = label disassembly is ldr instruction without ldr "=", the compiler according to the link address, in the text code segment The blank address at the end stores the target address value ( such as 0x10 at the 0x28 address, which is actually the target address value ). Calculate the offset to get the absolute address (the offset is automatically calculated by the offset compiler), the actual is the current running address 8 + PC register address 8 + offset # 24 = 40 (0x28), and finally put the value 0x10 in address 0x28 into r0. If the instruction is ldr pc, = on_sdram, it is to put 0x10 in the PC register, which realizes the jump.

 

 

 

 

 

the above,

2020/04/17

 

Guess you like

Origin www.cnblogs.com/IamLoser/p/12717346.html