Code optimization technique

Programming the 6502 microprocessor and its relatives in assembly and other languages.
BillG
Posts: 710
Joined: 12 Mar 2020
Location: North Tejas

Re: Code optimization technique

Post by BillG »

These are some of the noteworthy examples from ORing or XORing with constants.

Taking advantage of the fact that anything ORed with one is one:

Code: Select all

                          00321 ; 00013     W0 := W1 or $FF00;
 0106 D6 35           [3] 00322          ldab   W1+1
 0108 86 FF           [2] 00323          ldaa   #$FF
 010A 97 38           [4] 00324          staa   W0
 010C D7 39           [4] 00325          stab   W0+1
                          00326 ; 00014     W0 := W1 or $FF;
 010E 96 34           [3] 00327          ldaa   W1
 0110 C6 FF           [2] 00328          ldab   #$FF
 0112 97 38           [4] 00329          staa   W0
 0114 D7 39           [4] 00330          stab   W0+1
Something XORed with zero is unchanged:

Code: Select all

                          00331 ; 00015     W0 := B1 xor $9900;
 0116 D6 18           [3] 00332          ldab   B1
 0118 86 99           [2] 00333          ldaa   #153
 011A 97 38           [4] 00334          staa   W0
 011C D7 39           [4] 00335          stab   W0+1
XORing with one flips the bit. On the 6800, we have an instruction for that. Same speed but only a single byte:

Code: Select all

                          00336 ; 00016     W0 := W1 xor $FF00;
 011E 96 34           [3] 00337          ldaa   W1
 0120 D6 35           [3] 00338          ldab   W1+1
 0122 43              [2] 00339          coma
 0123 97 38           [4] 00340          staa   W0
 0125 D7 39           [4] 00341          stab   W0+1
                          00342 ; 00017     W0 := W1 xor $FF;
 0127 96 34           [3] 00343          ldaa   W1
 0129 D6 35           [3] 00344          ldab   W1+1
 012B 53              [2] 00345          comb
 012C 97 38           [4] 00346          staa   W0
 012E D7 39           [4] 00347          stab   W0+1
This is the usual sign extension:

Code: Select all

                          00348 ; 00018     W0 := S1;
 0130 D6 36           [3] 00349          ldab   S1
 0132 86 7F           [2] 00350          ldaa   #$7F      ; Thanks Mike B!
 0134 11              [2] 00351          cba
 0135 82 7F           [2] 00352          sbca   #$7F
 0137 97 38           [4] 00353          staa   W0
 0139 D7 39           [4] 00354          stab   W0+1
No need to actually do the sign extension here...

Code: Select all

                          00355 ; 00019     W0 := S1 or $FF00;
 013B D6 36           [3] 00356          ldab   S1
 013D 86 FF           [2] 00357          ldaa   #$FF
 013F 97 38           [4] 00358          staa   W0
 0141 D7 39           [4] 00359          stab   W0+1
And here we want the inverse of that:

Code: Select all

                          00360 ; 00020     W0 := S1 xor $FF00;
 0143 D6 36           [3] 00361          ldab   S1
 0145 86 7F           [2] 00362          ldaa   #$7F      ; Thanks Mike B!
 0147 11              [2] 00363          cba
 0148 89 80           [2] 00364          adca   #$80
 014A 97 38           [4] 00365          staa   W0
 014C D7 39           [4] 00366          stab   W0+1
Post Reply