Zero Flag

From C64-Wiki
(Redirected from Zero flag)
Jump to navigationJump to search

The zero status flag is one of seven single-bit flags stored in the processor status register of the 6510 CPU. The zero flag indicates the result of a 6510 instruction as being numerically zero (0) or non-zero. If an operation results in a value of zero, then the flag is set (1). If an operation results in a non-zero value, then the flag is cleared (0).

Discussion[edit | edit source]

The zero status flag is most often used as the basis for branching routines after an arithmetic or logical operation has been performed, as in the following source code:

*=c000
      ldx #$10   ; load the X index register with 10h (16 decimal)
DEX1: dex        ; decrement the value in the X index register
      bne DEX1   ; branch back to DEX1 if the zero flag is NOT set (i.e. not equal)

In this example the X index register is initially loaded with the value 10h (16 decimal). The next instruction decrements (ie. subtracts one from) X index register, to a value of 0Fh (15 decimal). The branching instruction BNE (Branch if Not Equal) evaluates the state of the zero status flag. Since the result of the DEX instruction was not zero (0), the BNE instruction is satisfied (Branch if Not Equal) and branches back to DEX1.

This loop continues until the X index register reaches a value of zero (0). When the DEX instruction finally sets the value in the X index register, the branching instruction BNE (Branch if Not Equal) fails, because the Zero status flag is not set, and the loop ends.

Execution now continues with the instruction following the BNE instruction.

Associated Instructions[edit | edit source]

The following instructions affect the Zero status flag

ADC AND ASL BIT CMP CPX CPY
DEC DEX DEY EOR INC INX INY
LDA LDX LDY LSR ORA PLA PLP
ROL ROI RTI SBC TAX TAY TSX
TXA TXS TYA 0 0 0 0

The following instructions evaluate the zero status flag or push the 6510's status register, including the zero status flag onto the Stack.

BEQ BNE BRK PHP