Saving bytes, byte by byte - an example

Programming the 6502 microprocessor and its relatives in assembly and other languages.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Saving bytes, byte by byte - an example

Post by GARTHWILSON »

Bregalad wrote:
GARTHWILSON wrote:
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).
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
RichTW
Posts: 95
Joined: 06 Oct 2010
Location: Palma, Spain

Re: Saving bytes, byte by byte - an example

Post 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:

Code: Select all

ASL A
ROL A
ROL A
AND #3
Save 1 byte and 4 cycles. Not a big space-saver, but definitely worthwhile.
CurtisP
Posts: 79
Joined: 10 Feb 2011

Re: Saving bytes, byte by byte - an example

Post 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.
White Flame
Posts: 704
Joined: 24 Jul 2012

Re: Saving bytes, byte by byte - an example

Post 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.
CurtisP
Posts: 79
Joined: 10 Feb 2011

Re: Saving bytes, byte by byte - an example

Post by CurtisP »

As long as you don't need to preserve the accumulator, of course
White Flame wrote:
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.
Post Reply