ARM汇编基础

7 篇文章 4 订阅

 原文在次:http://www.cnblogs.com/mddblog/p/4951650.html

一、Thumb指令与ARM指令
  Thumb指令为16位,因此存储代码的密度高,节省存储空间。但是功能不全,它只是ARM指令(32位)集的补充,是ARM指令集下的一个子集。在初级阶段我们不需要了解这些知识,只要有个概念知道有这么个东西就可以。

二、ARM的寄存器初步了解

  R0-R3:        用于函数参数及返回值的传递,超过4个参数,其它参数存在栈中,在ARM中栈是向下生长的,R0还可以作为返回值。
  R4-R6, R8, R10-R11: 没有特殊规定,就是普通的通用寄存器
  R7:          栈帧指针,指向母函数与被调用子函数在栈中的交界。
  R9:          在iOS3.0被操作系统保留
  R12:          内部过程调用寄存器,动态链接时会用到,不必深究
  R13:          SP(stack pointer),是栈顶指针
  R14:          LR(link register),存放函数的返回地址。
  R15:          PC(program counter),指向当前指令地址。
  CPSR:         当前程序状态寄存器,在用户状态下存放像condition标志中断禁用等标志的。
  另外还有VFP(向量浮点运算)相关的寄存器,不在列举。

三、常用汇编

  助记符  说明 
  ADC    带进位的加法
  ADD    加法
  AND    逻辑与
  B      分支跳转,很少单独使用
  BL          分支跳转,跳转后返回地址存入r14
  BX          分支跳转,并切换指令模式(Thumb/ARM)
  CMP        比较值,结果存在程序状态寄存器,一般用于分支判断
  BEQ        结果为0则跳转
  BNE        结果不为0跳转
  LDR        加寄存器,从内存加载到寄存器
  LDRB      装载字节到寄存器
  LDRH      装载半字到寄存器(一个字是32位)
  LSL         逻辑左移 这是一个选项,不是指令
  LSR         逻辑右移 这是一个选项,不是指令
  MOV        传送值/寄存器到一个寄存器 
  STR         存储一个寄存器,寄存器值存到内存 
  STRB       存储一个字节
  STRH       存储一个半字
  SUB         减法
  PUSH POP 堆栈操作

  更多指令请参考这里

四、函数调用

  函数的参数、局部变量、返回地址都在栈上存着,这部分栈上的内存称为栈帧。和R0~R15(不一定全部)、CPSR等一起构成了函数的运行环境。每一个函数系统都会分配一个栈帧,执行完成后系统自动收回。每个函数都以为R0~R15、CPSR等CPU相关寄存器为自己一人享有,所以要做一些必要操作。

  举个例子:假设A调用B时,那么A要保存自己的运行环境(保存现场),B执行完后,要恢复A的运行环境(恢复现场);另外A还可以通过R0—R4来传递参数,参数超过4个可以通过栈,B返回时可以通过R0传递返回值。其中主要涉及的就是栈的操作和寄存器的操作。下图为函数调用前后栈的布局,左边为调用前,右边为调用后,当B返回时应回到左边状态(A调用B之前的状态,就像没有调用B一样)。

  在上图中,一个栈帧除了已经提到的参数区域(parameter area)、链接区域(linkage area)、局部变量存储区(local storage area)外还有栈帧指针存放区域(saved frame pointer)、寄存器存储区(saved registers area),栈帧寄存器就不再解释,寄存器存储区:

  开始调用(现场保护):

  1)LR入栈;

  2)R7入栈,包存要恢复的寄存器入栈;

  3)R7 = SP地址;

  4)将callee会修改且在返回caller时需要恢复的寄存器入栈;

  5)分配栈空间给子程序使用。

  下图为汇编代码(使用hopper disassemble反编译的代码),分析如下:

  第一行:将LR, R7, R4-R6入栈;

  第二行:r7=sp-0xc(指向old R7),之所以减去0xc因为PUSH后,r4,r5,r6占去12个字节;

  第三行:保存要恢复的寄存器;

  第四行:给当前函数开辟栈空间。

  函数返回(恢复现场):

  1)释放栈空间;

  2)恢复所保存的寄存器;

  3)恢复R7;

  4)将之前存放的LR从栈上弹出到PC,这样函数就返回了。

  下图为汇编代码(使用hopper disassemble反编译的代码),分析如下:

  第一行:释放栈空间;

  第二行:恢复保存的寄存器;

  第三行:恢复保存的寄存器,恢复R7,将之前存放的LR从栈上弹出到PC。

  更详细函数调用请参考这里

  iOS内存分区请参考这里

 

