⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 矛与盾的较量(2)——crc原理篇.htm

📁 CRC16的源程序
💻 HTM
📖 第 1 页 / 共 4 页
字号:
                        name=L69><BR><A 
                        name=L70>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;下面赋值给寄存器ebx,以便进行crc32转换:</FONT><BR><A 
                        name=L71>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;EBX是待转换的字符串的首地址:</FONT><BR><A 
                        name=L72>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>lea</FONT> <FONT 
                        color=#ff0000>ebx</FONT><FONT 
                        color=#9932cd><B>,</B></FONT> szBuffer<BR><A 
                        name=L73><BR><A 
                        name=L74>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;进行crc32转换:</FONT><BR><A 
                        name=L75>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>invoke</FONT> arraycrc32<BR><A 
                        name=L76><BR><A 
                        name=L77>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;格式化输出:</FONT><BR><A 
                        name=L78>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>invoke</FONT> wsprintf<FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#ff0000>addr</FONT> szText<FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#ff0000>addr</FONT> szTemplate<FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#ff0000>addr</FONT> szBuffer<FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#ff0000>eax</FONT><BR><A name=L79><BR><A 
                        name=L80>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;好啦,让我们显示结果:</FONT><BR><A 
                        name=L81>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>invoke</FONT> MessageBox<FONT 
                        color=#9932cd><B>,</B></FONT> hWnd<FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#ff0000>addr</FONT> szText<FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#ff0000>addr</FONT> szTitle<FONT 
                        color=#9932cd><B>,</B></FONT> MB_OK<BR><A 
                        name=L82>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#9932cd><B>.</B></FONT><FONT 
                        color=#ff0000>ENDIF</FONT><BR><A 
                        name=L83>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#9932cd><B>.</B></FONT><FONT 
                        color=#ff0000>endif</FONT><BR><A 
                        name=L84>&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#9932cd><B>.</B></FONT><FONT 
                        color=#ff0000>ELSE</FONT><BR><A 
                        name=L85>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>mov</FONT> <FONT 
                        color=#ff0000>eax</FONT><FONT 
                        color=#9932cd><B>,</B></FONT>FALSE<BR><A 
                        name=L86>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>ret</FONT><BR><A 
                        name=L87>&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#9932cd><B>.</B></FONT><FONT 
                        color=#ff0000>ENDIF</FONT><BR><A 
                        name=L88>&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>mov</FONT> <FONT 
                        color=#ff0000>eax</FONT><FONT 
                        color=#9932cd><B>,</B></FONT>TRUE<BR><A 
                        name=L89>&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>ret</FONT><BR><A name=L90>WndProc <FONT 
                        color=#ff0000>endp</FONT><BR><A name=L91><BR><A 
                        name=L92><FONT 
                        color=#238e23>;**********************************************************</FONT><BR><A 
                        name=L93><FONT 
                        color=#238e23>;函数功能:生成CRC-32表</FONT><BR><A 
                        name=L94><FONT 
                        color=#238e23>;**********************************************************</FONT><BR><A 
                        name=L95>init_crc32table&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>proc</FONT><BR><A name=L96><BR><A 
                        name=L97>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;如果用C语言来表示,应该如下:</FONT><BR><A 
                        name=L98>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;</FONT><BR><A 
                        name=L99>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; i 
                        &lt; 256; i++)</FONT><BR><A 
                        name=L100>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;{</FONT><BR><A 
                        name=L101>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crc 
                        = i;</FONT><BR><A 
                        name=L102>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for 
                        (j = 0; j &lt; 8; j++)</FONT><BR><A 
                        name=L103>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</FONT><BR><A 
                        name=L104>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if 
                        (crc &amp; 1)</FONT><BR><A 
                        name=L105>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crc 
                        = (crc &gt;&gt; 1) ^ 0xEDB88320;</FONT><BR><A 
                        name=L106>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else</FONT><BR><A 
                        name=L107>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crc 
                        &gt;&gt;= 1;</FONT><BR><A 
                        name=L108>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT><BR><A 
                        name=L109>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crc32tbl[i] 
                        = crc;</FONT><BR><A 
                        name=L110>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT><BR><A 
                        name=L111>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;</FONT><BR><A 
                        name=L112>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>;呵呵,让我们把上面的语句改成assembly的:</FONT><BR><A 
                        name=L113><BR><A 
                        name=L114>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>mov</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#ff0000>ecx</FONT><FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#802000>256</FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#238e23>; repeat for every DWORD in 
                        table</FONT><BR><A 
                        name=L115>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>mov</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#ff0000>edx</FONT><FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#802000>0EDB88320h</FONT><BR><A name=L116><FONT 
                        color=#802000>$BigLoop</FONT><FONT 
                        color=#3080ca>:</FONT><BR><A 
                        name=L117>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>lea</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#ff0000>eax</FONT><FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#871f78>[</FONT><FONT 
                        color=#ff0000>ecx</FONT><FONT 
                        color=#3080ca>-</FONT><FONT color=#802000>1</FONT><FONT 
                        color=#871f78>]</FONT><BR><A 
                        name=L118>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>push</FONT>&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>ecx</FONT><BR><A 
                        name=L119>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>mov</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#ff0000>ecx</FONT><FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#802000>8</FONT><BR><A name=L120><FONT 
                        color=#802000>$SmallLoop</FONT><FONT 
                        color=#3080ca>:</FONT><BR><A 
                        name=L121>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>shr</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#ff0000>eax</FONT><FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#802000>1</FONT><BR><A 
                        name=L122>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>jnc</FONT>&nbsp;&nbsp;&nbsp;&nbsp; 
                        @F<BR><A 
                        name=L123>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>xor</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#ff0000>eax</FONT><FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#ff0000>edx</FONT><BR><A name=L124>@@<FONT 
                        color=#3080ca>:</FONT><BR><A 
                        name=L125>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>dec</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#ff0000>ecx</FONT><BR><A 
                        name=L126>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>jne</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#802000>$SmallLoop</FONT><BR><A 
                        name=L127>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>pop</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#ff0000>ecx</FONT><BR><A 
                        name=L128>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>mov</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#871f78>[</FONT>crc32tbl<FONT 
                        color=#3080ca>+</FONT><FONT 
                        color=#ff0000>ecx</FONT><FONT 
                        color=#3080ca>*</FONT><FONT color=#802000>4</FONT><FONT 
                        color=#3080ca>-</FONT><FONT color=#802000>4</FONT><FONT 
                        color=#871f78>]</FONT><FONT 
                        color=#9932cd><B>,</B></FONT> <FONT 
                        color=#ff0000>eax</FONT><BR><A 
                        name=L129>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>dec</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#ff0000>ecx</FONT><BR><A 
                        name=L130>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>jne</FONT>&nbsp;&nbsp;&nbsp;&nbsp; <FONT 
                        color=#802000>$BigLoop</FONT><BR><A name=L131><BR><A 
                        name=L132>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>ret</FONT><BR><A 
                        name=L133>init_crc32table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT 
                        color=#ff0000>endp</FONT><BR><A name=L134><BR><A 
                        name=L135><BR><A name=L136><FONT 
                        color=#238e23>;**************************************************************</FONT><BR><A 
                        name=L137><FONT 
                        color=#238e23>;函数功能:计算CRC-32</FONT><BR><A 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -