tags:

views:

692

answers:

4

I was looking up some ARM assembly and i notice EOR/XOR and AND all set the C and O flag. How does that work? Are they always cleared? what conditions are they set?

+2  A: 

Recall that you can arbitrarily shift the result following the operation. The carry flag is set based on the carry out from the barrel shifter. The overflow flag is never affected.

moonshadow
+4  A: 

I don't believe the normal AND/EOR set the carry and overflow flags at all. The ANDS and EORS variants (AND/EOR with set) can set carry but only if the right hand side is a shifted operand. Neither of these affect the overflow flag at all.

For example,

ANDS R0,R1,R2, LSR #1

will set carry depending on b0 of R2. If the bit was 1 (i.e., R2 was odd), carry will be set.

See here for the gory details (chapter 3, the instruction set).

paxdiablo
+2  A: 

As other posters noted, the C flag is updated based on a shift that is optionally applied to one of the source registers before the operation.

The V (overflow) flag is unaffected by these instructions.

The Z flag is set if the result is exactly zero, and the N flag is set if the result is negative when viewed as a twos-complement integer (i.e. the high order bit is a one).

Stephen Canon
+4  A: 

If you decide to continue with ARM assembler you need a copy of the ARM ARM, which is the ARM Architecture Reference Manual. There are many versions and each have their own "features". I recommend collecting as many as you can but thats another story. There are a number of ways to get an electronic copy for free. Either way:

AND you need the S bit set which for ARM means use ANDS instead of AND. For thumb mode you are always using ANDS but gas for example refuses to accept ANDS in thumb mode (but always disassembles it as an ands).

If S==1 and Rd == R15 then 
  CPSR=SPSR
else if S==1
  N Flag = Rd[31]
  Z Flag = if Rd == 0 then 1 else 0
  C Flag = shifter_carry_out
  V flag = unaffected

EOR is the same as AND when it comes to the flags

dwelch
wow that looks ugly, I didnt format it right. Anyway google search for "the arm arm" without the quotes and the first hit is rev I of the arm arm directly from arm.com
dwelch
i fixed the formatting for you. Select the text and hit the binary looking button.
acidzombie24