ARM指令集編碼格式解讀
說明:
1、本文參考的書籍《ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition》中的Chapter A5: ARM Instruction Set Encoding.
2、本人對本文最終效果中的表格縮進沒有對齊的現象表示歉意,因為目前本人解決不了 :)
3、本文的解讀流程如下:
1、Format of the CPSR and SPSRs:因為CPSR中有影響指令執行的條件標志;
2、The major subdivisions of the ARM instruction set:這里細分ARM指令為7個種類;
3、The condition code field:對指令中的條件位域進行了解讀;
4、Data-processing and miscellaneous instructions:針對the major subdivisions of the ARM instruction set中的7中ARM指令種類,對第一種“Data-processing and miscellaneous instructions”進行了解讀;
5、Data-processing (register):針對Data-processing and miscellaneous instructions中的14種指令,對第一種“Data-processing (register)”進行了解讀;
6、AND (register):針對Data-processing (register)中的22種指令,對第一種“AND (register)”進行了解讀;
4、如您在3中看到的,本文僅僅是對32位的ARM指令的一種簡單的理解性解讀;
********************************目錄******************************
一、為什么要解讀ARM指令編碼?
二、Format of the CPSR and SPSRs(CPSR and SPSRs格式):
三、The major subdivisions of the ARM instruction set(細分ARM指令集):
四、The condition code field(條件位域):
五、Data-processing and miscellaneous instructions(數據處理和雜項指令):
六、Data-processing (register)(數據處理(寄存器)):
七、AND (register):
****************************************************************
一、為什么要解讀ARM指令編碼?
以前每次當我看到或者聽說匯編指令的時候,我都會覺得很好奇:
1、匯編指令入是如何組成的?
2、mov r2, 0x33: 這條ARM匯編指令是如何保存指令中的mov,r2,0x33各部分?
3、bic r1, r1, r2:這條ARM匯編指令是如何保存指令中的bic,r1,r1,r2各部分?
4、一條ARM匯編指令那么長,怎么可能32位就能包含?而且還包含了判斷條件在里面;
雖然很多時候好奇,但是畢竟個人知識水平有限,並沒有能夠理解這里面的原理,也許今天解讀完這章英文文檔能夠從中得知玄機.
二、Format of the CPSR and SPSRs(CPSR and SPSRs格式):
1、以下是CPSR and SPSRs的位域格式圖:
2、由於本文僅僅需要用Condition flags,所以不對其他位域進行解讀:
Condition flags, bits[31:28]
Set on the result of instruction execution(設置的指令執行結果). The flags are:
1、N, bit[31] Negative condition flag (負數標志)
2、Z, bit[30] Zero condition flag (0標志)
3、C, bit[29] Carry condition flag (進位標志)
4、V, bit[28] Overflow condition flag (溢出標志)
The condition flags can be read or written in any mode.( 在任何模式下可以對條件標志讀取或寫入)
三、The major subdivisions of the ARM instruction set(細分ARM指令集):
1、ARM 指令流是一連串的字對齊的四字節指令流。每個 ARM 指令是一個單一的 32 位字(4字節)。ARM 指令細分編碼格式如下圖:
2、以下是對細分ARM指令集的位域分布,以及細分出的指令種類說明:
cond |
op1 |
op |
Instruction classes(指令分類) |
not 1111 |
00x |
- |
數據處理和雜項指令(本文只對這部分進行分析,其他部分類似) |
010 |
- |
加載/存儲字或無符號的字節 |
|
011 |
0 |
加載/存儲字或無符號的字節 |
|
1 |
媒體指令 |
||
10x |
- |
分支、帶鏈接分支、塊數據傳輸 |
|
11x |
- |
協處理器指令或軟中斷,包括浮點指令和先進SIMD數據傳輸 |
|
1111 |
- |
- |
如果cond字段為0b1111,只能無條件地執行指令 |
表格中的op1、op字段中的x、-表示可以是0,也可以是1 |
3、本文后續部分只對Instruction classes(指令分類)中的“數據處理和雜項指令”部分進行解讀,其他部分類似,沒必要全部解讀,畢竟不是為了翻譯。
四、The condition code field(條件位域):
1、每一條可條件執行的條件指令都有4位的條件位域(記住,只有四位) ,條件位域的值在0b0000-0b1110之間,如下是條件位域在32位ARM指令中的位置:
2、以下是對條件位域的值的列表,可條件執行指令執行受CPSR的condition flags中對應的位影響:
cond |
助記符 |
意義(整數) |
意義(浮點數) |
條件標志 |
0000 |
EQ |
Equal(相等) |
Equal(相等) |
Z == 1 |
0001 |
NE |
Not equal(不相等) |
Not equal, or unordered |
Z == 0 |
0010 |
CS |
Carry set(進位) |
Greater than, equal, or unordered |
C == 1 |
0011 |
CC |
Carry clear(借位) |
Less than(小於) |
C == 0 |
0100 |
MI |
Minus, negative(負數) |
Less than(小於) |
N == 1 |
0101 |
PL |
Plus, positive or zero |
Greater than, equal, or unordered |
N == 0 |
0110 |
VS |
Overflow(溢出) |
Unordered(無序) |
V == 1 |
0111 |
VC |
No overflow(沒有溢出) |
Not unordered(非無序) |
V == 0 |
1000 |
HI |
Unsigned higher (無符號大於) |
Greater than, or unordered |
C == 1 and Z == 0 |
1001 |
LS |
Unsigned lower or same |
Less than or equal |
C == 0 or Z == 1 |
1010 |
GE |
Signed greater than or equal |
Greater than or equal |
N == V |
1011 |
LT |
Signed less than |
Less than, or unordered |
N != V |
1100 |
GT |
Signed greater than |
Greater than(大於) |
Z == 0 and N == V |
1101 |
LE |
Signed less than or equal |
Less than, equal, or unordered |
Z == 1 or N != V |
1110 |
None (AL) |
Always (unconditional) |
Always (unconditional) |
Any |
1、Unordered means at least one NaN operand. 無序的意思是至少有一個非數字操作數 2、HS (unsigned higher or same) is a synonym for CS. HS(無符號大於或相同)是同義詞CS. 3、LO (unsigned lower) is a synonym for CC. LO(無符號更低)是同義詞CC. 4、AL is an optional mnemonic extension for always, except in IT instructions. AL是always的助記符,IT指令除外. |
3、匯編語言中溢出和進位的不同:
1、對於非符號數來說,不存在溢出的問題,它的進位就相當於符號數中的溢出;
2、而對於符號數來說,不存在進位的問題:
1、兩個正數相加(或一個正數減一個負數)得到負數;
2、兩個負數相加得到正數,就是溢出了;
3、一個正數和一個負數相加不可能溢出。
五、Data-processing and miscellaneous instructions(數據處理和雜項指令):
1、數據處理和雜項指令的位域分布圖如下:
2、這是對數據處理和雜項指令的位域說明表:
op |
op1 |
op2 |
Instruction or instruction class |
Variant |
0 |
not |
xxx0 |
Data-processing (register) |
- |
0xx1 |
Data-processing (register-shifted register) |
- |
||
10xx0 |
0xxx |
Miscellaneous instructions |
- |
|
1xx0 |
Halfword multiply and multiply accumulate |
- |
||
0xxxx |
1001 |
Multiply and multiply accumulate |
- |
|
1xxxx |
1001 |
Synchronization primitives |
- |
|
not |
1011 |
Extra load/store instructions |
- |
|
11x1 |
Extra load/store instructions |
- |
||
0xx1x |
1011 |
Extra load/store instructions, unprivileged |
- |
|
11x1 |
Extra load/store instructions |
- |
||
1 |
not |
- |
Data-processing (immediate) |
- |
10000 |
- |
16-bit immediate load, MOV (immediate) |
v6T2 |
|
10100 |
- |
High halfword 16-bit immediate load, MOVT |
v6T2 |
|
10x10 |
- |
MSR (immediate), and hints |
- |
3、本文后續部分只對Instruction or instruction class中的“Data-processing (register)(數據處理(寄存器))”部分進行解讀,其他部分類似,沒必要全部解讀,畢竟不是為了翻譯。
六、Data-processing (register)(數據處理(寄存器)):
1、數據處理(寄存器)位域分布如下圖所示:
2、這對數據處理(寄存器)位域的說明表,本人就不對內容進行翻譯了,如果有困難,可以使用bing,google翻譯,不建議使用其他的翻譯軟件,不解釋原因 :)
op |
op2 |
imm5 |
Instruction |
See |
0000x |
- |
- |
Bitwise AND (本文只對該指令進行解讀) |
AND (register) on page A8-326 |
0001x |
- |
- |
Bitwise Exclusive OR |
EOR (register) on page A8-384 |
0010x |
- |
- |
Subtract |
SUB (register) on page A8-712 |
0011x |
- |
- |
Reverse Subtract |
RSB (register) on page A8-576 |
0100x |
- |
- |
Add |
ADD (register, ARM) on page A8-312 |
0101x |
- |
- |
Add with Carry |
ADC (register) on page A8-302 |
0110x |
- |
- |
Subtract with Carry |
SBC (register) on page A8-594 |
0111x |
- |
- |
Reverse Subtract with Carry |
RSC (register) on page A8-582 |
10xx0 |
- |
- |
See Data-processing and miscellaneous instructions on page A5-196 |
|
10001 |
- |
- |
Test |
TST (register) on page A8-746 |
10011 |
- |
- |
Test Equivalence |
TEQ (register) on page A8-740 |
10101 |
- |
- |
Compare |
CMP (register) on page A8-372 |
10111 |
- |
- |
Compare Negative |
CMN (register) on page A8-366 |
1100x |
- |
- |
Bitwise OR |
ORR (register) on page A8-518 |
1101x |
00 |
00000 |
Move |
MOV (register, ARM) on page A8-488 |
not 00000 |
Logical Shift Left |
LSL (immediate) on page A8-468 |
||
01 |
- |
Logical Shift Right |
LSR (immediate) on page A8-472 |
|
10 |
- |
Arithmetic Shift Right |
ASR (immediate) on page A8-330 |
|
11 |
00000 |
Rotate Right with Extend |
RRX on page A8-572 |
|
not 00000 |
Rotate Right |
ROR (immediate) on page A8-568 |
||
1110x |
- |
- |
Bitwise Bit Clear |
BIC (register) on page A8-342 |
1111x |
- |
- |
Bitwise NOT |
MVN (register) on page A8-506 |
3、本文后續部分只對Instruction中的“Bitwise AND”部分進行解讀,其他部分類似,沒必要全部解讀,畢竟不是為了翻譯。
七、AND (register):
1、本文只對AND (register)中的Encoding A1 ARMv4*, ARMv5T*, ARMv6*, ARMv7指令進行解讀,以下是位域分布圖:
2、指令編碼位域意義:
1、cond: 是條件碼
2、S: 代表該指令是否會影響CPSR的狀態標志
3、Rn: 是保存第一個操作數的寄存器
4、Rm: 保存第二個操作數的寄存器
5、Rd: 保存運行結果的寄存器
6、imm5: 保存Rm中的數據要被移多少位
7、type: 移位的類型,向左移還是向右移,以及是否是循環移位等等
3、匯編語法:
1、AND{S}{<c>}{<q>} {<Rd>,} <Rn>, <Rm> {, <shift>}
2、解析:
1、S: 如果有S標志,指令運行結果會更新CPSR狀態標志。否則,該CPSR狀態標志不會更新;
2、<c>, <q>: See Standard assembler syntax fields on page A8-287.
3、<Rd>: 目的寄存器;
4、<Rn> : 第一個操作數寄存器;
5、<Rm>: 第二個操作數寄存器;
6、<shift>: 這個數字會對<Rm>寄存器中的值進行移位;
3、AND指令Demo:
1、ADD R0,R2,R3,LSL#1 ; R0 = R2 + (R3 << 1)
2、解析:
1、S:有S標志,說明運算結果會影響CPSR寄存器的狀態標志位;
2、<c>:沒有條件,說明沒有是無條件執行;
3、<Rd>:目的寄存器為R0;
4、<Rn>:第一個操作數寄存器為R2;
5、<Rm>:第二個操作數寄存器為R3;
6、<shift>:對R3進行邏輯左移1位;