!pr0
An Even Trickier "Index to Mask"...............Charles Putney
                                                 Dublin, Eire

I got AAL today (September 1984 issue), and pored through it as usual.  The "index" article on page 18 caught my eye.  Naturally I tried to think of a smaller way of coding the routine like your "TRICKIER.WAY" of 32 bytes.  Here it is, in only 23 bytes!

!lm+5
1000 *--------------------------------
1010 *      PUTNEY'S WAY
1020 *--------------------------------
1030 PUTNEY.WAY
1040        AND #7       .....421
1050        LSR          ......42, 1 IN CARRY
1060        PHA          SAVE FOR LATER PLP
1070        LDA #1       INITIAL MASK VALUE
1080        BCC .1       NO NEED TO SHIFT 1
1090        ASL
1100 .1     PLP          GET 1.....42 AS NV.BDIZC
1110        BCC .2       NO NEED TO SHIFT 2
1120        PHP
1130        ASL
1140        ASL
1150        PLP
1160 .2     BNE .3       NO NEED TO SHIFT 4
1170        ASL
1180        ASL
1190        ASL
1200        ASL
1210 .3     RTS
!lm-5

The timing, not including a JSR to it nor the RTS at the end, varies from a best case of 21 cycles to a worst case of 39 cycles.

[One note of warning:  the PLP pulls a status of 000000xx, setting the I-status to zero.  This enables IRQ interrupts, which might be very dangerous if you have an interrupting source connected and were otherwise unprepared.]


Another Tricky Way................................Bruce Love
                                       Hamilton, New Zealand

Here is my effort to improve your version of turning an index into a mask.  It uses (shudder!) self-modifying code, but it is shorter and faster and I think easy to understand.

!lm+5
LOVE.VERSION
       AND #7
       EOR #7
       STA .1+1
       LDA #1
.1     BNE .1  (OFFSET FILLED IN)
       ASL
       ASL
       ASL
       ASL
       ASL
       ASL
       ASL
       RTS
!lm-5


And still another................................David Eisler
                                          Littleton, Colorado

With reference to "Turn Index into a Mask" (AAL Sept 84), here is another tricky alternative.  It uses only the A-register, is only 16 bytes long, and takes 9 to 23 cycles.

!lm+5
EISLER.VERSION
       AND #7
       STA .1+1
       LDA #$80
.1     BNE .1  (OFFSET FILLED IN)
       LSR
       LSR
       LSR
       LSR
       LSR
       LSR
       LSR
       RTS
!lm-5