学习ARM汇编指令的经典资料。 对ARM的各种汇编指令进行了深入全面的讲解,并给出了各种示例和详细注解,并且还对一些值得注意的点或是容易犯错误的地方进行了总结。 目录 前言................................................................................................................................................................i 目录................................................................................................................................................................I ARM7TDMI(-S)指令集及汇编....................................................................................................................1 ARM处理器寻址方式..........................................................................................................................2 寄存器寻址....................................................................................................................................2 立即寻址........................................................................................................................................2 寄存器偏移寻址............................................................................................................................2 寄存器间接寻址............................................................................................................................3 基址寻址........................................................................................................................................3 多寄存器寻址................................................................................................................................4 堆栈寻址........................................................................................................................................4 块拷贝寻址....................................................................................................................................5 相对寻址........................................................................................................................................5 指令集介绍............................................................................................................................................7 ARM指令集..................................................................................................................................7 指令格式................................................................................................................................7 第2个操作数................................................................................................................7 #immed_8r...........................................................................................................7 Rm..........................................................................................................................8 Rm,shift..................................................................................................................8 条件码............................................................................................................................9 ARM存储器访问指令........................................................................................................11   LDR和STR........................................................................................................11   LDM和STM.......................................................................................................14   SWP.....................................................................................................................17 ARM数据处理指令............................................................................................................19 数据传送指令..............................................................................................................20   MOV............................................................................................................20   MVN............................................................................................................20 算术逻辑运算指令......................................................................................................20   ADD.............................................................................................................20   SUB..............................................................................................................21   RSB..............................................................................................................21   ADC.............................................................................................................21   SBC..............................................................................................................21   RSC..............................................................................................................22   AND.............................................................................................................22   ORR..............................................................................................................22   EOR..............................................................................................................22   BIC...............................................................................................................23 比较指令......................................................................................................................23   CMP.............................................................................................................23   CMN.............................................................................................................23   TST...............................................................................................................24   TEQ..............................................................................................................24 乘法指令......................................................................................................................25   MUL.............................................................................................................25   MLA.............................................................................................................25   UMULL........................................................................................................25   UMLAL........................................................................................................26   SMULL........................................................................................................26   SMLAL........................................................................................................26 ARM跳转指令....................................................................................................................27   B...........................................................................................................................27   BL.........................................................................................................................27   BX........................................................................................................................27 ARM协处理器指令............................................................................................................28   CDP......................................................................................................................28   LDC......................................................................................................................29   STC......................................................................................................................29   MCR.....................................................................................................................30   MRC.....................................................................................................................30 ARM杂项指令....................................................................................................................31   SWI......................................................................................................................31   MRS.....................................................................................................................32   MSR.....................................................................................................................33 ARM伪指令........................................................................................................................34   ADR.....................................................................................................................35   ADRL...................................................................................................................35   LDR......................................................................................................................36   NOP......................................................................................................................37 Thumb指令集.............................................................................................................................39 Thumb指令集与ARM指令集的区别..............................................................................39 Thumb存储器访问指令.....................................................................................................40   LDR和STR........................................................................................................41   PUSH和POP......................................................................................................43   LDMIA和STMIA..............................................................................................43 Thumb数据处理指令.........................................................................................................45 数据传送指令..............................................................................................................46   MOV............................................................................................................46   MVN............................................................................................................46   NEG..............................................................................................................47 算术逻辑运算指令......................................................................................................47   ADD.............................................................................................................47   SUB..............................................................................................................48   ADC.............................................................................................................49   SBC..............................................................................................................49   MUL.............................................................................................................50   AND.............................................................................................................50   ORR..............................................................................................................50   EOR..............................................................................................................51   BIC...............................................................................................................51   ASR..............................................................................................................51   LSL...............................................................................................................52   LSR..............................................................................................................52   ROR..............................................................................................................53 比较指令......................................................................................................................53   CMP.............................................................................................................53   CMN.............................................................................................................54   TST...............................................................................................................54 Thumb跳转指令.........................................................................................................55   B...................................................................................................................55   BL.................................................................................................................55   BX................................................................................................................55 Thumb杂项指令.........................................................................................................56   SWI..............................................................................................................56 Thumb伪指令.............................................................................................................57   ADR.............................................................................................................57   LDR..............................................................................................................57   NOP..............................................................................................................58 伪指令..................................................................................................................................................59 符号定义伪指令..........................................................................................................................59   GBLA、GBLL、GBLS..............................................................................................59   LCLA、LCLL、LCLS................................................................................................60   SETA、SETL、SETS.................................................................................................61   RLIST...........................................................................................................................61   CN................................................................................................................................62   CP.................................................................................................................................62   DN、SN.......................................................................................................................62   FN.................................................................................................................................63 数据定义伪指令..........................................................................................................................63   LTORG.........................................................................................................................64   MAP.............................................................................................................................64   FIELD..........................................................................................................................65   SPACE..........................................................................................................................66   DCB.............................................................................................................................66   DCD和DCDU............................................................................................................67   DCDO..........................................................................................................................67   DCFD和DCFDU........................................................................................................68   DCFS和DCFSU.........................................................................................................68   DCI...............................................................................................................................69   DCQ和DCQU............................................................................................................69   DCW和DCWU..........................................................................................................70 报告伪指令..................................................................................................................................70   ASSERT.......................................................................................................................70   INFO............................................................................................................................71   OPT..............................................................................................................................71   TTL和SUBT..............................................................................................................72 汇编控制伪指令..........................................................................................................................73   IF、ELSE和ENDIF...................................................................................................73   MACRO和MEND.....................................................................................................74   WHIL和WEND.........................................................................................................75 杂项伪指令..................................................................................................................................76   ALIGN.........................................................................................................................77   AREA...........................................................................................................................78   CODE16和CODE32..................................................................................................79   END.............................................................................................................................80   ENTRY.........................................................................................................................80   EQU.............................................................................................................................81   EXPORT和GLOBAL................................................................................................81   IMPORT和EXTERN.................................................................................................82   GET和INCLUDE......................................................................................................83   INCBIN........................................................................................................................83   KEEP............................................................................................................................83   NOFP...........................................................................................................................84   REQUIRE....................................................................................................................84   PEQUIRE8和PRESERVE8.......................................................................................84   RN................................................................................................................................84   ROUT...........................................................................................................................85 ARM伪指令................................................................................................................................86   ADR.............................................................................................................................86   ADRL...........................................................................................................................86   LDR..............................................................................................................................86   NOP..............................................................................................................................86   LDFD...........................................................................................................................86   LDFS............................................................................................................................87 Thumb伪指令.............................................................................................................................87   ADR.............................................................................................................................87   LDR..............................................................................................................................87   NOP..............................................................................................................................88 ARM汇编程序设计............................................................................................................................88 文件格式......................................................................................................................................88 ARM汇编的一些规范................................................................................................................88 汇编语句格式......................................................................................................................88 标号......................................................................................................................................89   基于PC的标号...................................................................................................89   基于寄存器的标号..............................................................................................90   绝对地址..............................................................................................................90 局部标号..............................................................................................................................90 符号......................................................................................................................................91 常量......................................................................................................................................91   数字常数..............................................................................................................91   字符常量..............................................................................................................92   布尔常量..............................................................................................................92 段定义..................................................................................................................................92 宏定义及其作用..................................................................................................................93 子程序的调用..............................................................................................................................94 数据比较跳转..............................................................................................................................95 循环..............................................................................................................................................95 数据块复制..................................................................................................................................95 栈操作..........................................................................................................................................96 特殊寄存器定义及应用..............................................................................................................96 散转功能......................................................................................................................................97 查表操作......................................................................................................................................97 长跳转..........................................................................................................................................97 对信号量的支持..........................................................................................................................98 伪指令使用..................................................................................................................................98 一个完整的例子..........................................................................................................................98 外围部件控制..............................................................................................................................99 三级流水线介绍..........................................................................................................................99 C与汇编混合编程............................................................................................................................100 内嵌汇编....................................................................................................................................100 内嵌汇编的指令用法........................................................................................................103 内嵌汇编器与armasm汇编器的差异.............................................................................104 内嵌汇编注意事项............................................................................................................104 访问全局变量............................................................................................................................106 C与汇编相互调用....................................................................................................................107 寄存器的使用规则............................................................................................................108 堆栈使用规则....................................................................................................................108 参数传递规则....................................................................................................................109 C程序调用汇编程序........................................................................................................110 汇编程序调用C程序.......................................................................................................111 ARM指令集列表..............................................................................................................................113 ARM存储器访问指令表列表..................................................................................................113 ARM数据处理指令列表..........................................................................................................114 ARM乘法指令列表..................................................................................................................115 ARM跳转指令列表..................................................................................................................116 ARM协处理器指令列表..........................................................................................................117 ARM杂项指令列表..................................................................................................................118 ARM伪指令列表......................................................................................................................119 Thumb指令集列表...........................................................................................................................120 Thumb存储器访问指令列表...................................................................................................120 Thumb数据处理指令列表.......................................................................................................121 Thumb跳转指令及软中断指令列表.......................................................................................122 Thumb伪指令列表...................................................................................................................123 汇编预定义变量及伪指令................................................................................................................124 预定义的寄存器和协处理器名................................................................................................124 通用寄存器........................................................................................................................124 程序状态寄存器................................................................................................................124 浮点数寄存器....................................................................................................................124 协处理器及协处理器寄存器............................................................................................125 内置变量列表............................................................................................................................125 伪指令列表................................................................................................................................126 指令条件码列表........................................................................................................................128 CPSR和SPSR分配图.............................................................................................................................129
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值