閱讀137 返回首頁    go 阿裏雲 go 技術社區[雲棲]


網絡子係統9_ip校驗和計算

//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
}

最後更新:2017-04-03 15:22:09

  上一篇:go 核心編程隨筆2
  下一篇:go poj 1002 487-3279