一、iOS汇编
1、真机:arm64汇编
寄存器
指令
堆栈
2、模拟器:x86汇编
二、lldb
(lldb)register read x0 (lldb)register read w0 (lldb)register write x0 0x002a1b (lldb)register read w0 (lldb)register read
三、生成汇编文件
xcrun -sdk iphoneos clang -S -arch arm64 main.c -o main.s
四、寄存器
1、 通⽤寄存器
64bit的:x0 ~ x28
32bit的:w0 ~ w28(属于x0 ~ x28的低32bit)
x0 ~ x7通常拿来存放函数的参数,更多的参数使用堆栈来传递 x0通常拿来存放函数的返回值
2、 程序计数器
pc(Program Counter) 记录CPU当前指令的是哪一条指令 存储着当前CPU正在执⾏的指令的地址 类似于8086汇编的ip寄存器
3、 堆栈指针
sp(Stack Pointer)
fp(Frame Pointer),也就是x29
4、 链接寄存器
lr(Link Register),也就是x30
存储着函数的返回地址
5、 程序状态寄存器
cpsr(Current Program Status Register)
spsr(Saved Program Status Register),异常状态下使⽤


五、怎么编写汇编指令
1、在Xcode的OC文件中内嵌写入
__asm{ "mov x0, 100" }
2、用Xcode新建.s文件里面写汇编代码
arm.h
#ifndef arm_h #define arm_h void test(); int add(int a, int b); int sub(int a, int b); #endif /* arm_h */
arm.s
// 声明一个代码段 .text .global _test, _add, _sub // 内部\私有函数 mycode: mov x0, #0x1 mov x1, #0x2 add x2, x0, x1 ret // test函数的实现 _test: ; bl指令(函数调用) bl mycode mov x3, #0x2 mov x4, #0x1 ; b指令带条件 ; mov x0, #0x5 ; mov x1, #0x5 ; cmp x0, x1 ; bgt mycode ; mov x0, #0x5 ; ret ; mycode: ; mov x1, #0x6 ; b指令 ; b mycode ; mov x0, #0x5 ; mycode: ; mov x1, #0x6 ; cmp指令 ; mov x0, #0x1 ; mov x1, #0x3 ; cmp x0, x1 ; mov指令 ; mov x0, #0x8 ; mov x1, x0 ; add指令 ; mov x0, #0x1 ; mov x1, #0x2 ; add x2, x0, x1 ; sub指令 ; mov x0, #0x5 ; mov x1, #0x2 ; sub x2, x0, x1 ret // add函数的实现 _add: add x0, x0, x1 ret // sub函数的实现 _sub: sub x0, x0, x1 ret
使用的时候跟C语言的使用方式一致,导入头文件,直接调用全局(global)函数。