### BRANCHES AND TESTING

Another very important principle in machine language is the ability to test, and detect certain conditions, in a similar fashion to the "IF... THEN, IF... GOTO" structure in CBM BASIC.

The various flags in the status register are affected by different instructions in different ways. For example, there is a flag that is set when an instruction has caused a zero result, and is reset when a result is not zero. The instruction:

```   LDA #\$00
```
will cause the zero result flag to be set, because the instruction has resulted in the accumulator containing a zero.

There are a set of instructions that will, given a particular condition, branch to another part of the program. An example of a branch instruction is BEQ, which means Branch if result EQual to zero. The branch instructions branch if the condition is true, and if not, the program continues onto the next instruction, as if nothing had occurred. The branch instructions branch not by the result of the previous instructions), but by internally examining the status register. As was just mentioned, there is a zero result flag in the status register. The BEQ instruction branches if the zero result flag (known as Z) is set. Every branch instruction has an opposite branch instruction. The BEQ instruction has an opposite instruction BNE, which means Branch on result Not Equal to zero (i.e., Z not set).

The index registers have a number of associated instructions which modify their contents. For example, the INX instruction INcrements the X index register. If the X register contained \$FF before it was incremented (the maximum number the X register can contain), it will "wrap around" back to zero. If you wanted a program to continue to do something until you had performed the increment of the X index that pushed it around to zero, you could use the BNE instruction to continue "looping" around, until X became zero.

The reverse of INX, is DEX, which is DEcrement the X index register. If the X index register is zero, DEX wraps around to \$FF. Similarly, there are INY and DEY for the Y index register.

 This page has been created by Sami Rautiainen. Read the small print. Last updated July 10, 2002.