歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> ARM匯編語言(Assembly Language)

ARM匯編語言(Assembly Language)

日期:2017/3/1 9:08:32   编辑:Linux編程

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

    這是最重要的指令,因為一個匯編程序一般都由data、bss、text三段組成,.section就是用來定義這每一段在內存中的區域。.data段用來存放已初始化的數據,.bss存放未初始的數據,.text存放instruction codes。這三段在內存中的大小是固定的,bss一般由用戶程序初始化0,不占用flash空間。data、bss都是靜態的全局變量,而函數內部動態的局部變量都放在堆棧中。

數據類型

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

Copyright © Linux教程網 All Rights Reserved