Negative Flag

From C64-Wiki
(Redirected from Negative status flag)
Jump to: navigation, search

The Negative Status Flag is a processor status flag, which indicates that the result of an operation, when read as a binary number with two's complement sign, represents a negative number. If this flag is cleared, the result was either positive or zero. The following instructions set or clear the negative status flag according to the result of their operation: ADC, AND, ASL, BIT, CMP, CPX, CPY, DEC, DEX, DEY, EOR, INC, INX, INY, LDA, LDX, LDY, LSR, ORA, PLA, PLP, ROL, ROR, RTI, SBC, TAX, TAY, TSX, TXA, TXS and TYA.

Branching instructions[edit]

Two branching instructions, are used to perform conditional jumps according to the state of the negative status flag:

  • BMI performs the jump only if the negative status flag is set.
  • BPL performs the jump only if the negative status flag is cleared.

Use when comparing signed numbers[edit]

Notice that the comparison operations, CMP, CPX and CPY, work by performing a subtraction of the operand from the register being compared: The result itself is discarded, but the resulting states of the negative, zero and carry status flags are preserved. If the compared numbers use two's-complement signs, the negative status flag is used to determine which number was largest:

           LDX #NumberA
           CPX #NumberB
           BMI ASmallest
           BNE ALargest
           ...           ; This part is executed if NumberA = NumberB
           JMP Done

ALargest:  ...           ; This part is executed if NumberA > NumberB
           JMP Done

ASmallest: ...           ; This part is executed if NumberA < NumberB
Done:

For comparson of unsigned bytes, refer to the Carry status flag article.