!pr3
Reminder about Wrap-Around Addressing.............Bill Parker

Buried on the right side of page 65 of the November, 1983 issue of Call APPLE is the examination by Martin Smith of another quirk of the 6502.  I say "another quirk" because it is similar to the JMP indirect wrap-around bug.  Remember it?

As reported in the October 1980 issue of Apple Assembly Line, "JMP ($xxFF)" will not jump to the address pointed to by the two bytes beginning at $xxFF; rather the two bytes at $xxFF and $xx00 will be used.  (Where xx means any page of memory.

A similar wrap-around situation can be found when indexing like this:

       STACK  .EQ $100
              LDX #1
              LDA STACK-1,X

Since STACK-1 is $FF, a page zero address mode is assembled.  Indexing from within page zero never leaves page zero, so the above example references loacation $0000 rather than $0100.

The above is important, because many programmers use it in a "WHEREAMI" section of code to find the program's current address:
       STACK    .EQ $100
       WHEREAMI JSR $FF58   (KNOWN rts INSTRUCTION)
                TSX
                LDA STACK-1,X   GET PCL
                LDY STACK,X     GET PCH

For the Merlin Assembler, the problem can be corrected by forcing the assembler to use an absolute addressing mode rather than a page zero addressing mode.  This is done by suffixing a ":" to the opcode, like this:

                LDA: STACK-1,X

The S-C Assemblers have no syntactical way to force absolute mode, but it can be done by defining the symbol STACK after its use.  Here's an interesting example:

     0800- BD FF 00 1000        LDA STACK-1,X
     0100-          1010 STACK  .EQ $100
     0803- B5 FF    1020        LDA STACK-1,X

Since the assembler doesn't know the value of STACK in the first line, it has to assume it will be a two-byte address, and allocates that much space.  By the time it gets to the last line it knows better.

The fact that indexing wraps around inside page zero is a plus sometimes.  (I guess that explains why the chip works that way!)  It has the effect of letting you use both positive and negative index offsets.  Just beware of getting so used to negative offsets that you try to use them OUTSIDE page zero!

!np

Clarification about our copyrights.........Bob Sander-Cederlof

We frequently are asked if it is all right to use ideas and even programs published in the Apple Assembly Line in articles or books our readers write for publication elsewhere, or even in software they plan to sell.

Sure!  Just give us credit.  Say where you got it, and hopefully tell your customers how they too can subscribe.  The more you sell, the more we sell.  The more we spread the good information around, the more we all benefit.
