flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
revolution 19 Aug 2012, 15:51
Can we assume you understand how the CRC works?
http://en.wikipedia.org/wiki/Cyclic_redundancy_check If we don't understand how algorithms work then we can't know where or when our code is failing. |
|||
![]() |
|
zhak 19 Aug 2012, 17:58
revolution wrote: Can we assume you understand how the CRC works? I hope so ![]() Finally, it seems that I managed to build a working CRC-16 algo, which gives me the same results as online calc for CRC-16 (if pre-initialized with 0x0000) and CRC-16 Modbus (if preinitialized with 0xFFFF). Code: crc16: mov si, data mov cx, data_length mov bx, 0 .l1: lodsw xor bx, ax mov dx, 16 .l2: shr bx, 1 jnc short .l3 xor bx, 0xa001 .l3: dec dx jnz short .l2 dec cx jnz short .l1 It is word-aligned since my data message is 512-bytes aligned. However, I do not completely understand the following: Quote:
So, if I calculate CRC for 512 bytes, but I have only 396 bytes of data, and the rest are just zeroes, then I need to invert final CRC value? |
|||
![]() |
|
baldr 21 Aug 2012, 15:41
zhak,
You really should attempt to understand what CRC algorithm calculates, not how. Those 396 bytes, with 116 zeros appended afterwards, represent 4Ki coefficients for polynomial over GF(2). That polynomial is divided (over GF(2)) by some other polynomial (over GF(2), often carefully chosen ;-). The gist of your quote is that if N is divisible by M, then N*K is divisible too, and in this case K == x**116. Post-conditioning (i.e. inversion after calculation) allows chunking: you may calculate CRC for some chunk, then use it as a seed for another CRC calculation (probably for another chunk ;-), provided that two inversions cancel each other. Pre-conditioning serves two purposes: it's crucial for chunking and it detects leading zeros: 0*x**n+a[n-1]*x**(n-1)+...+a[0] has exactly the same remainder as a[n-1]*x**(n-1)+...+a[0], when divided by anything (cf. 1 and 1.(0), as I don't know proper notation for unsignificant leading zeroes). |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.