SWAPF

Post your requests and ideas on the future development of mikroC.
Post Reply
Author
Message
david68
Posts: 40
Joined: 20 Mar 2008 13:13
Location: Belgium

SWAPF

#1 Post by david68 » 21 Jun 2008 14:37

Hi there,

Wishing a C implementation of the assembly SWAPF instruction.

Useful when dealing with BCD values. For instance, to get the high nibble (= The 4 MSB bits of a byte), in assembly, you'd write:

Code: Select all

swapf  my_value, w   ; Swap the nibbles of "my_value", and store the result to W
andlw 0x0F   ; Filter out only what was the high nibble (which is now the lower one)
movwf  my_high_nibble   Store the result back in a new zone
3 ROM locations, and 3 cycles.

In miKroC, i had to write:
my_high_nibble = (my_value >> 4)

which was compiled in

Code: Select all

;mikroCeval.c,114 :: 		my_high_nibble = (my_value >> 4) ;
$003B	$0828			MOVF	_my_value, 0
$003C	$00A7			MOVWF	_my_high_nibble
$003D	$0CA7			RRF	_my_high_nibble, 1
$003E	$13A7			BCF	_my_high_nibble, 7
$003F	$0CA7			RRF	_my_high_nibble, 1
$0040	$13A7			BCF	_my_high_nibble, 7
$0041	$0CA7			RRF	_my_high_nibble, 1
$0042	$13A7			BCF	_my_high_nibble, 7
$0043	$0CA7			RRF	_my_high_nibble, 1
$0044	$13A7			BCF	_my_high_nibble, 7

That is 10 ROM locations and 10 instruction cycles.

Far from being optimized, despite the level 5 optimizations selected in the options.


So, here's my wish : In the BCD library, it would be useful to implement such a swap function, on the byte level.

david68
Posts: 40
Joined: 20 Mar 2008 13:13
Location: Belgium

#2 Post by david68 » 21 Jun 2008 14:45

As I think of it, another way would be to implement two new functions to get the hi_nibble or lo_nibble directly from the 8-bits short passed as parameter.

For example:

Code: Select all

unsigned short my_value = 0x53        (Hexadecimal !)

hi_nibble (my_value)   would return 0x05  as unsigned short
lo_nibble (my_value)   would return 0x03 as unsigned short
That's another way that could complete the swapf function.

So, this wish becomes now a 3-in-1 wish ! ;-)))

Post Reply

Return to “mikroC Wish List”