ARM汇编3:
基础知识
ARM架构通过 Load store的方式去获取并修改内存地址对应的数据,也就是,只有load 和 store可以用于访问内存。而X86中,通常有大量指令可以进行内存的读写修改,要更加复杂。
实例介绍
在下列代码中,变量var1,var2存储于内存中,我们不能通过mov去获取或者改变内存中的数值,下面介绍具体的方法。
首先介绍如下语法知识。
-
.data 的指令用于表示数据部分的开始,其中存储了已经初始化的数据。
-
_start 表示代码entry point的位置。
_start:ldr r0, =var1 // Load the address of var1 into r0ldr r1, [r0] // Load the value at the address in r0 into r1mov r2, #43 // Move the immediate value 43 into r2ldr r3, =var2 // Load the address of var2 into r3str r2, [r3] // Store the value in r2 into the address pointed to by r3.data
var1: .word 5 // Define a word at var1 with the initial value 5
var2: .word 6 // Define a word at var2 with the initial value 6
指令介绍
LDR可用于将内存中的数据(word)加载到寄存器。如上图指令,内存中的数据的拷贝,不能通过mov进行,需要通过LDR!具体介绍参考链接1
STR用于将寄存器中的数值存入内存中,如上图,针对r3, 通过[*] 的操作,实现类似dereference的操作。
分步调试
我们通过https://cpulator.01xz.net/?sys=arm 分步执行指令,也就是不断输入step over。
在运行一条指令后,可以看到r0变为10。为什么10?10的原因是 var1所对应的内存地址是10,如下图,内存地址为10,对应的是var1的地址。接下来,我们获取了var1的地址,但是var对应的数值呢? [r0] 类似于dereference的操作,可以让r1获取t0的地址对应的数值。
Arm uses load-store architecture, only load and store instructions can access memory. In x86, a lot of instructions can read and modify memory, which is more complex.
下图显示了在执行str前后,寄存器和内存的状态,可以看到var2对应的内存发生了改变。从6变为了43(2b)
参考链接
[1] https://developer.arm.com/documentation/ddi0406/b/Application-Level-Architecture/Instruction-Details/Alphabetical-list-of-instructions/LDR--register-?lang=en
[2] https://developer.arm.com/documentation/ddi0406/b/Application-Level-Architecture/Instruction-Details/Alphabetical-list-of-instructions/STR--register-?lang=en