Stack on ZP ($04->), TOS in acc, SP in x (points 1 past 2TOS), temp variables in y, $02, $03 (good inlining limits: 3 mem, never increase memory footprint; 5 mem, only the most basic operations; 6 mem, also words whose definition is just two other words; 8 mem, all common primitives) temp1 = $02 temp2 = $03 stack = $04 2tos = $03 3tos = $02 4tos = $01 5tos = $00 Stack manipulation dup: sta stack, x inx (6 cl / 3 mem) drop: dex lda stack, x (6 cl / 3 mem) swap: ldy 2tos, x sta 2tos, x tya (10 cl / 5 mem) nip: dex (2 cl / 1 mem) over: sta stack, x lda 2tos, x inx (10 cl / 5 mem) tuck: ldy 2tos, x sta 2tos, x sty stack, x inx (14 cl / 7 mem) rot: ldy 2tos, x sta 2tos, x lda 3tos, x sty 3tos, x (16 cl / 8 mem) -rot: ldy 3tos, x sta 3tos, x lda 2tos, x sty 2tos, x (16 cl / 8 mem) 2dup: sta stack, x ldy 2tos, x inx sty stack, x inx (16 cl / 8 mem) 2drop: dex dex lda stack, x (8 cl / 4 mem) 2swap: ldy 3tos, x sta 3tos, x sty temp1 lda 2tos, x ldy 4tos, x sta 4tos, x sty 2tos, x lda temp1 (30 cl / 16 mem) 2nip: ldy 2tos, x dex dex sty 2tos, x (12 cl / 6 mem) 2over: sta stack, x lda 4tos, x inx sta stack, x lda 4tos, x inx (20 cl / 10 mem) Indexed stack manipulation pick: sta temp1 stx temp2 txa sec sbc temp1 tax lda 2tos, x ldx temp2 (22 cl / 13 mem) pick-i: sta stack, x lda stack - , x inx (10 cl / 5 mem) swapn: ldy 2tos, x sta temp1 stx temp2 txa sec sbc temp1 tax lda 2tos, x sty 2tos, x ldx temp2 dex (32 cl / 18 mem) (cannot be used with immed 0:) swapn-i:ldy stack - , x sta stack - , x tya (10 cl / 5 mem) Basic arithmetic add: clc adc 2tos, x dex (8 cl / 4 mem) add-i: clc adc # (4 cl / 3 mem) sub: clc sbc 2tos, x eor #$ff dex (10 cl / 6 mem) #sub-i: sec # sbc # (4 cl / 3 mem) (can be done with add-i) rsub: sec sbc 2tos, x dex (8 cl / 4 mem) rsub-i: clc sbc # eor #$ff (6 cl / 5 mem) 2add: clc adc 3tos, x tay lda 2tos, x adc 4tos, x sta 4tos, x tya dex dex (26 cl / 13 mem) 2add-i: clc adc # bcc 2 inc 2tos, x (7-12 cl / 7 mem) (ditto for neg. values) #2add-i: clc # adc # # tay # lda 2tos, x # adc # # sta 2tos, x # tya (18 cl / 11 mem) Memory access fetch: sta stack, x lda (2tos, x) dex (12 cl / 5 mem) store: sta stack, x lda 3tos, x sta (2tos, x) dex dex (18 cl / 8 mem) indir: sta temp2 lda 2tos, x sta temp1 ldy #0 lda (temp1), y sta 2tos, x iny lda (temp1), y (28-29 cl / 15 mem) Dictionary space here: sta stack, x inx inx lda dptrhi sta 2tos, x lda dptrlo (20 cl / 12 mem) allot: adc dptrlo sta dptrlo bcc 3 inc dptrhi (<- fails on neg. allot) dex lda stack, x (17-22 cl / 14 mem)