3

I'm doing some testing using ARM assembler (specifically ARM7 little-endian), and I can't find any way of setting the flags/CPSR register without setting them indirectly using an arithmetic instruction.

Is there a way I can write directly to the CPSR flags instead?

Notlikethat
  • 20,095
  • 3
  • 40
  • 77
Ravi
  • 49
  • 1
  • 4
  • Why do you need to change CPSR? There is probably a way to achieve what you actually need without fiddling with the register directly. – Igor Skochinsky Dec 03 '14 at 17:48
  • [Google **setting arm CPSR**](https://www.google.com/#q=setting+arm+cpsr) and [2nd hit is heyrick](http://www.heyrick.co.uk/armwiki/The_Status_register) which has the information. – artless noise Dec 03 '14 at 17:56
  • There are also many other questions in [the arm tag](http://stackoverflow.com/search?q=[arm]+cpsr+is%3Aquestion) that have the answer to your question, such as [Accessing banked registers](http://stackoverflow.com/questions/2784978/explicitly-accessing-banked-registers-on-arm). – artless noise Dec 03 '14 at 18:03
  • @artlessnoise That and the other questions really don't obviously help beginners who just want to set the flags - I was a little suprised to find [we didn't have a good _clear_ duplicate for "how do I set the flags?"](http://stackoverflow.com/search?q=cpsr_f), so now we do. – Notlikethat Dec 03 '14 at 21:10
  • @Notlikethat True, the information is not on StackOverflow in an obvious question, but it is in hundreds of other places. – artless noise Dec 04 '14 at 01:02
  • @artlessnoise As a beginner to this, the documentation gets very confusing very quickly, and I needed a simple answer. But I admit the information was out there. – Ravi Dec 06 '14 at 12:58

2 Answers2

4

The MSR instruction writes to the PSRs. Note that on an ARM7 it's only available in a privileged mode (the notion of APSR for nonprivileged access to certain CPSR fields came in later architectures).

The ARMv4t reference manual includes this specific example of clearing the flags:

MRS R0, CPSR ; Read the CPSR
BIC R0, R0, #0xf0000000 ; Clear the N, Z, C and V bits
MSR CPSR_f, R0 ; update the flag bits in the CPSR

Note the read-modify-write operation to preserve reserved bits for the register form. For the specific case of setting flags, writing an immediate value directly to CPSR_f is permitted provided you write zeros to the reserved bits.

Notlikethat
  • 20,095
  • 3
  • 40
  • 77
1

Have you tried the MSR and MRS instructions?

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38