你好,游客 登录 注册 搜索
背景:
阅读新闻

ARM汇编语言(Assembly Language)

[日期:2016-12-08] 来源:Linux社区  作者:archiexie [字体: ]

ARM汇编语言(Assembly Language)是ARM CPU所能接受的最底层唯一语言(所有的高级语言最终都要转换成汇编语言然后汇编成processor instruction codes)。ARM汇编的核心是ARM指令集。理解ARM汇编有助于理解底层processor内部的工作原理,有助于对高级语言的优化。由于ARM汇编小、快的特点,经常被用在processor的初始化配置中(常见于bootloader、kernel的初始化代码)。


ARM Assembly Language(语法)

不同于其他高级语言,汇编语言没有一个标准的语法格式,不同的assembler有着不同的语法,不同的processor有着不同的指令(instruction code)格式。机器所能执行的是raw instruction code,汇编语言使用人类易懂的mnemonics来代替instruction code,然后通过assembler汇编成二进制的raw instruction code。以下主要针对ARM处理器指令格式及GNU Assembler进行讲解。

语句格式(Layout)

ARM汇编源文件是由每行一条语句顺序组成的文本文件。语句格式如下:

label: instruction @comment

每条语句由标签(label)、指令(instruction)、注释(comment)三项组成且每一项都是可选的:

  • Label

        内存地址的标记,指向一个特定地址,常被跳转指令(branch instructions)用来跳转。
  • Instruction

        ARM汇编指令(ARM assembly instruction)、所使用的汇编器指令(assembler directive)。
  • Comment

        注释以@符号开始,但在有些现代汇编器如GAS(GNU Assember)中,也可以使用C语言风格 /**/

指令格式(Instruction Format)

<op>{cond}{flags} Rd, Rn, Operand2

  • <op>

        使用easier-to-remember指令助记符(Opcode mnemonic)代替机器能理解但人类难理解的instruction code。
  • {cond}

        可选的两个字母的条件码(condition code),使指令依此条件执行。condition code 的判断依据是CPSR寄存器的N、Z、C、V标记位(见ARM体系结构),使用比较指令或者在指令后加S(如ADDS,MOVS)可更新这些FLAGS。
CODEMEANINGFLAGS
EQ EQual equals zero Z
NE Not Equal !Z
VS Overflow Set V
VC No overflow (oVerflow Clear) !V
MI MInus/negative N
PL PLus/positive or zero !N
CS Carry set/unsigned higher or same C
CC Carry clear/unsigned lower !C
HI Unsigned higher C and !Z
LS Unsigned lower or same !C or Z
GE Signed greater than or equal N == V
LT Signed less than N != V
GT Signed greater than !Z and (N == V)
LE Signed less than or equal Z or (N != V)
AL Always (default) Any
  • {flags}

        可选的附加标记。
  • Rd

        目的寄存器
  • Rn

        第一个寄存器
  • Operand2

        第二个寄存器或操作数

Addressing Modes(寻址方式)

常见寻址方式:

