Howto nibble swap

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've looked, but couldn't find the answer for my problem.

Swap the nibbles of a byte / register.
Is that possible in C todo in 1 instruction through a macro or so?

Execution speed is of the essence :roll:

MY MICROCONTROLLER CAN BEAT THE HELL OUT OF YOUR MICROCONTROLLER /ATMEL

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your best bet would probably create a C macro that implements some inline assembly using the SWAP opcode.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tricksy
very 'tricksy'

MY MICROCONTROLLER CAN BEAT THE HELL OUT OF YOUR MICROCONTROLLER /ATMEL

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm pretty sure I've seen GCC generate a SWAP instruction in response
to some combination of 4-bit shifts. It wasn't in time-critical code, so my "inquiry"
was limited to "Gee, isn't that cute?".

Ultimately, though, if you Absolutely Need the compiler to Always generate a
certain instruction (sequence), you need, as EW says, to use (inline) assembly.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MaxK wrote:
tricksy
very 'tricksy'

If you look in the "Inline Asm" section of the avr-libc user manual, it not only is a good tutorial on the subject, it gives you an example using the SWAP instruction.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, its really getting time to look again in the avr libc manual,

I was sortof keeping myself busy with an interpreter / bytecode interpreter. And found it odd that my switch case statement was 'way' (15% ish )slower then a dirty if/elseif list. I did use a continue statement in the switchcase and in the if/elseif but that only a very limited effect speedwise.

Then I started actually reading the lss file to see what gcc had come up with... seems it already was using swaps.
Sometimes I should really take more care into account of asking something. :oops:

un important stuff, just to see why I asked the nibbly question in the first place:

But I didnt understand why it could use 77 cpu cycles to fetch a 16 bit instruction and decode/run it,, especially if it was the 1st to 3rd in the list..
So i was flaming my switch case list till I noticed my power hungry 'Instruction-fetch' routine...
It was almost 10 to 20 times longer then one instruction itself!

Ok, simple fix my instruction grabbing :D

MY MICROCONTROLLER CAN BEAT THE HELL OUT OF YOUR MICROCONTROLLER /ATMEL