从这个 wiki 页面(http://www.heyrick.co.uk/armwiki/The_Status_register)我们得到 CPSR(和 SPSR)的位布局:
31 30 29 28 27 - 24 - 19 … 16 - 9 8 7 6 5 4 … 0
N Z C V Q - J - GE[3:0] - E A I F T M[4:0]
声明一些标志(或只是计算这些):
int armflag_N = (Cpsr>>31)&1;
int armflag_Z = (Cpsr>>30)&1;
int armflag_C = (Cpsr>>29)&1;
int armflag_V = (Cpsr>>28)&1;
int armflag_Q = (Cpsr>>27)&1;
int armflag_J = (Cpsr>>24)&1;
int armflag_GE = (Cpsr>>16)&7;
int armflag_E = (Cpsr>>9)&1;
int armflag_A = (Cpsr>>8)&1;
int armflag_I = (Cpsr>>7)&1;
int armflag_F = (Cpsr>>6)&1;
int armflag_T = (Cpsr>>5)&1;
int armflag_M = (Cpsr>>0)&15;
(“ >>
”表示右移指定位数,“ &
”是按位与运算符,因此“ (val>>num)&mask
”表示右移 val num 位,然后提取掩码下的位)。
现在您有了带有标志的变量,这是有条件地打印标志的方法,
printf("Flags: ");
printf("%s ", armflag_N ? "N" : "-" );
...