Search code examples
assemblyarmthumb

How does subtracting 0x1 - 0x80000000 cause an overflow?


  MOV   R0, #0x80000000 
  MOV   R1, #0x1 
  SUBS  R2, R1, R0

Upon Running this code, Flag N and Z are set. Now, I know N flag is set if the operation results in a negative result and Z flag is set when there is an overflow.

The thing I don't understand is that how does 0x1 - 0x80000000 causes overflow. Any help is appreciated!


Solution

  • Consider extending the two numbers to 36 bits. 1 is still 1, of course. 80000000 becomes f80000000. 1 - f80000000 = 080000001, a positive number. Since 080000001 doesn't fit in 32 bits as a positive number, there is overflow.