• arm ldrb指令


    目录

    ldrb指令

    1. 语法

    2. Memory access mode

    3. 详细说明

    ldr指令

    1.语法

    ldrex指令



    ldrb指令

    1. 语法

    armv7手册语法:

    1. LDRB{<c>}{<q>} <Rt>, [<Rn> {, #+/-<imm>}] Offset: index==TRUE, wback==FALSE
    2. LDRB{<c>}{<q>} <Rt>, [<Rn>, #+/-<imm>]! Pre-indexed: index==TRUE, wback==TRUE
    3. LDRB{<c>}{<q>} <Rt>, [<Rn>], #+/-<imm> Post-indexed: index==FALSE, wback==TRUE
    4. <Rt> The destination register.
    5. <Rn> The base register. The SP can be used. For PC use see LDRB (literal) on page A8-418.
    6. +/- Is + or omitted if the immediate offset is to be added to the base register value (add == TRUE), or – if
    7. it is to be subtracted (add == FALSE). #0 and #-0 generate different instructions.
    8. <imm> The immediate offset used for forming the address. For the offset addressing syntax, <imm> can be
    9. omitted, meaning an offset of 0. Any value in the range 0-4095 is permitted.

     功能描述:

    1. Load Register Byte (immediate) calculates an address from a base register value and an immediate offset, loads a
    2. byte from memory, zero-extends it to form a 32-bit word, and writes it to a register. It can use offset, post-indexed,
    3. or pre-indexed addressing. For information about memory accesses see Memory accesses on page A8-291.

    核心:从某个地址加载一个字节(Byte)寄存器 Rt中,根据memory access方式不同分为:

    • Offset addressing
    • Pre-indexed addressing
    • Post-indexed addressing

    2. Memory access mode

    Offset addressing:

     Offset addressing:

    The offset value is applied to an address obtained from the base register. The result is used as the address for the memory access. The value of the base register is unchanged. The assembly language syntax for this mode is:    [, ]

    Pre-indexed addressing :

    Pre-indexed addressing
    The offset value is applied to an address obtained from the base register. The result is used as the address for the memory access, and written back into the base register. The assembly language syntax for this mode is: [, ]!

    Post-indexed addressing :

    Post-indexed addressing
    The address obtained from the base register is used, unchanged, as the address for the memory access. The offset value is applied to the address, and written back into the base register The assembly language syntax for this mode is: [],

    3. 详细说明

    • Offset addressing:[Rn, offset]

            说明:最终访问内存的地址 = Rn+offset,这种操作后Rn的值不会改变

    • Pre-indexed addressing:[ Rn, offset] !

            说明:最终访问内存的地址 = Rn+offset,这种操作后Rn的值 = Rn+offset,注意加载的值来自地址:Rn + offset的值(更新后),类似于++i。       

    • Post-indexed addressing :[Rn],offset

            说明:最终访问内存的地址 = Rn,这种操作后Rn的值 = Rn+offset,跟pre-indexed的核心区别在于加载的值来自地址:Rn(更新前前),类似于i++

    4. 实验验证:

    通过实现一个c调用汇编的程序代码验证ldrb功能:

    c程序:arm.c

    1. #include
    2. #include
    3. #include
    4. extern void asm_strcpy(const char *src, char *dest);
    5. int main(){
    6. const char *s = "Hello World";
    7. char tmp[32];
    8. memset(tmp, 0, sizeof(tmp)/sizeof(char));
    9. asm_strcpy(s, tmp);
    10. printf("-----%s------\n", tmp);
    11. }
    12. ~

    汇编程序:ldrb.S

    1. .globl asm_strcpy
    2. asm_strcpy:
    3. loop:
    4. ldrb r4, [r0, #1]!
    5. cmp r4,#0
    6. beq over
    7. strb r4,[r1], #1
    8. b loop
    9. over:
    10. mov pc,lr
    11. .type asm_strcpy,%function

    编译:arm-linux-androideabi-gcc -o ldrb ldrb.S arm.c  -pie -fPIE 

    运行:

    D:\tmp>adb shell ldrb
    -----ello World------

    可以看到由于采用了[r0, #1] pre-indexed的memory access方式,r4寄存器加载的值来自于地址:r0 + 1。如果改成ldrb r4, [r0], #1,可正常输出Hello Wold,如下:

    D:\tmp>adb shell ldrb
    -----Hello World------

    ldr指令

    1.语法

    Load Register (immediate) calculates an address from a base register value and an immediate offset, loads a word
    from memory, and writes it to a register. It can use offset, post-indexed, or pre-indexed addressing. For information
    about memory accesses see Memory accesses on page A8-291.

    于ldrb的区别在于一次加载一个word长度的数据。arm32位中即32 bit的数据。

    举例:

    1. 1. ldr r0, =0x00000001 @加载立即数1到寄存器r0,= 意思对变量取地址
    2. 2.
    3. .globl get_int
    4. get_int:
    5. ldr r0, =abc @将0x10数值加载r0寄存器
    6. mov pc,lr
    7. .long abc
    8. .equ abc, 0x10

    ldrex指令

    Load Register Exclusive calculates an address from a base register value and an immediate offset, loads a word from
    memory, writes it to a register and:
    • if the address has the Shared Memory attribute, marks the physical address as exclusive access for the
    executing processor in a global monitor
    • causes the executing processor to indicate an active exclusive access in the local monitor.

    ldrex跟ldr核心区别实现原子操作,如内核的atomic_cmpxchg就是基于ldrex和strex指令实现,具体可以参考如下文章:

    宋宝华:关于ARM Linux原子操作的实现_linux阅码场的技术博客_51CTO博客

  • 相关阅读:
    记录本地Nginx发布vue项目
    HJ61 放苹果
    关于《Java并发编程之线程池十八问》的补充内容
    2023/9/7 -- C++/QT
    ES主集群的优化参考点
    Zookeeper:节点
    我们在文本摘要方面取得了什么成就?
    MySQL5.7主从同步
    安利一个好用的IDEA插件 object-helper-plugin
    Riesz表示定理
  • 原文地址:https://blog.csdn.net/GetNextWindow/article/details/126079001