You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Define: CMN <Xd>, <Xm>{, <shift> #<amount>} , note #amount range 0 to 63
Note: LSL when shift = 0, LSR when shift = 1, ASR when shift = 2
Example1: cmn x1, x2, asr #2
对于CMN,根据实验有以下几种情况:
Condition
Algo
N
Z
C
-0x01 > -0x0E
(-0x01) + (- 0x0E) = -0x0F
1
0
1
0 = 0
0 + 0 = 0
0
1
0
-0x0F < -0x01
(-0x0F) + (-0x01) = -0x10
1
0
1
0x0F > -0x01
(0x0F) + (-0x01) = 0x0E
0
0
1
-0x0F < 0x01
(-0x0F) + (0x01) = -0x0E
1
0
0
-0x01 < 0x01
(-0x01) + 0x01 = 0
0
1
1
2.3 Condition Codes
(CMP and CMN) Sets the condition flags to the result of a comparison if the original condition is true. If not true, the conditional flags are set to a specified condition flag state. The conditional compare instruction is very useful for expressing nested or compound comparisons.
Greater than, equal to, or unordered (identical to HS).
C = 1
HS
0b0010
Greater than, equal to (unsigned) (identical to CS).
Greater than, equal to, or unordered (identical to CS).
C = 1
CC
0b0011
Carry clear (identical to LO).
Less than (identical to LO).
C = 0
LO
0b0011
Unsigned less than (identical to CC).
Less than (identical to CC).
C = 0
MI
0b0100
Minus, Negative.
Less than.
N = 1
PL
0b0101
Positive or zero.
Greater than, equal to, or unordered.
N = 0
VS
0b0110
Signed overflow.
Unordered. (At least one argument was NaN).
V = 1
VC
0b0111
No signed overflow.
Not unordered. (No argument was NaN).
V = 0
HI
0b1000
Greater than (unsigned).
Greater than or unordered.
(C = 1) && (Z = 0)
LS
0b1001
Less than or equal to (unsigned).
Less than or equal to.
(C = 0) || (Z = 1)
GE
0b1010
Greater than or equal to (signed).
Greater than or equal to.
N==V
LT
0b1011
Less than (signed).
Less than or unordered.
N!=V
GT
0b1100
Greater than (signed).
Greater than.
(Z==0) && (N==V)
LE
0b1101
Less than or equal to (signed).
Less than, equal to or unordered.
(Z==1) || (N!=V)
AL
0b1110
Always executed.
Default. Always executed.
Any
NV
0b1111
Always executed.
Always executed.
Any
// The test is:// suppose the x1 = 1, x2 = -3// Use the `cmn` instruction to compare the x1 and x2// When the result is neg number, make x2 += 1;// until the result is zero, then return the function.test_cmn_jump: msr NZCV, xzrmov x0, xzrmov x1, #0x0loop:add x1, x1, #0x1mov x2, #-0x3 cmn x1, x2 // NZCV = 1000 mrs x0, NZCV b.mi loopret
05-ARMv8-指令集-跳转和比较指令
1. CLZ
计算最高为1的比特位前面有多少个0。例如, 0x0800 0000 0000 000F,前面是有4个0的(使用64位的寄存器),如果使用Wn寄存器,按照32位算。
CLZ <Xd>, <Xn>
clz x0, x1
[计算x1寄存器内的值最高位1的比特位前面有多少个0,并放入x0]2. 比较指令
比较指令有CMP和CMN,CMP本质为:
CMP x1, x2
->x1 = x1 - x2
;CMN为负向比较:CMN x1, x2
->x1 = x1 + x2
。2.1 CMP
NZC = a - b,根据不同的比较结果,来确定NZC的标志位,若x1 > x2, NCZV = 0100,若x1 = x2, NCZV = 0110,若x1 < x2, NCZV = 1000。
CMP <Xd|SP>, #<imm>{, lsl <#shift>}
, note shift supports #0 and 06_ARMv8_指令集_一些重要的指令 #12 only.cmp x1, #8
(x1 = x2 - 8)cmp x1, #8, lsl #12
( x1 = x2 - (8 << 12) )CMP <Xd>, <Xm>{, <shift> #<amount>}
, note #amount range 0 to 63cmp x1, x2, asr #2
2.1 CMN
NZC = a + b,a和b如果加和为负数,N置位; a + b的和为0,Z置位;a + b溢出(两个负数相加),C置位。
CMN <Xd|SP>, <R><m>, {<extend> {#<amount>}}
cmn x0, x1
( x0 = x0 - x1 )cmn x0, x1, lsl #5
( x0 = x0 - (x1 << 5) )CMN <Xd|SP>, #<imm>{, lsl <#shift>}
, note shift supports #0 and 06_ARMv8_指令集_一些重要的指令 #12 only.cmn x1, #8
(x1 = x2 - 8)cmn x1, #8, lsl #12
( x1 = x2 - (8 << 12) )CMN <Xd>, <Xm>{, <shift> #<amount>}
, note #amount range 0 to 63cmn x1, x2, asr #2
对于CMN,根据实验有以下几种情况:
2.3 Condition Codes
(
CMP
andCMN
) Sets the condition flags to the result of a comparison if the original condition is true. If not true, the conditional flags are set to a specified condition flag state. The conditional compare instruction is very useful for expressing nested or compound comparisons.Condition codes: 1
0b0000
0b0001
0b0010
0b0010
0b0011
0b0011
0b0100
0b0101
0b0110
0b0111
0b1000
0b1001
0b1010
0b1011
0b1100
0b1101
0b1110
0b1111
3. 条件选择指令
条件选择指令包括CSEL\CSET\CSINC三条指令,也是非常重要的指令。
3.1 CSEL
CSEL, 条件选择指令,如果cond为真,那么xd就是xn的值,否则就是xm的值。注意,为上面表格的值。
CSEL <Xd>, <Xn>, <Xm>, <cond>
csel x1, x1, x2, EQ
-> 如果Z=1,x1维持原始值,否则x2给x1赋值csel x1, x1, x2, MI
-> 如果N=1,x1维持原始值,否则x2给x1赋值3.2 CSET
CSET, 条件置位指令,如果cond为真,那么xd就是1,否则就是0。注意,为上面表格的值。
CSET <Xd>, <cond>
cset x1, EQ
-> 如果Z=1,x1是1, 否则为0cset x1, MI
-> 如果N=1,x1是1, 否则为03.3 CSINC
CSINC,条件选择并增加指令 如果cond为真,那么xd就是xn的值,否则就是xm+1的值。注意,为上面表格的值。
CSINC <Xd>, <Xn>, <Xm>, <cond>
csinc x1, x1, x2, EQ
-> 如果Z=1,x1维持x1, 否则为x2+1csinc x1, x2, x3, MI
-> 如果N=1,x1为x2, 否则为x3+13.4 Example
用汇编实现下面的c代码:
分析拆解:
4. 跳转与返回指令
b.<cond>
,xx为以上表格的指,不返回Example:
分析:
调用顺序应该是main -----> bl_test -----> csel_test ---> ret, 如果使用bl指令跳转到csel_test中,csel_test返回之后 bl_test拿到的是csel_test返回地址PC,而且X30寄存器被修改为csel_test子函数的地址,如果把PC+4返回给main函数,main函数看到的是当前的PC+4,肯定是错误的。这个难点就在于bl跳转子函数之后的返回需要处理好,重置X30寄存器的地址的值。
Ref
Footnotes
ARM Cortex-A Series Programmer's Guide for ARMv8-A -Conditional instructions ↩
The text was updated successfully, but these errors were encountered: