程序生成步骤
转自:https://azeria-labs.com/writing-arm-assembly-part-1/- 写好汇编代码到
helloworld.s
- 编译文件(但是没有链接)
只编译不链接形成.o文件。里面包含了对各个函数的入口标记,描述,当程序要执行时还需要链接(link)
链接就是把多个.o文件链成一个可执行文件
as helloworld.s -o helloworld.o
- 链接文件,形成可执行文件
ld helloworld.o -o helloworld
GDB调试
程序代码
.data
var1: .word 3
var2: .word 4
.text
.global _start
_start:
ldr r0, adr_var1 @ load the memory address of var1 via label adr_var1 to R0
ldr r1, adr_var2 @ load the memory address of var2 via label adr_var2 to R1
ldr r2, [r0] @ load the value (0x03) at memory address found in R0 to R2
str r2, [r1, r2, LSL#2] @ address mode: offset. Store the value found in R2 (0x03) to the memory address found in R1 with the offset R2 left-shifted by 2. Base register (R1) unmodified.
str r2, [r1, r2, LSL#2]! @ address mode: pre-indexed. Store the value found in R2 (0x03) to the memory address found in R1 with the offset R2 left-shifted by 2. Base register modified: R1 = R1 + R2<<2
ldr r3, [r1], r2, LSL#2 @ address mode: post-indexed. Load value at memory address found in R1 to the register R3. Then modifiy base register: R1 = R1 + R2<<2
bkpt
adr_var1: .word var1
adr_var2: .word var2
- 进入调试
gdb helloworld
- 下断点。在_start函数下好断点
break _start
- 运行。会执行到断点位置
run 或者 r
- 向下执行3步
nexti 3
- 查看下面的10条指令
x:十六进制,i:指令
x/10i $pc
- 查看所有寄存器状态数据
info registers
或者: i r
- 查看指定内存地址存储的数据
x:十六进制
w:word,字类型
x/w 0x0001009F
网友评论