0

Studying the ARM Thumb instruction set, I noticed that while the general-purpose registers and Link Register are pushed (most of the time) onto the stack, the status register (CPSR) is not. This surprised me, as the status register is part of the program state that needs to be recovered after the ending of a subroutine. Is there a reason the status register is not saved?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Bas
  • 881
  • 1
  • 10
  • 23
  • Why should it be pushed into stack? The PSR registers are banked per *mode* of operation and are containing a global state of the system and not of a function. Aside, no one is preventing you from saving it yourself if needed. – Eugene Sh. Aug 10 '22 at 14:12
  • 3
    ARM calling conventions usually do not call for the flags to be preserved. – fuz Aug 10 '22 at 14:22
  • 1
    Note that some general purpose registers are also modified by the function and not restored. So CPSR is not special here. – Raymond Chen Aug 10 '22 at 14:27
  • CPSR is call-clobbered in the standard calling convention. See [What are callee and caller saved registers?](https://stackoverflow.com/a/56178078) to understand more about call-preserved vs. call-clobbered in a calling convention. – Peter Cordes Aug 10 '22 at 20:16

1 Answers1

1

I used thumb2 in college to write part of an OS. There will be a time you see the CSPR pushed onto the stack, when you move from handler mode to unprivileged thread mode or vice versa. Almost all registers are popped/pushed onto the stack when switching modes.

It is not in ARM convention to maintain it when returning from subroutines in the same mode, but the flags of the last transaction can be manually saved and referenced.