9.5.1. Fastest ROMable 6502 NEXT

V následujícím kódu je JV adresa v zero page. Na ní je uložen kód instrukce JMP (addr) který je zde uložen v rámci inicializace. Za ní na adresách JV+1 a JV+2, které slouží jako 16-bitový registr, je je uložana adresa následující instrukce.

NEXT:
        INC JV+1
        BEQ NEXT1:
        INC JV+1
        BEQ NEXT2
        JMP JV
NEXT1:
        INC JV+1
NEXT2:
        INC JV+2
        JMP JV
ENTER:  ; HL def begins with JMP ENTER
        LDA JV+2
        PHA
        JDA JV+1
        PHA
        LDA #$6C
        STA JV
        LDY #0
        LDA (JV+1),Y
        CLC
        ADC #3
        PHA
        INY
        LDA (JV+1),Y
        ADC #0
        STA JV+2
        PLA
        STA JV+1
        JMP JV
EXIT:
        PLA
        STA JV+1
        PLA
        STA JV+2
NEXT:
        INC JV+1
        BEQ NEXT1:
        INC JV+1
        BEQ NEXT2:
        JMP JV
NEXT1:
        INC JV+1
NEXT2:
        INC JV+2
        JMP JV

On the other end of the spectrum, if you want multiple stacks for multiple threads, you can provide 32 deep data stacks and 16 deep return stacks with separate high byte and low byte stacks, giving 8 independent threads with three dedicated pages of RAM:

DL -- low data stack byte, aligned on a page boundary
DH = DL+256
RL = RH+256
RH = RL+128

If you have 8K RAM, with the zero page and hardware stack, this is 1 1/4K, add a page per thread for user variables, PAD, and Pictured numeric input, and you have allocated 5 1/4K, leaving 2 3/4K for I/O and other uses (block buffers, input buffer, serial port buffer)

ENTER:  ; HL def begins with JSR ENTER
        DEY
        LDA JV+2
        STA RH,Y
        LDA JV+1
        STA RL,Y
        LDA #$6C
        STA JV
        PLA
        CLC
        ADC #1
        STA JV+1
        PLA
        ADC #0
        STA JV+2
        JMP JV
ENTER1:
        INC JV+2
        JMP JV

EXIT:
        LDA RH,Y
        STA JV+2
        LDA RL,Y
        STA JV+1
        INY
NEXT:
        INC JV+1
        BEQ NEXT1:
        INC JV+1
        BEQ NEXT2:
        JMP JV
NEXT1:
        INC JV+1
NEXT2:
        INC JV+2
        JMP JV