I am going through different mode of ARM processor. I want to check the processor state(ex: register values) while it is in different mode.
So can someone help me to
You'll need inline assembly for this since C has no built in way to achieve what you want. This isn't exactly the most efficient way to do this (my GCC still generates a useless mov r3, #0
instruction).
You're probably better off implementing the function in bare assembly.
#define MODE_USR 0x10
#define MODE_FIQ 0x11
#define MODE_IRQ 0x12
#define MODE_SVC 0x13
#define MODE_ABT 0x17
#define MODE_UND 0x1B
#define MODE_SYS 0x1F
#define MODE_MASK 0x1F
void switch_mode(int mode) {
register unsigned long tmp = 0;
mode &= ~MODE_MASK;
asm volatile(
"mrs %[tmp], cpsr_all \n"
"bic %[tmp], %[tmp], %[mask] \n"
"orr %[tmp], %[tmp], %[mode] \n"
"msr cpsr_all, %[tmp] \n"
: : [mode] "r" (mode), [mask] "I" (MODE_MASK), [tmp] "r" (tmp)
);
}
The function just sets the mode bits in the program status register. You can find the constants in the ARM reference manual for your exact architecture.
Here is the code generated by my compiler:
00000000 :
0: e3c0001f bic r0, r0, #31 ; sane-ify the input
4: e3a03000 mov r3, #0 ; useless instruction generated by gcc
8: e10f3000 mrs r3, CPSR ; saves the current psr
c: e3c3301f bic r3, r3, #31 ; clear mode bits
10: e1833000 orr r3, r3, r0 ; set mode bits to the inputted bits
14: e129f003 msr CPSR_fc, r3 ; set current psr to the modified one
18: e12fff1e bx lr