1
  "mov #10,r5\n"
  "sub #1,r5\n" //test value

I run in hardware, when 'sub' exec, the Carry flag is set. why?

enter image description here

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Duo jia
  • 13
  • 2

1 Answers1

3

When it comes to subtraction, the carry flag is reversed compared to addition. If the value wraps around, carry is set to 0. If it doesn't (as in your example) it is set to 1.

Unfortunately, this is not 100% clear when reading the processor manual. However, this behavior is consistent with other processors, e.g. 6502.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • 1
    Some other ISAs like 8086 use their carry flag as a borrow output. Others, like ARM (and apparently MSP430 and 6502) use it as a `!borrow` out of the top bit. So you have to check the manual, e.g. look at the jump if unsigned-lower instruction to see if it looks for C==0 or C==1. – Peter Cordes Nov 08 '21 at 10:12