Page 2 of 2
Re: Saving bytes, byte by byte - an example
Posted: Mon May 08, 2017 7:27 am
by GARTHWILSON
Another one to add even to the NMOS part is removing the CMP #0 after anything affecting the accumulator, or the CPY #0 after anything affecting Y, or the CPX #0 after anything affecting X.
This is quite obvious, not even an "optimisation".
Totally agreed; but the waste of these instructions is epidemic. One person posting there recently said he just wanted to make the code more clear (which it does not do).
Re: Saving bytes, byte by byte - an example
Posted: Mon May 08, 2017 1:11 pm
by RichTW
A favourite one of mine is, to divide by 64, instead of:
Code: Select all
LSR A
LSR A
LSR A
LSR A
LSR A
LSR A
do this:
Save 1 byte and 4 cycles. Not a big space-saver, but definitely worthwhile.
Re: Saving bytes, byte by byte - an example
Posted: Thu May 11, 2017 4:10 pm
by CurtisP
I'm working on a 6502-targeted compiler, which is generating a lot of STA/LDA to the same location, so I plan to write some sort of peep-hole optimizer. I've decided to use straight BRx instructions in the IF/ELSE structures, even though that will limit the number of instructions in the blocks, because they are so common. FOR/WHILE/DO loops will use BRx/JMP combinations.
Re: Saving bytes, byte by byte - an example
Posted: Thu May 11, 2017 5:05 pm
by White Flame
For doing an in-range test, instead of testing both the low bound & high bound, offset it so the range starts at 0 and do an unsigned test with the high bound.
Code: Select all
lda value
sec
sbc #low_bound
cmp #high_bound-low_bound
bcs out_of_bounds
If you don't need the SEC in there, it saves a fair percentage of bytes & cycles.
Re: Saving bytes, byte by byte - an example
Posted: Tue May 23, 2017 1:21 am
by CurtisP
As long as you don't need to preserve the accumulator, of course
For doing an in-range test, instead of testing both the low bound & high bound, offset it so the range starts at 0 and do an unsigned test with the high bound.