SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: What is the xPSR register in ARM Cortex-M?
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

What is the xPSR register in ARM Cortex-M?

Jamie Kellett
Last updated: September 14, 2023 6:56 am
Jamie Kellett 6 Min Read
Share
SHARE

The xPSR (program status register) is one of the key registers in the ARM Cortex-M processor that contains status information about the processor state. It provides details on the current state of the application program being executed as well as the result of the previously executed instruction. The ‘x’ in xPSR stands for ‘extended’ as it is an extended version of the PSR used in older ARM processors.

Contents
Overview of xPSRBits in the xPSR registerKey Uses of xPSRDifference between xPSR and IPSRTypical Operations using xPSRTips for Using xPSRConclusion

Overview of xPSR

The xPSR register is 32-bits wide and contains the current state information of various processor controls like the instruction set state, interrupt disables, saturation flag as well as the condition flags. It is updated automatically by the processor after the execution of each instruction to reflect the new program state. The xPSR register can be read using the MRS instruction but cannot be directly written to using the MSR instruction. Its value needs to be modified using specific instructions like CPS, SETEND, CPSIE, etc.

Bits in the xPSR register

The xPSR register contains the following bit fields:

  • N, Z, C, V bits (bits 31-28): These are the negative, zero, carry and overflow flags used for conditional execution. They are set or cleared automatically based on the result of arithmetic or logical instructions.
  • Q bit (bit 27): The saturation flag. It is set if saturation occurs during execution of a Q-flagged instruction like QADD.
  • ICI bits (bits 26-25): Reserved bits in Cortex-M processors. Used in other ARM architectures.
  • T bit (bit 24): Thumb state bit. Indicates if the processor is executing in Arm or Thumb mode.
  • IT bits (bits 15-10): IT block state bits for Thumb conditional execution.
  • M, F bits (bits 4-0): Mode and FIQ mask bits that determine the current processor mode.

Key Uses of xPSR

Here are some of the main uses of the xPSR register in Cortex-M processors:

  • Determining processor mode – The M and F bits in xPSR indicate if the processor is in Thread, Handler, or Privileged mode.
  • Checking condition flags – The N, Z, C, V bits reflect the status flags which can be tested using conditional instructions.
  • Identifying Thumb state – The T bit indicates if the processor is executing Thumb or ARM instructions.
  • Detecting interrupts – Changes in xPSR mode bits can identify when exceptions and interrupts occur.
  • Saving context – xPSR is part of the context saved during exception handling to preserve the state.
  • Monitoring saturation – The Q bit shows if any saturation happened for Q-flagged arithmetic instructions.

Difference between xPSR and IPSR

The IPSR (Interrupt Program Status Register) is similar to the xPSR but has some differences:

  • IPSR contains only the interrupt number bits and does not have the other xPSR fields like flags, or mode bits.
  • IPSR is accessed using the MRS and MSR instructions but xPSR can only be read using MRS.
  • IPSR is updated with the interrupt number during exception handling while xPSR contains the program status.
  • xPSR is used to save context during exception return while IPSR is used to identify the pending interrupt.

Typical Operations using xPSR

Here are some typical operations involving the xPSR register on Cortex-M:

  • Reading xPSR – Use MRS instruction like ‘MRS R0, xPSR’ to save xPSR into a general purpose register.
  • Checking for zero – Test the Z flag using ‘ANDS R0, xPSR #ZFlagMask’ to see if last result was zero.
  • Changing to handler mode – Write to the mode bits using ‘MSR CONTROL, #0x3’ to switch to handler mode.
  • Identifying exceptions – Check the mode bits to see if an exception occurred when waking up from sleep.
  • Context saving – Save xPSR along with other general registers during exception handling.
  • Conditional execution – Use the IT instruction with condition flags to implement Thumb if-then blocks.

Tips for Using xPSR

Here are some tips for working with xPSR register:

  • Use MRS and MSR instructions to read and modify xPSR as it cannot be accessed directly.
  • Be careful when changing mode bits – it can trigger unintended exceptions.
  • Save and restore xPSR during context save and restore operations.
  • Check for specific flags like Z, C, V instead of whole xPSR when possible.
  • Use xPSR flags for conditional execution within IF-THEN blocks.
  • Monitor mode bits to detect processor state changes due to interrupts.
  • Many debuggers can show xPSR contents during program trace and debugging.

Conclusion

The xPSR register is a key component of the ARM Cortex-M processor containing vital status information about the current program state. Understanding its various fields like the flags, mode bits, and exceptions allows developers to write efficient conditional code, monitor interrupts, save context during handling, and debug the application effectively. Referencing and manipulating the xPSR at the right points in the program flow is an important skill for mastering ARM Cortex-M application development.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article What are the disadvantages and benefits of ARM’s CMSIS?
Next Article How many core integer registers does the Cortex-M4 processor have?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

Switching Context Inside an ISR on Cortex-M

Switching between different execution contexts is an important concept on…

6 Min Read

ARM Cortex M Comparison

The ARM Cortex-M series of processors are designed for embedded…

7 Min Read

Switching from MSP to PSP for Cortex-M Task Switching

Task switching on Cortex-M microcontrollers can be done using either…

8 Min Read

Measuring interrupt latency on Arm Cortex-M processors

Interrupt latency is an important performance metric for real-time embedded…

7 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account