My Favorite Instruction of 2023

Basic and Machine Language

Moderator: Moderators

User avatar
pixel
Vic 20 Guru
Posts: 1548
Joined: Fri Feb 28, 2014 3:56 am
Location: Bielefeld, Germany

Re: My Favorite Instruction of 2023 / 2024

Post by pixel »

Mike wrote: Fri Nov 15, 2024 7:51 am About the only address mode that goes unused is (ZP,X) - and only in more recent times I have found use for that address mode, not just for the trivial case of X=0, but also when X<>0.
I second this. Wanna slap myself around all Sunday for X < 128.
A man without talent or ambition is most easily pleased. Others set his path and he is content.
https://github.com/SvenMichaelKlose
User avatar
Mike
Herr VC
Posts: 5134
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: My Favorite Instruction of 2023 / 2024

Post by Mike »

pixel wrote:I second this.
Using (ZP,X) with X<>0 comes around with reserving a larger part of the ZP as data stack and using X as data stack pointer. Especially ($00,X) can be used to work on memory data with a set of pointers contained in said data stack. INC ZP,X and DEC ZP,X are available to conveniently increment or decrement these pointers - $00,X and $01,X address the respective low- and high-byte of the pointers. Other offsets can be used to probe deeper into parameter lists: as indirect-ZP wraps, the "base address" actually works as signed byte offset on the data stack pointer in X!

If one dispenses with BASIC as built-in language and only uses KERNAL routines, the addresses $00..$8F are free for that purpose.

A rather benign use of that is found in MINIMON, where I use INC $00,X and INC $01,X to increment two pointers in ZP by the same amount (this is necessary in the T command when copying backwards, then the pointers to source and target block are moved to their respective end). Instead of:

Code: Select all

CLC
LDA src  :ADC size  :STA src
STA src+1:ADC size+1:STA src+1
CLC
LDA dst  :ADC size  :STA dst
STA dst+1:ADC size+1:STA dst+1
... which uses 30 bytes (size resides outside ZP), I do:

Code: Select all

 [...]
 LDX #src:JSR Adjust
 LDX #dst:JSR Adjust
 [...]
.Adjust
 CLC
 LDA $00,X
 ADC size
 STA $00,X
 LDA $01,X
 ADC size+1
 STA $01,X
 RTS
... which just needs 26 bytes. :)

Another use in MINIGRAFIK, with X either 0 or 1, lets the current pixel co-ordinates in $FD/$FE run into the direction of the endpoint in $F9/$FA, or stop when both are equal (and signal that condition to the caller in the Z flag):

Code: Select all

.2417  B5 FD     LDA $FD,X
.2419  D5 F9     CMP $F9,X
.241B  F0 09     BEQ $2426
.241D  08        PHP
.241E  B0 03     BCS $2423
.2420  F6 FD     INC $FD,X

>2422  2C

.2423  D6 FD     DEC $FD,X
.2425  28        PLP
.2426  60        RTS
Forth anyone? :mrgreen:
User avatar
pixel
Vic 20 Guru
Posts: 1548
Joined: Fri Feb 28, 2014 3:56 am
Location: Bielefeld, Germany

Re: My Favorite Instruction of 2023 / 2024

Post by pixel »

Mike wrote: Sun Nov 17, 2024 8:06 am Forth anyone? :mrgreen:
https://github.com/SvenMichaelKlose/tun ... src/lib/zp

That's cute. But combine pointers and stacks with indexed indirect X... :mrgreen:
A man without talent or ambition is most easily pleased. Others set his path and he is content.
https://github.com/SvenMichaelKlose
User avatar
Mike
Herr VC
Posts: 5134
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: My Favorite Instruction of 2023

Post by Mike »

pixel wrote:

Code: Select all

.proc zpw_dec_x
    dec 0,x
    lda 0,x
    cmp #$ff
    bne :+
    dec 1,x
:   rts
.endproc
One instruction shorter:

Code: Select all

.proc zpw_dec_x
    lda 0,x
    bne :+
    dec 1,x
:   dec 0,x
    rts
.endproc
;)
User avatar
pixel
Vic 20 Guru
Posts: 1548
Joined: Fri Feb 28, 2014 3:56 am
Location: Bielefeld, Germany

Re: My Favorite Instruction of 2023

Post by pixel »

Mike wrote: Sun Nov 17, 2024 8:40 am One instruction shorter:

Code: Select all

.proc zpw_dec_x
    lda 0,x
    bne :+
    dec 1,x
:   dec 0,x
    rts
.endproc
;)
m)
A man without talent or ambition is most easily pleased. Others set his path and he is content.
https://github.com/SvenMichaelKlose
Post Reply