A brief analysis of ARM instructions ADR and LDR

Publisher:码字先生Latest update time:2016-05-11 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The LDR instruction itself is a data read, which loads the value in the memory into the register, because the ARM MOV instruction does not support the direct transfer of data from the memory to the register. The meaning of the LDR instruction is relatively easy to understand. For example:
    LDR R0, _START; means loading the value of the memory location marked by _START into R0.
        However, the ARM assembler gives LDR another pseudo-instruction meaning: for address reading. These are two completely different applications, but both are represented by LDR, so it is easy to confuse them. "For address reading" means writing the value used to represent the "address" into the register, and the register stores the address, which is very similar to a pointer. The usage of LDR as a pseudo-instruction is: add an equal sign in front of a label or an immediate value to indicate the address. For example:
    LDR R0, =_START; means writing the address where the _START label is located into R0
    LDR R0, =0x12345678; means writing the address value 0x12345678 into R0
        Because LDR itself is both an instruction and a pseudo-instruction, and the pseudo-instruction LDR is more difficult to understand, I would like to state here that this article will only discuss the LDR pseudo-instruction, and the so-called LDR is only the LDR used as a pseudo-instruction, and should not be confused with the LDR instruction.
        When LDR is used as a pseudo-instruction, another close relative, the ADR pseudo-instruction, is introduced.
        ADR and LDR are both pseudo-instructions for reading addresses. They both write an address into a register and act like pointers. The value written into the register will be used to address the memory.
        But ADR can only use labels, not immediate values, and there is no equal sign. Its typical usage is:
    ADR R0, _START; means writing the address where the _START label is located into R0
        But there are more fundamental differences between them. ADR reads relative addresses, while LDR reads absolute addresses. Some people may think that this does not matter, because it is just a different way of expressing addresses. Although this is the case, it is a big deal. The storage location of the code is specified by the connection file when it is connected, and it may be moved to different locations during runtime. If each place involving storage addresses in a piece of code is represented by an offset relative to the first address, such as _START+0x800, then this piece of code will run correctly no matter where it is moved in the future. But if each address is a hard-coded absolute address, such as 0x30008000, then this piece of code must be moved to the address position in the memory to run correctly. Therefore, it seems that relative addresses are more flexible than absolute addresses. This is indeed the case. Because ADR is more specific, the compiled code is often more efficient than LDR.
        However, there is a price to pay for being useful. The address reading range of ADR is limited and small, while LDR can read any address value. Why is this so? We have to start with why ADR and LDR are called pseudo-instructions. Pseudo-instructions are because they are not actually executed instructions, but the assembler replaces them with executable ARM instructions during the assembly stage. So what instructions are actually executed?
        If the address range after LDR does not exceed the range of MOV, it will be replaced by MOV instruction by the assembler. If it exceeds the range of MOV, the assembler will open another memory location for it, called memory pool, to store this value. Because the memory can store 32-bit values, this value is taken from the memory pool and given to the register, so that the loading of any address value is realized. The number in the memory pool cannot be changed during operation. What is written is what it is. Therefore, the memory pool is the advantage of LDR, but also its disadvantage. Let's talk about why MOV has a range limit. Because ARM instructions are fixed-length and can only be 32 bits, the MOV instruction word itself must occupy several bits, and the remaining bits can be used for valid data. Therefore, the data that can be stored in the MOV instruction code must not reach 32 bits. If you want to get a 32-bit number, you can only use the memory pool. The ARM instruction description contains a detailed conversion process of MOV, which will not be described in detail.
        The ADR pseudo-instruction can read relative positions at runtime. However, there are range restrictions, because ADR must find the current address position during runtime to use relative addresses. For the ADR pseudo-instruction, the assembler replaces it with addition and subtraction instructions. When encountering ADR, the compiler will use an ADD or SUB current PC register and a constant to implement the function of the ADR pseudo-instruction, thus realizing the reading of the relative current address at runtime. If it cannot be replaced with an instruction, an error will occur and the compilation will fail. Because ADD and SUB encounter the same range problem as MOV, ADR cannot read any 32-bit address like LDR fetches data from the memory pool. To be precise, the address reading range of ADR is:

    When the address value is byte-aligned, the range of its reference is: -255 ~ 255B;
    when the address value is word-aligned, the range of its reference is: -1020 ~ 1020B;

        If you want to implement a larger jump, you can use the ADRL pseudo-instruction. When the assembler compiles the source program, the ADRL pseudo-instruction is replaced by two appropriate instructions. If it cannot be implemented with two instructions, an error will be generated and the compilation will fail.

    When the address value is byte-aligned, the range of its reference is: -64K~64K;
    when the address value is word-aligned, the range of its reference is: -256K~256K;

        By analogy, although the ARM assembler does not provide an ADR pseudo-instruction that can be compiled into three appropriate instructions, imagine that if it can be compiled into three, the address range read by ADR will be larger. But that will reduce the efficiency, so it is still not cost-effective. ARM decisively gave up. If there is a real need, then just convert the address yourself and write it. Although assembly is very low-level, it is not low-level enough.


Reference address:A brief analysis of ARM instructions ADR and LDR

Previous article:Overview of the ARM microprocessor instruction set 4 - the difference between MOV and LDR
Next article:Difference between LDR instruction and LDR pseudo instruction

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号