SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: What is the Application Program Status Register (APSR) 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 Application Program Status Register (APSR) in Arm Cortex-M?

Jamie Kellett
Last updated: October 5, 2023 9:40 am
Jamie Kellett 9 Min Read
Share
SHARE

The Application Program Status Register (APSR) in Arm Cortex-M is a 32-bit register that contains application level status and control information. It provides details on the condition flags from previous instruction executions as well as information on the current processor state. The APSR allows monitoring and control of application programs in Arm Cortex-M processors.

Contents
Overview of the APSRAPSR Register FormatCondition FlagsSaturation FlagGreater Than or Equal FlagsProcessor Mode FieldReading the APSRWriting the APSRUsing the APSR FlagsAPSR in Exception HandlingInteraction with xPSRTips and TricksConclusion

Overview of the APSR

The APSR is generally used to check the status of the conditional flags and the current processor mode. It contains several fields that provide this information:

  • N – Negative flag
  • Z – Zero flag
  • C – Carry flag
  • V – Overflow flag
  • Q – Saturation flag
  • GE – Greater than or Equal flags
  • Mode – Indicates current processor mode

The condition flags (N, Z, C, V) are set by instruction executions and indicate the result of operations. For example, an ADD instruction that results in a negative result will set the N flag. These flags can be tested by conditional instructions to alter program flow.

The Q and GE flags are related to DSP operations and indicate saturation or pass/fail results on those type of instructions.

The Mode field indicates the current processor mode – Thread, Handler, etc. This informs debuggers and monitors of the current program state.

By reading the APSR, a program can check the status flags, saturation conditions, or processor mode very efficiently with a single register access. The flags in the APSR are useful for optimizing branches, loops, and conditionally executed code.

APSR Register Format

The APSR register format is as follows: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 | N | Z | C | V | Q | | Mode |

  • Bits 31-28 – Condition flags (N, Z, C, V)
  • Bit 27 – Saturation flag (Q)
  • Bits 26-24 – Reserved
  • Bits 23-16 – Greater than or Equal flags (GE)
  • Bits 15-0 – Processor mode field

Condition Flags

The condition flags in bits 31-28 provide the negative (N), zero (Z), carry (C), and overflow (V) status flags. These flags are updated after most data processing instructions like ADD, SUB, etc. They can be tested using conditional execution instructions like BEQ, BGT, etc.

Saturation Flag

Bit 27 contains the Q flag which indicates saturated results. It is set if a DSP saturating instruction saturates the result.

Greater Than or Equal Flags

Bits 23-16 contain the GE flags which are used for DSP threshold compare instructions. A bit in this field is set if the comparison passes for that number.

Processor Mode Field

The lower 16 bits contain the current processor mode encoding. This indicates if the processor is in Thread mode, Handler mode, or another supported mode.

Reading the APSR

To read the APSR, the MRS (Move to Register from Special Register) instruction is used like: MRS R0, APSR

This will copy the value in the APSR to general purpose register R0 where it can be examined. The condition flags, mode, and other fields will be present in the R0 register value.

For example: ; Execute instruction that sets condition flags ADD R1, R2, R3 ; Read APSR into R0 MRS R0, APSR ; R0 now contains flags/mode from APSR

Writing the APSR

To write to the APSR, the MSR (Move to Special Register from Register) instruction is used like: MSR APSR, R0

This will copy R0 into the APSR to set the condition flags and other fields. Note that some bits like the Q flag cannot be set directly by software.

For example: ; Create value to write to APSR in R0 MOV R0, #0xF00000 ; Copy R0 to APSR MSR APSR, R0 ; APSR now has flags/mode from R0

Using the APSR Flags

The condition flags in the APSR are used to conditionally control program flow and execution. Some examples include:

  • Branching on zero (BEQ)
  • Branching on positive/negative (BPL, BMI)
  • Compare and branch on equal (CBNE)
  • Conditional selection (IT/SEL)

Based on the flags from a previous instruction, a conditional instruction can execute or be skipped. This avoids having to explicitly compare a value in a register.

For example: ; Perform subtractions SUBS R1, R2, R3 ; Branch on negative (N flag set) BMI failure ; Branch on zero (Z flag set) BEQ done ; Continue if positive number

The SUBS instruction sets the N and Z flags which are automatically tested by the conditional branching. No explicit compare is needed against R1.

APSR in Exception Handling

The APSR plays an important role in exception handling on Cortex-M processors. When an exception occurs:

  1. The current value of the APSR is automatically saved to the stacked PSR
  2. The exception handler runs
  3. The APSR is restored on exception return

This ensures the flags and status are preserved across the exception. The handler code can also view the stacked PSR to inspect the APSR at the time of the exception.

Interaction with xPSR

The xPSR (extended PSR) is a special register that contains the APSR along with the IPSR and EPSR registers. When code reads/writes the xPSR, it is accessing the APSR component specifically.

For example: MRS R0, xPSR ; Read xPSR into R0 ; R0 bits 31-28 contain APSR condition flags ; R0 bits 24-16 contain APSR GE flags ; R0 bits 15-0 contain APSR mode field

The xPSR can be thought of as a larger register that encompasses the APSR (along with IPSR and EPSR).

Tips and Tricks

Here are some useful tips for working with the APSR register:

  • Use MRS and MSR to read and write the APSR efficiently.
  • Test condition flags to optimize conditional code and minimize explicit compares.
  • Inspect stacked xPSR values in exception handlers to view APSR contents.
  • Set PUSH and POP instructions to automatically save/restore APSR for function calls.
  • Utilize LDM and STM to save and restore multiple registers including APSR.
  • Combine conditional execution with APSR flag tests for very efficient if/then code.

Conclusion

The Application Program Status Register (APSR) contains important condition flags, status bits, and mode settings for monitoring and controlling application programs. By leveraging the APSR, developers can optimize code flow, handle exceptions, and simplify conditional execution. The APSR is a powerful tool available in all Cortex-M processors that should be utilized to its full potential.

Understanding the format fields of the APSR provides the foundation for effectively applying it within application code. The ability to quickly read and write the APSR using MRS and MSR allows setting up and testing the flags. Exception handling relies on the automatic stacking of the APSR on interrupts to preserve the state. And the APSR integrated within xPSR gives a full view of the current processor status.

Overall, mastering usage of the Application Program Status Register helps unlock the performance and efficiency benefits of conditional execution, optimized branching, streamlined interrupt handling, and more on Arm Cortex-M processors.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article ARM Program Status Registers
Next Article What are the the Application Program Status Register (APSR) in Arm Cortex-M
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

GNU-ARM Compiler Performance for Cortex-M0/M1

The GNU Arm Embedded Toolchain provides a complete open source…

5 Min Read

What is the endianness of arm cortex M33?

The ARM Cortex-M33 is a little endian processor. This means…

7 Min Read

ARM Architecture

ARM processors are among the most popular CPU architectures in…

11 Min Read

File System Limitations on Cortex-M1 in FPGA Implementations

The Cortex-M1 processor from ARM is a popular choice for…

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

Sign in to your account