多寄存器的加载与存储
ARMv7-a架构的芯片提供了高效的寄存器数据加载与存储指令,一条指令可以从内存中加载多个寄存器,也可以将多个寄存器的数据存储至内存中。读写的内存必须是连续的;虽然数据量不大,但基于这些指令可以实现高效的数据拷贝、(软)中断的上下文保存、恢复,以及内核的任务切换、应用层的协程实现。Arm Architecture Reference Manual 对这些指令做了详尽的说明:
编写多寄存器的加载和存储示例
ARMv7芯片提供了以上多种指令,原因是这些指令在加载、存储数据时,具体功能实现有细微的差别。如加载或存储是递增的(increment ),还是递减的(decrement )?在加载或存储的内存操作在增减之前(before )还是之后(after )?这些为了探究以上指令的具体功能实现及差别,笔者编写了简单的应用,其中汇编代码为:
.macro load_store_entry, ls_func
.global \ls_func
.type \ls_func, %function
\ls_func:
push {r4, r5, r6, lr}
.endm
.macro load_store_exit, ls_func
pop {r4, r5, r6, pc}
.size \ls_func, . - \ls_func
.align 2
.endm
.arch armv7-a
.text
.arm
load_store_entry ls_stmda
stmda r0, {r1, r2, r3}
load_store_exit ls_stmda
load_store_entry ls_stmda1
stmda r0!, {r1, r2, r3}
load_store_exit ls_stmda1
load_store_entry ls_stmia
stmia r0, {r1, r2, r3}
load_store_exit ls_stmia
load_store_entry ls_stmia1
stmia r0!, {r1, r2, r3}
load_store_exit ls_stmia1
load_store_entry ls_stmdb
stmdb r0, {r1, r2, r3}
load_store_exit ls_stmdb
load_store_entry ls_stmdb1
stmdb r0!, {r1, r2, r3}
load_store_exit ls_stmdb1
load_store_entry ls_stmib
stmib r0, {r1, r2, r3}
load_store_exit ls_stmib
load_store_entry ls_stmib1
stmib r0!, {r1, r2, r3}
load_store_exit ls_stmib1
load_store_entry ls_ldmda
ldmda r0, {r1, r2, r3}
bl ls_dump_result
load_store_exit ls_ldmda
load_store_entry ls_ldmda1
ldmda r0!, {r1, r2, r3}
bl ls_dump_result
load_store_exit ls_ldmda1
load_store_entry ls_ldmia
ldmia r0, {r1, r2, r3}
bl ls_dump_result
load_store_exit ls_ldmia
load_store_entry ls_ldmia1
ldmia r0!, {r1, r2, r3}
bl ls_dump_result
load_store_exit ls_ldmia1
load_store_entry ls_ldmdb
ldmdb r0, {r1, r2, r3}
bl ls_dump_result
load_store_exit ls_ldmdb
load_store_entry ls_ldmdb1
ldmdb r0!, {r1, r2, r3}
bl ls_dump_result
load_store_exit ls_ldmdb1
load_store_entry ls_ldmib
ldmib r0, {r1, r2, r3}
bl ls_dump_result
load_store_exit ls_ldmib
load_store_entry ls_ldmib1
ldmib r0!, {r1, r2, r3}
bl ls_dump_result
load_store_exit ls_ldmib1
其中ls_dump_result 函数定义于C代码文件中,用于将r0, r1, r2, r3 四个寄存器的数据输出;为节省篇幅,相关的C代码省略。在设备上测试,最后的结果为:
=====================================
r0: 0x129a120; r1 - r3: 0x2019, 0x2020, 0x2021
After stmda r0, {r1, r2, r3}, r0: 0x129a120 ->
[0x129a110]: 00000000
[0x129a114]: 00000000
[0x129a118]: 00002019
[0x129a11c]: 00002020
[0x129a120]: 00002021
[0x129a124]: 00000000
[0x129a128]: 00000000
[0x129a12c]: 00000000
[0x129a130]: 00000000
=====================================
r0: 0x129a120; r1 - r3: 0x2019, 0x2020, 0x2021
After stmda r0!, {r1, r2, r3}, r0: 0x129a114 ->
[0x129a110]: 00000000
[0x129a114]: 00000000
[0x129a118]: 00002019
[0x129a11c]: 00002020
[0x129a120]: 00002021
[0x129a124]: 00000000
[0x129a128]: 00000000
[0x129a12c]: 00000000
[0x129a130]: 00000000
=====================================
r0: 0x129a120; r1 - r3: 0x2019, 0x2020, 0x2021
After stmia r0, {r1, r2, r3}, r0: 0x129a120 ->
[0x129a110]: 00000000
[0x129a114]: 00000000
[0x129a118]: 00000000
[0x129a11c]: 00000000
[0x129a120]: 00002019
[0x129a124]: 00002020
[0x129a128]: 00002021
[0x129a12c]: 00000000
[0x129a130]: 00000000
=====================================
r0: 0x129a120; r1 - r3: 0x2019, 0x2020, 0x2021
After stmia r0!, {r1, r2, r3}, r0: 0x129a12c ->
[0x129a110]: 00000000
[0x129a114]: 00000000
[0x129a118]: 00000000
[0x129a11c]: 00000000
[0x129a120]: 00002019
[0x129a124]: 00002020
[0x129a128]: 00002021
[0x129a12c]: 00000000
[0x129a130]: 00000000
=====================================
r0: 0x129a120; r1 - r3: 0x2019, 0x2020, 0x2021
After stmdb r0, {r1, r2, r3}, r0: 0x129a120 ->
[0x129a110]: 00000000
[0x129a114]: 00002019
[0x129a118]: 00002020
[0x129a11c]: 00002021
[0x129a120]: 00000000
[0x129a124]: 00000000
[0x129a128]: 00000000
[0x129a12c]: 00000000
[0x129a130]: 00000000
=====================================
r0: 0x129a120; r1 - r3: 0x2019, 0x2020, 0x2021
After stmdb r0!, {r1, r2, r3}, r0: 0x129a114 ->
[0x129a110]: 00000000
[0x129a114]: 00002019
[0x129a118]: 00002020
[0x129a11c]: 00002021
[0x129a120]: 00000000
[0x129a124]: 00000000
[0x129a128]: 00000000
[0x129a12c]: 00000000
[0x129a130]: 00000000
=====================================
r0: 0x129a120; r1 - r3: 0x2019, 0x2020, 0x2021
After stmib r0, {r1, r2, r3}, r0: 0x129a120 ->
[0x129a110]: 00000000
[0x129a114]: 00000000
[0x129a118]: 00000000
[0x129a11c]: 00000000
[0x129a120]: 00000000
[0x129a124]: 00002019
[0x129a128]: 00002020
[0x129a12c]: 00002021
[0x129a130]: 00000000
=====================================
r0: 0x129a120; r1 - r3: 0x2019, 0x2020, 0x2021
After stmib r0!, {r1, r2, r3}, r0: 0x129a12c ->
[0x129a110]: 00000000
[0x129a114]: 00000000
[0x129a118]: 00000000
[0x129a11c]: 00000000
[0x129a120]: 00000000
[0x129a124]: 00002019
[0x129a128]: 00002020
[0x129a12c]: 00002021
[0x129a130]: 00000000
=====================================
[0x129a100]: 01010101
[0x129a104]: 02020202
[0x129a108]: 03030303
[0x129a10c]: 04040404
[0x129a110]: 05050505
[0x129a114]: 06060606
[0x129a118]: 07070707
[0x129a11c]: 08080808
r0: 0x129a110; After ldmda r0, {r1, r2, r3} ->
r0: 0x129a110
r1: 03030303
r2: 04040404
r3: 05050505
=====================================
[0x129a100]: 01010101
[0x129a104]: 02020202
[0x129a108]: 03030303
[0x129a10c]: 04040404
[0x129a110]: 05050505
[0x129a114]: 06060606
[0x129a118]: 07070707
[0x129a11c]: 08080808
r0: 0x129a110; After ldmda r0!, {r1, r2, r3} ->
r0: 0x129a104
r1: 03030303
r2: 04040404
r3: 05050505
=====================================
[0x129a100]: 01010101
[0x129a104]: 02020202
[0x129a108]: 03030303
[0x129a10c]: 04040404
[0x129a110]: 05050505
[0x129a114]: 06060606
[0x129a118]: 07070707
[0x129a11c]: 08080808
r0: 0x129a110; After ldmia r0, {r1, r2, r3} ->
r0: 0x129a110
r1: 05050505
r2: 06060606
r3: 07070707
=====================================
[0x129a100]: 01010101
[0x129a104]: 02020202
[0x129a108]: 03030303
[0x129a10c]: 04040404
[0x129a110]: 05050505
[0x129a114]: 06060606
[0x129a118]: 07070707
[0x129a11c]: 08080808
r0: 0x129a110; After ldmia r0!, {r1, r2, r3} ->
r0: 0x129a11c
r1: 05050505
r2: 06060606
r3: 07070707
=====================================
[0x129a100]: 01010101
[0x129a104]: 02020202
[0x129a108]: 03030303
[0x129a10c]: 04040404
[0x129a110]: 05050505
[0x129a114]: 06060606
[0x129a118]: 07070707
[0x129a11c]: 08080808
r0: 0x129a110; After ldmdb r0, {r1, r2, r3} ->
r0: 0x129a110
r1: 02020202
r2: 03030303
r3: 04040404
=====================================
[0x129a100]: 01010101
[0x129a104]: 02020202
[0x129a108]: 03030303
[0x129a10c]: 04040404
[0x129a110]: 05050505
[0x129a114]: 06060606
[0x129a118]: 07070707
[0x129a11c]: 08080808
r0: 0x129a110; After ldmdb r0!, {r1, r2, r3} ->
r0: 0x129a104
r1: 02020202
r2: 03030303
r3: 04040404
=====================================
[0x129a100]: 01010101
[0x129a104]: 02020202
[0x129a108]: 03030303
[0x129a10c]: 04040404
[0x129a110]: 05050505
[0x129a114]: 06060606
[0x129a118]: 07070707
[0x129a11c]: 08080808
r0: 0x129a110; After ldmib r0, {r1, r2, r3} ->
r0: 0x129a110
r1: 06060606
r2: 07070707
r3: 08080808
=====================================
[0x129a100]: 01010101
[0x129a104]: 02020202
[0x129a108]: 03030303
[0x129a10c]: 04040404
[0x129a110]: 05050505
[0x129a114]: 06060606
[0x129a118]: 07070707
[0x129a11c]: 08080808
r0: 0x129a110; After ldmib r0!, {r1, r2, r3} ->
r0: 0x129a11c
r1: 06060606
r2: 07070707
r3: 08080808
函数入口和返回时的寄存器操作
笔者编写的汇编代码中,函数在入口和返回时有push 和pop 两条指令,分别用于寄存器的保存和恢复;相关的内存为栈空间:
push {r4, r5, r6, lr}
pop {r4, r5, r5, pc}
经调试,这两条指令实际上是:
stmdb sp!, {r4, r5, r6, lr}
ldmia sp!, {r4, r5, r6, pc}
|