Disassembly Windows htonl () function

Source: Internet
Author: User
Tags htons

Because I write a network program in the system kernel sometimes need to call htons htonl such functions to convert, but because the kernel can only call the C runtime Library, the other API can not be called. I have also been exposed to a little assembly, never to learn. Read the old code to learn the way the first few chapters, as their own anti-compilation test, the results of their own really anti-out, for people who know the assembly is very easy indeed.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ULONG myHtonl(ULONG i) {     ULONG eax,edx,ecx;     eax = i;     edx = i;     edx = edx << 16;     eax = eax & 0x0ff00;     eax = eax | edx;     edx = i;     edx = edx & 0x0ff0000;     ecx = i >> 16;     edx = edx | ecx;     eax = eax << 8;     edx = edx >> 8;     eax = eax | edx;     return eax; }

Variables are used to register the name, so as to avoid confusion. I feel this code is not a bit of a problem, how he did not judge his system is not small end on the conversion, if their own computer is high-end how to do?

I looked at the back of a person on the Internet to write analog code.

http://wxxweb.blog.163.com/blog/static/13512690020103145256909/

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 8 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 ypedef unsigned short int uint16;typedef unsigned long int uint32;  // 短整型大小端互换#define BigLittleSwap16(A)  ((((uint16)(A) & 0xff00) >> 8) | \                                                 (((uint16)(A) & 0x00ff) << 8)) // 长整型大小端互换#define BigLittleSwap32(A)  ((((uint32)(A) & 0xff000000) >> 24) | \                                                  (((uint32)(A) & 0x00ff0000) >> 8) | \                                                 (((uint32)(A) & 0x0000ff00) << 8) | \                                                  (((uint32)(A) & 0x000000ff) << 24)) // 本机大端返回1,小端返回0int checkCPUendian(){       union{              unsigned long int i;              unsigned char s[4];       }c;         c.i = 0x12345678;       return (0x12 == c.s[0]);} // 模拟htonl函数,本机字节序转网络字节序unsigned long int HtoNl(unsigned long int h){       // 若本机为大端,与网络字节序同,直接返回       // 若本机为小端,转换成大端再返回       return checkCPUendian() ? h : BigLittleSwap32(h);} // 模拟ntohl函数,网络字节序转本机字节序 unsigned long int NtoHl(unsigned long int n){       // 若本机为大端,与网络字节序同,直接返回       // 若本机为小端,网络数据转换成小端再返回       return checkCPUendian() ? n : BigLittleSwap32(n);} // 模拟htons函数,本机字节序转网络字节序unsigned short int HtoNs(unsigned short int h){       // 若本机为大端,与网络字节序同,直接返回        // 若本机为小端,转换成大端再返回       return checkCPUendian() ? h : BigLittleSwap16(h);} // 模拟ntohs函数,网络字节序转本机字节序unsigned short int NtoHs(unsigned short int n){       // 若本机为大端,与网络字节序同,直接返回       // 若本机为小端,网络数据转换成小端再返回       return checkCPUendian() ? n : BigLittleSwap16(n);}

Looking at his own added judgment, I estimate that the Windows system is estimated to be replaced by a macro honts these functions, if the small-end definition of the conversion content, if the high-end definition of the direct return can be. That's the way Windows likes to write code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.