//ip校验和计算使用与体系结构相关的内联汇编 //x86_64版本 //b 1字节 //w 2字节 //l 4字节 //q 8字节 //1010 1010 逻辑左移 -> 0101 010[0] //1010 1010 算数左移 -> 0101 010[0] //1010 1010 逻辑右移 -> [0]101 0101 //1010 1010 算数右移 -> [1]101 0101 1.1 static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl) { unsigned int sum; asm( " movl (%1), %0\n"//将sum = *iph " subl $4, %2\n"//ihl = ihl-4 " jbe 2f\n"//长度为0,跳转到2处 " addl 4(%1), %0\n"//sum += *(iph+4) " adcl 8(%1), %0\n"//sum += *(iph+8) " adcl 12(%1), %0\n"//sum += *(iph + 12) "1: adcl 16(%1), %0\n"//sum += *(iph + 16) " lea 4(%1), %1\n"//iph = iph + 4 " decl %2\n"//ihl -= 1 " jne 1b\n"//如果ihl!=0,跳转到1 " adcl $0, %0\n"//sum = sum+0 " movl %0, %2\n"//ihl = sum " shrl $16, %0\n"//sum逻辑右移16位 " addw %w2, %w0\n"//取ihl低16位加到sum低16位 " adcl $0, %0\n"//sum = sum+0 " notl %0\n"//sum取反 "2:" : "=r" (sum), "=r" (iph), "=r" (ihl)//输出,都使用寄存器保存输出 : "1" (iph), "2" (ihl)//第一个输入为iph,第二个输入为ihl : "memory");//表明内存可能发生变化 return(sum);//返回sum }
时间: 2024-11-05 14:42:03