zy w wrote:
hello,
can anyone help me to translate the following function using
mspgcc's inline assembly language.
__asm__ __volatile__()
uint16_t
convert_endian
(
register uint16_t dt
)
{
/$
swpb @dt
$/
return( dt );
}
The 16 bit variable at the location, where dt points to is swapped. Swap
means high byte and low byte are swapped. (little<->big endian order)
Not tested, but the behavior should be equal to:
uint16_t convert_endian(uint16_t dt) {
uint8_t ptr = (uint8_t *)dt; /*make dt a pointer - to the LSByte*/
uint8_t cpy = *ptr;
/**/
*ptr = *(ptr+1);
*(ptr+1) = cpy;
return dt;
}
Obviously this is much slower than using swpb.
Your ASM function could be rewritten as an inline assembly macro (not
tested):
#define convert_endian(val); asm( "swpb @%op \n\t" \
: [op] "=r" (val) : [opsrc] "r" (val) );
Ralf