ARM条件码与CPSR标志位的关系
2007-07-14 15:26
323 查看
在ARM体系中,所有ARM指令均可条件执行,设置了一些条件执行码,但这些条件码对应设置CPSR中的Z, C, N, V标志位:
[align=left]l 0000 = EQ - Z set (equal)[/align]
[align=left]l 0001 = NE - Z clear (not equal)[/align]
[align=left]l 0010 = CS - C set (unsigned higher or same)[/align]
[align=left]l 0011 = CC - C clear (unsigned lower)[/align]
[align=left]l 0100 = MI - N set (negative)[/align]
[align=left]l 0101 = PL - N clear (positive or zero)[/align]
[align=left]l 0110 = VS - V set (overflow)[/align]
[align=left]l 0111 = VC - V clear (no overflow)[/align]
[align=left]l 1000 = HI - C set and Z clear (unsigned higher)[/align]
[align=left]l 1001 = LS - C clear or Z set (unsigned lower or same)[/align]
[align=left]l 1010 = GE - N set and V set, or N clear and V clear (greater or equal)[/align]
[align=left]l 1011 = LT - N set and V clear, or N clear and V set (less than)[/align]
[align=left]l 1100 = GT - Z clear, and either N set and V set, or N clear and V clear (greater than)[/align]
[align=left]l 1101 = LE - Z set, or N set and V clear, or N clear and V set (less than or equal)[/align]
[align=left]l 1110 = AL - always[/align]
[align=left]l 1111 = NV - never[/align]
[align=left]怎样去理解这些设置呢?拿1001 = LS - C clear or Z set (unsigned lower or same)来说:[/align]
是为什么LS就对应 C clear and Z set。看下面的例子:
MOV R0, #5
MOV R1, #6
CMP R0, R1
MOVLS R2, R0 ; if R0 < R1 则 将小值存入R2中
在这个例子中,MOVLS 能够正确执行的条件C=0 and z=1 成立,是通过CMP设置了,看看reference manual中的CMP设置规则:
[align=left]C flag:For a subtraction, including the comparison instruction CMP, C is set to 0 if the subtraction produced a borrow (that is, an unsigned underflow), and to 1 otherwise.[/align]
[align=left]Z flag: Is set to 1 if the result of the instruction is zero (which often indicates an equal result from[/align]
[align=left]a comparison), and to 0 otherwise.[/align]
[align=left] 这就不难理解了 LS 与标志位的对应了。下面给出标志位的设置规则,便于我们从中推导上面每一个例子。[/align]
[align=left]In either case, the new condition code flags (after the instruction has been executed) usually mean:[/align]
[align=left]N Is set to bit 31 of the result of the instruction. If this result is regarded as a two's complement[/align]
[align=left]signed integer, then N = 1 if the result is negative and N = 0 if it is positive or zero.[/align]
[align=left]Z Is set to 1 if the result of the instruction is zero (which often indicates an equal result from[/align]
[align=left]a comparison), and to 0 otherwise.[/align]
[align=left]C Is set in one of four ways:[/align]
[align=left]• For an addition, including the comparison instruction CMN, C is set to 1 if the addition[/align]
[align=left]produced a carry (that is, an unsigned overflow), and to 0 otherwise.[/align]
[align=left]• For a subtraction, including the comparison instruction CMP, C is set to 0 if the[/align]
[align=left]subtraction produced a borrow (that is, an unsigned underflow), and to 1 otherwise.[/align]
[align=left]• For non-addition/subtractions that incorporate a shift operation, C is set to the last bit[/align]
[align=left]shifted out of the value by the shifter.[/align]
[align=left]• For other non-addition/subtractions, C is normally left unchanged (but see the[/align]
[align=left]individual instruction descriptions for any special cases).[/align]
[align=left]V Is set in one of two ways:[/align]
[align=left]• For an addition or subtraction, V is set to 1 if signed overflow occurred, regarding the[/align]
[align=left]operands and result as two's complement signed integers.[/align]
[align=left]• For non-addition/subtractions, V is normally left unchanged (but see the individual[/align]
[align=left]instruction descriptions for any special cases).[/align]
来看下GT (signed greater)标志位的设置:
CMP -5, -4 ; 相减为不等于0,为负值,且有下溢出,所以N=1, V=1
CMP 6,5 ; 相减后值为1(N=0),正值且无下溢(V=0)
[align=left]l 0000 = EQ - Z set (equal)[/align]
[align=left]l 0001 = NE - Z clear (not equal)[/align]
[align=left]l 0010 = CS - C set (unsigned higher or same)[/align]
[align=left]l 0011 = CC - C clear (unsigned lower)[/align]
[align=left]l 0100 = MI - N set (negative)[/align]
[align=left]l 0101 = PL - N clear (positive or zero)[/align]
[align=left]l 0110 = VS - V set (overflow)[/align]
[align=left]l 0111 = VC - V clear (no overflow)[/align]
[align=left]l 1000 = HI - C set and Z clear (unsigned higher)[/align]
[align=left]l 1001 = LS - C clear or Z set (unsigned lower or same)[/align]
[align=left]l 1010 = GE - N set and V set, or N clear and V clear (greater or equal)[/align]
[align=left]l 1011 = LT - N set and V clear, or N clear and V set (less than)[/align]
[align=left]l 1100 = GT - Z clear, and either N set and V set, or N clear and V clear (greater than)[/align]
[align=left]l 1101 = LE - Z set, or N set and V clear, or N clear and V set (less than or equal)[/align]
[align=left]l 1110 = AL - always[/align]
[align=left]l 1111 = NV - never[/align]
[align=left]怎样去理解这些设置呢?拿1001 = LS - C clear or Z set (unsigned lower or same)来说:[/align]
是为什么LS就对应 C clear and Z set。看下面的例子:
MOV R0, #5
MOV R1, #6
CMP R0, R1
MOVLS R2, R0 ; if R0 < R1 则 将小值存入R2中
在这个例子中,MOVLS 能够正确执行的条件C=0 and z=1 成立,是通过CMP设置了,看看reference manual中的CMP设置规则:
[align=left]C flag:For a subtraction, including the comparison instruction CMP, C is set to 0 if the subtraction produced a borrow (that is, an unsigned underflow), and to 1 otherwise.[/align]
[align=left]Z flag: Is set to 1 if the result of the instruction is zero (which often indicates an equal result from[/align]
[align=left]a comparison), and to 0 otherwise.[/align]
[align=left] 这就不难理解了 LS 与标志位的对应了。下面给出标志位的设置规则,便于我们从中推导上面每一个例子。[/align]
[align=left]In either case, the new condition code flags (after the instruction has been executed) usually mean:[/align]
[align=left]N Is set to bit 31 of the result of the instruction. If this result is regarded as a two's complement[/align]
[align=left]signed integer, then N = 1 if the result is negative and N = 0 if it is positive or zero.[/align]
[align=left]Z Is set to 1 if the result of the instruction is zero (which often indicates an equal result from[/align]
[align=left]a comparison), and to 0 otherwise.[/align]
[align=left]C Is set in one of four ways:[/align]
[align=left]• For an addition, including the comparison instruction CMN, C is set to 1 if the addition[/align]
[align=left]produced a carry (that is, an unsigned overflow), and to 0 otherwise.[/align]
[align=left]• For a subtraction, including the comparison instruction CMP, C is set to 0 if the[/align]
[align=left]subtraction produced a borrow (that is, an unsigned underflow), and to 1 otherwise.[/align]
[align=left]• For non-addition/subtractions that incorporate a shift operation, C is set to the last bit[/align]
[align=left]shifted out of the value by the shifter.[/align]
[align=left]• For other non-addition/subtractions, C is normally left unchanged (but see the[/align]
[align=left]individual instruction descriptions for any special cases).[/align]
[align=left]V Is set in one of two ways:[/align]
[align=left]• For an addition or subtraction, V is set to 1 if signed overflow occurred, regarding the[/align]
[align=left]operands and result as two's complement signed integers.[/align]
[align=left]• For non-addition/subtractions, V is normally left unchanged (but see the individual[/align]
[align=left]instruction descriptions for any special cases).[/align]
来看下GT (signed greater)标志位的设置:
CMP -5, -4 ; 相减为不等于0,为负值,且有下溢出,所以N=1, V=1
CMP 6,5 ; 相减后值为1(N=0),正值且无下溢(V=0)
相关文章推荐
- ARM条件码与CPSR标志位的关系
- ARM条件码与CPSR标志位的关系
- ARM条件码与CPSR标志位的关系
- ARM条件码与CPSR标志位的关系——cmp MOVLS与MOVHI 指令
- ARM处理器CPSR标志位和条件符之间的关系
- ARM处理器CPSR标志位和条件符之间的关系
- ARM处理器CPSR标志位和条件符之间的关系
- 指令+s对cpsr中标志位的影响
- ARM处理器CPSR标志位和条件符之间的关系
- ARM处理器CPSR标志位和条件符之间的关系
- ARM处理器CPSR标志位和条件符之间的关系
- 操作影响cpsr的条件码的指令(teq等),与影响关系
- 关系数据库范式
- 人际关系的55个绝招
- SYSFS以及设备文件或kobject目录创建的关系
- Web开发技术之间的关系
- storm源码之理解Storm中Worker、Executor、Task关系
- Android softkeyboard 和 其他界面关系 softInputMode
- MTK平台学习--History管理机制与EntryNewScreen函数的关系
- 通信时间和通信距离的关系