简体   繁体   中英

Loading ARM CPSR into C and formatting?

Whilst being given a document teaching ARM assembly the document now tells me to load the CPRS into C and format the data into a friendly format, such as -

Flags: N Z IRQ FIQ
State: ARM
Mode: Supervisor

Now I've loaded the CPRS into a variable within my program, but I'm struggling to understand what format the CPRS is in, I've seen things using hex to reset flags and etc along which bytes are control, field, status and extension masks.

I put my CPRS into an int just to see what the data shows and I'm given 1610612752 , I'm assuming I shouldn't be loading it into an int and something else in order for it to be much more clear.

Any hints pushing me to the right direction would be most appreciated.

From This wiki page, ( http://www.heyrick.co.uk/armwiki/The_Status_register ) we get the bit layout of the CPSR (and 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]

Declare some flags (or just compute these):

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;

(The " >> " means to rightshift specified number of bits, and " & " is the bitwise and operator, so " (val>>num)&mask " means rightshift val num bits, and then extract the bits under the mask).

Now you have variables with flags, Here is how you could conditionally print a flag,

printf("Flags: ");
printf("%s ", armflag_N ? "N" : "-" );
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM