Hi,
PICaxe Basic doesn't have an operator to exchange the two nibbles within a single Byte and also the resident SWAP (byte exchange) function is rather inefficient - about twice the number of Program bytes and double the execution time of the snippet below.
Firstly, the two nibbles within a Byte can be exchanged in a "one liner" which (in its byte assignment form) uses only about 8 bytes of Program space and executes in around 1500 PIC instruction cycles (i.e. 1.5 ms @ 4 MHz) :
Note that the first version relies on truncating the PICaxe's 16-bit "internal" calculation back to 8 bits, so the result cannot be assigned directly to a Word variable. A Word can of course be assigned from the Byte result, or an "AND $FF" appended as shown in the second example.
---------
Two Bytes (or Words) can be easily swapped via a third variable, or by using PICaxe Basic's SWAP command, but the following doesn't need another variable and almost halves the Program memory and execution time used by the resident command.
The number of Program bytes and the execution time are similar to those of the nibble-swap routine above (and of the "additional variable" version). The nature of the XOR function is such that, for example, the second line can be written as "w2 = w1 XOR w2", but this will run slower and use more Program bytes !
Swapping the two Bytes within a single Word is of course just a "special case" of swapping any two Byte variables as above, but cannot be used with the 8 additional S_Wn "system" variables.
EDIT: Unlikely to be an issue in PICaxe style programs, but worth noting that this "trick" does not work if attempting to exchange a variable (not its value) with itself, e.g. the equivalent of SWAP b1 , b1 .
Cheers, Alan.
PICaxe Basic doesn't have an operator to exchange the two nibbles within a single Byte and also the resident SWAP (byte exchange) function is rather inefficient - about twice the number of Program bytes and double the execution time of the snippet below.
Firstly, the two nibbles within a Byte can be exchanged in a "one liner" which (in its byte assignment form) uses only about 8 bytes of Program space and executes in around 1500 PIC instruction cycles (i.e. 1.5 ms @ 4 MHz) :
Code:
b1 = b1 * 257 / 16 ; Exchange the nibbles within any byte
OR
w1 = b1 * 257 / 16 & 255 ; Exchange the nibbles within the (or low) byte
---------
Two Bytes (or Words) can be easily swapped via a third variable, or by using PICaxe Basic's SWAP command, but the following doesn't need another variable and almost halves the Program memory and execution time used by the resident command.
Code:
; Fast SWAP (i.e. exchange) of two words or bytes, without using an additional variable.
w1 = w1 XOR w2
w2 = w2 XOR w1
w1 = w1 XOR w2
AND
b1 = b1 XOR b2
b2 = b2 XOR b1
b1 = b1 XOR b2
Swapping the two Bytes within a single Word is of course just a "special case" of swapping any two Byte variables as above, but cannot be used with the 8 additional S_Wn "system" variables.
EDIT: Unlikely to be an issue in PICaxe style programs, but worth noting that this "trick" does not work if attempting to exchange a variable (not its value) with itself, e.g. the equivalent of SWAP b1 , b1 .
Cheers, Alan.
Last edited: