Tuesday, 7 February 2012

Effective Endian conversion in x86

Instead of Using the bit shift operators, we can use the assembly instruction bswap provided by x86 systems.
I am using the EAX Register for manipulation because it is the same register used for returning value. So i dont need any extra instruction to move my variable to EAX register

// Using the X86 instruction bswap and gcc inline assembly
inline unsigned int ntohl(unsigned int x)
{
asm("bswapl %1" : "=a" (x) : "a"(x));
}
inline unsigned int htonl(unsigned int x)
{
asm("bswapl %1" : "=a" (x) : "a"(x));
}
view raw endian.h hosted with ❤ by GitHub

No comments:

Post a Comment