ModeDescriptionExample
立即数 hash加上整型 如#64,#0x1234
寄存器直接 寄存器中的数值作为操作数 ADD R0,R1,R2
寄存器间接 寄存器中的值作为地址,通过这个地址去取得操作数 LDR R0,[R1]
寄存器基址变址 间接寻址的扩展,地址组成改为寄存器基址+偏移量 形如[R1,#4]、[R1,R2]、[R1,#4]!、[R1],#4,后两种执行完R1值会自加4

GNU Assembler Directives(GNU汇编指令)

Assemblers reserve special keywords for instructing the assembler how to perform special functions as the mnemonics are converted to instruction codes.
All assembler directives have names that begin with a full-stop “.”.

.section

    这是最重要的指令,因为一个汇编程序一般都由databsstext三段组成,.section就是用来定义这每一段在内存中的区域。.data段用来存放已初始化的数据,.bss存放未初始的数据,.text存放instruction codes。这三段在内存中的大小是固定的,bss一般由用户程序初始化0,不占用flash空间。databss都是静态的全局变量,而函数内部动态的局部变量都放在堆栈中。

数据类型

DirectiveData Type
.ascii Text string
.asciz Null-terminated text string
.byte Byte value
.double Double-precision floating-point number
.float Single-precision floating-point number
.int 32-bit integer number
.long 32-bit integer number (same as .int)
.octa 16-byte integer number
.quad 8-byte integer number
.short 16-bit integer number
.single Single-precision floating-point number (same as .float)

其他

DirectiveDescription
.include 类似C语言#include
.equ 类似C语言中的宏定义,使用时用&
.extern 类似C语言的extern声明
.global 声明全局变量
.rodata 只读数据段
.comm Declares a common memory area for data that is not initialized
.lcomm 同.comm,只是局部的不能被global
.align Insert 0-3 bytes of 0x00’s so that the next location will be on a 4-byte(word) boundary
.type 定义函数
.end 文件结束

ARM Assembly Instructions (ARM汇编指令)

Assembly is just like any other computer language; you must first know the basics: the syntaxof the language. After you know how to speak assembly, then comes the interesting part — vocabulary.

Thumb指令集具有高密度的优势,其在硬件层最终也被映射到ARM指令集,所以效率也相当。

Thumb is designed as a target for C compilers, it is not designed to be used directly; rather,developers should use a higher language such as C.You must understand the principles behind the instruction set to write optimized code, but unlike the ARM ISA(Instruction Set Architecture), almost all optimization should bedone directly in C.

由于当前绝大多数的ARM核都同时支持ARM和Thumb两种指令集,ARM开发了Unified Assembler Language (UAL)同时支持这两个指令集。

MOVEMENT

OpcodeDescription
MOV (Move) copies data into a register
MVN (Move Negated) copies a negated value into a register
MOVW (Move Wide) copies a 16-bit constant into a register while zeroing the top 16 bits of the target register
MOVT (Move Top) copies a 16-bit constant into the top part of a register, leaving the bottom half untouched
NEG (Negate) takes the value in Rs and performs a multiplication by –1 before placing the result into Rd

ARITHMETIC

OpcodeDescription
ADD adds together two registers and places the result into a register
ADC (Add with carry) adds two numbers together and also uses the carry flag
SUB subtracts one number from another
SBC (Subtract with carry) is like the SUB instruction
RSB (Reverse subtract) is like SUB; RSB subtracts the value of two registers but reverses the order of the operation
RSC (Reverse subtract with carry) is like RSB

SATURATING ARITHMETIC

这个与上述算术指令的差异在于限定了操作数的取值范围,当出现溢出,CPSR的Q会置位,但这个Q位在后续的计算中不会被清除,也就是说如果是一系列运算,Q置位只能说明其中之一发生了溢出,但具体是哪个不清楚。

OpcodeDescription
QADD used in the same way as the ADD instruction, but does not update condition codes
QSUB executes a saturating subtraction
QDADD (Saturating Double Add) calculates SAT(Rm + SAT(Rn * 2)), Q according to Addition not Doubling
QDSUB (Saturating Double Subtraction) calculates Rm minus two times Rn. SAT(Rm – SAT(Rn * 2))

DATA TRANSFER

ARM使用的是Load/Store架构,数据必需从存储器搬到寄存器中才能使用。

OpcodeDescription
LDR (Load) is an instruction used for moving a single data element from system memory into a register
STR (Store) from register to system memory

LOGICAL

OpcodeDescription
AND 按位与
ORR 按位或
EOR (Exclusive-OR)按位异或
BIC is the equivalent of AND NOT; in C, it is equivalent to operand1 & (!operand2)
CLZ (Count Leading Zeros) is an instruction that takes the register Rm, counts the number of leading zeros, and places the result in Rm

COMPARE

Compare instructions are instructions that do not return any results, but set condition codes.

OpcodeDescription
CMP compares two values, updating the CPSR. It is the equivalent to operand1 - operand2
CMN is the equivalent to operand1 + operand2
TST is the equivalent to operand1 & operand2
TEQ compares operand1 and operand2 using a bitwise exclusive OR

BRANCH

OpcodeDescription
B (Branch)is a permanent branch; no return is possible
BL (Branch with Link) the address just after BL will be put into r14
BX (Branch and Exchange) is an instruction that enables the program to switch between ARM state and Thumb state
BLX (Branch with Link and Exchange) is like the BX instruction but also updates the Link register r14

MULTIPLY

OpcodeDescription
MUL Rd = Rm * Rs
MLA Multiply two numbers together with accumulate.Rd = (Rm * Rs) + Rn
UMULL (Unsigned Multiply Long) RdHi,RdLo = Rm * Rs
UMLAL (Unsigned Multiply with Accumulate Long) RdHi, RdLo = RdHi, RdLo + ( Rm * Rs )
SMULL (Signed Multiply Long)
SMLAL (Signed Multiply with Accumulate Long)

DIVIDE

OpcodeDescription
SDIV (Signed Divide) SDIV r0, r1, r2 ; r0 = r1/r2
UDIV Unsigned divide

MULTIPLE REGISTER DATA TRANSFER

OpcodeDescription
STM is the “store multiple” instruction
LDM is the “load multiple” instruction

BARREL SHIFTER

OpcodeDescription
LSL (Logical Shift Left) shifts the value left by the specified amount, padding with zeros
LSR (Logical Shift Right) is just like LSL
ASR (Arithmetic Shift Right) is just like LSR,the difference with LSR is that ASR keeps the signed bit
ROR (Rotate Right) rotates a number. Bits moved out of the right end of the register are rotated back into the left end
RRX (Rotate Right Extended) is just like ROR but without the Carry flag

STACK OPERATIONS

堆栈是内存最末端的一块区域,它的底也就是内存的最末端。堆栈主要存放函数调用需要传递数据。
PUSH and POP

COPROCESSOR INSTRUCTIONS

OpcodeDescription
MRC (Move to ARM Registers from Coprocessor)
MCR (Move to Coprocessor from ARM Registers)

MISCELLANEOUS INSTRUCTIONS

OpcodeDescription
SVC (Supervisor Call) causes an exception and switch to Supervisor mode
NOP is short for No Operation
MRS (Move to ARM Register from System coprocessor)
MSR (Move to System coprocessor register from ARM Register)

References

1. Professional-Embedded-ARM-Development
2. Professional Assembly Language

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-12/138049.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款