WHOAHBOT!

6502, assembly and you. Part 3

Before I begin, let me say that while programming for the 6502, one must surely consider the following musical choices
to be at a premium: Studio One Scorcher, or at the
very least: Budos Band II.

At any rate! If you have been following along, you surely have guessed that the missing instruction from the quiz in part 2 is of course TXA!

Once, again, our cast of characters:

The Sim, The Book, The Reference

And on to the plot. This is act 3, the climax and then the dénouement!

Processor status register!

On our happy little substrate of silica, we have the following flags that are stored in the processor status register:

bit   7                           0
    +---+---+---+---+---+---+---+---+
    | N | V |   | B | D | I | Z | C |
    +---+---+---+---+---+---+---+---+

Instructions to the processor can set these flags after an instruction to indicate things like whether the result of the last instruction (dec, inc, load) was zero. These all have their use, but we will set careful study for the moment upon the Z flag.

By the by, if you haven’t read Zero: The Biography of a Dangerous Idea, you most certainly would enjoy it… By the by and by, if I lent you my copy of that book, could you please return it?

Avast! Processor status flags, like our friend Zero are quite handy for control flow, as in the following example:

LDA #01
LDX #05
jump:
STA $01FF,x
DEX
BNE jump

Pray, notice carefully that we are no longer jumping forever! There is a new sherrif in town and her name is BNE. Also note carefully the section of the reference that speaks volumes of the DEX instruction:

DEX  Decrement Index X by One

     X - 1 -> X                       N Z C I D V
                                      + + - - - -

     addressing    assembler    opc  bytes  cyles
     --------------------------------------------
     implied       DEC           CA    1     2

The two plusses there indicate that this instruction can set the N or Z flags in the processor status register, in this case, decrementing X during the last run will set the Z flag of the processor status register, and the BNE instruction checks to see if that flag is set before jumping back into he subroutine.

Gifted with this new information, the careful bit manipulation of the skilled assembly alchemist becomes clear, and much more interesting operations present themselves.

Do be careful when cooking with this recipe! Never forget that unless you are wary, another operation may have set that processor status flag much earlier in your program, and it may not be safe to use unless cleared beforehand.

Thank you, dear reader. I bid you the fondest of farewells for now. I may continue with this series, if my mad education in this realm continues, but I shall always keep fond memories of our times together and the bits we flipped.