Search code examples
assemblymsp430instruction-set

CMP and carry flag


Processor: MSP430 16 bit RISC

Can someone explain the CMP instruction in terms of when the carry flag is actually set below. From the manual it says,

CMP(.B) src,dst ..... dst - src

If src is not equal to dst, will the carry flag be set?

cmp    r15, r11
jnc    #1234

Solution

  • The User's Guide says:

    Description
    The source operand is subtracted from the destination operand. This is made by adding the 1s complement of the source + 1 to the destination. The result affects only the status bits in SR.
    […]
    Status Bits
    C: Set if there is a carry from the MSB, reset otherwise

    In other words, C is set if there is an unsigned overflow.

    This can also be seen in the jump instructions: JC (jump if carry) and JHS (jump if higher or same) are the same instruction, as are JNC (jump if no carry) and JLO (jump if lower).

    Example     If R5 ≥ R6 (unsigned), the program continues at Label2.

    CMP R6,R5        ; Is R5 >= R6? Info to C
    JHS Label2       ; Yes, C = 1
    ...              ; No, R5 < R6. Continue