No announcement yet.

16 Bit CRC

  • Filter
  • Time
  • Show
Clear All
new posts

  • 16 Bit CRC

    i need a routine to calculate a 16 bit crc. i found the one at
    , but i don't know what to do with it. does it need to be assembled and then $link'd? i have nasm but it gives me an unknown error and i'm not experienced at all in intel assembly. could you please tell me how to use this file. thanks,

  • #2
    It was specifically written for Turbo Assembler. If you'll email me at [email protected], I'll email you the .OBJ and a sample file.


    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Home of the BASIC Gurus


    • #3
      it you can use a 32-bit crc instead, try the code at

      if you must use 16-bit, get the code offered by dave, or try this alternative from my own collection:
      <font face="courier new, courier" size="3"><pre>
      ' filecrc16.bas - include functon for checking open file's crc 16 value
      ' first open the file for binary, and then call filecrc16 with file number
      ' as the passed 'variable'.
      ' (general usage)

      function filecrc16%(byval infile%) local public
      local inbuffsize%, inbuff$, buffseg&, buffadr&, bytesleft&, filedone%, saver%

      inbuffsize% = fre(t$)
      inbuff$ = string$(inbuffsize%, 0)
      buffseg& = strseg(inbuff$)
      buffadr& = strptr(inbuff$)
      bytesleft& = lof(infile%)
      if bytesleft& = 0 then function = 0 : exit function
      seek infile%, 0
      gosub loadinbuff

      gosub crcstring
      gosub loadinbuff
      loop until istrue(filedone%)

      function = saver%
      exit function


      if istrue(bytesleft&) then
      if bytesleft& < inbuffsize% then
      inbuffsize% = bytesleft&
      inbuff$ = string$(inbuffsize%, 0)
      buffseg& = strseg(inbuff$)
      buffadr& = strptr(inbuff$)
      end if
      get infile%,,inbuff$
      decr bytesleft&, inbuffsize%
      filedone% = -1
      end if

      ! pushf
      ! push ds
      ! push si
      ! push di
      ! mov ds, buffseg&
      ! mov si, buffadr&
      ! mov cx, inbuffsize%
      ! cld
      ! mov dx, saver%
      ! lodsb
      ! sub ah, ah
      ! xchg ah, al
      ! xor dx, ax
      ! mov di, 8
      ! mov bx, dx
      ! shl dx, 1
      ! and bx, &h8000
      ! jz nextbit
      ! xor dx, &h1021
      ! dec di
      ! jnz bitloop
      ! dec cx
      ! jnz byteloop
      ! mov saver%, dx
      ! pop di
      ! pop si
      ! pop ds
      ! popf
      end function

      powerbasic support
      ( mailto:[email protected][email protected]</a> )
      mailto:[email protected]


      • #4

        If you prefer to calculate crc in Basic, try subroutine, I often use:

        SUB CrcCalc (b AS BYTE)

        ' Calculate next CRC in global variable ' Crc'

        DIM i AS WORD
        DIM c AS LONG

        c = (Crc XOR b * 256) AND &HFF00
        FOR i = 0 TO 7
        IF (c AND &H8000) > 0 THEN
        c = 2 * (c AND &H7FFF) XOR &H1021
        c = 2 * (c AND &H7FFF)
        END IF
        NEXT i

        Crc = (Crc AND 255) * 256 XOR c

        END SUB ' CrcCalc




        • #5
          If you must use 16-bit, get the code offered by Dave, or try this
          alternative from my own collection:...
          Your own collection consists of lots of things, including this 
          routine, which I wrote for an article that appeared in BASICally 
          Speaking in August 1994.
          I do the same thing--grab code and stash it away, then forget about 
          its origin unless I make the effort to leave some type of source 
          identifier with the code.  This also is not the first time that 
          something I wrote has suddenly popped up on screen, utterly familiar 
          to me but now anonymous. 
          Internet postings such as those that appear right here increase the 
          likelihood that years from now, we'll all see even more evidence of 
          our work floating in the common background. 
          Is this good or bad?

          -- Greg
          [email protected]


          • #6
            Footnote: apparently the code I posted above is courtesy of Greg Turgeon!

            Yes Greg, I often find this exact situation occuring. If I recall, I came across this code in Compuserve or Fidonet, but I'm not 100% sure.

            I think it's both good and bad...!

            PowerBASIC Support
            ( mailto:[email protected][email protected]</A> )
            mailto:[email protected]


            • #7
              Looking at Cezary' s routine up here, i' m not able to understand what does it calculate the CRC of, while e.g. in the Lance' s posted code it is highlighted that it' s the CRC of the file open as #InFile%. Maybe it' s the same for Cezary' s code, i.e. it' s the CRC of the file open as #b, but i can' t see this in the code. I would be interested in implementing Cezary' s code if it refers to a file.

              Davide Vecchi
              [email protected]


              • #8
                Thanks Mr. Zielinski, Edmonds and Turgeon. I used both your routines. I'm communicating between an 8086 based handheld and a Windows 95 desktop computer so that's why I needed a 16 bit CRC. I used Gregs code on the handheld (It took .05 seconds to do a CRC on a 2048 byte string vs. 37.5 seconds with Cezary's code) and Cezary's on the desktop where time was not an issue. In reply to David's question, you feed the Cezary code a byte at a time from the string you want the CRC done on. It keeps track of the total CRC in a global WORD variable CRC. Both routines return the same result if you change the saver variable in Gregs code to a word instead of integer. I altered Greg's code to accept a string instead of a file handle since I'm doing serial comms with it. Thank you again, everyone.


                • #9
                  You guys may want to be very careful about what you do with code you find on the internet. I'm involved in a dispute at the moment over just such an incident where I found some code on the internet and then reposted it.

                  I meant no malice toward the author, but he doesn't see it that way and is doing his level best to make my life miserable (this is three years after the event occured).

                  The lesson learned is... If you find code on the internet (or anywhere) that doesn't belong to you, make sure that it "explicitly states" that it is free for use, or try to obtain permission from the author to use it. Whatever you do, don't repost it without getting a signed document and blood sample from the author.

                  Which brings up an interesting point... If you have posted source code on this web BBS owned by PowerBASIC, Inc. then you are giving any reader of this web BBS permission to use that code without restriction. Period. If you disagree with that policy, please delete your code. This forum is for the "OPEN" exchange of ideas, code and the promotion of both the PowerBASIC products and BASIC programming.

                  Thanks for your cooperation!


                  PowerBASIC Support
                  mailto:[email protected][email protected]</A>

                  Home of the BASIC Gurus


                  • #10
                    ps. Please don't ask about who is involved in my aforementioned dispute. I will not be naming names. My only point was to bring to everyone's attention the potential for problems when you assume too much about what you find on the internet.


                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>

                    Home of the BASIC Gurus


                    • #11
                      Dave ..

                      I try very hard to not ever break the copyright rules, as do you.
                      There is one more thing that really deserves note here about the
                      PowerBASIC stuff itself!

                      Correct me if I'm wrong, but, per the best way of viewing it as I
                      can make of it all, even the PowerBASIC example code that's included
                      in the package you buy, has a Copyright notice. Because that notice
                      has no actual release defined in either it or the manuals, from a
                      strict interpretation standpoint, we aren't even free to use the
                      examples which contain that notice!

                      Of course, I don't think that PowerBASIC really intends to do this.
                      It's just that many of don't really stop to think about what the
                      real nuts and bolts of the Copyright laws are until we are faced
                      with a cuss fight over 'em.

                      If you really get down to it, what PowerBASIC, in my humble opinion,
                      needs to do is to also use a release for use clause that is keyed
                      to the valid licensee of the tools!

                      Having to scrap several thousand disks, CDROM's or whatever over a
                      little deal like this ain't fun, and isn't cheap!

                      Actually, there are examples that were furnished, segments of which
                      I would like to use! However, I honestly believe I cannot do so
                      with the notice that is posted with the sample code..

                      Care to comment on that or add a public post in the FAQ segment of
                      the Forum which will clear that point?

                      Mike Luther
                      [email protected]
                      Mike Luther
                      [email protected]


                      • #12

                        If you read the PowerBASIC license agreement, you'll see that you are free to use and distribute the PowerBASIC code.

                        The only exception, that I am aware of is the license agreement for PowerTree. You can not redistribute code that gives away the necessary information for accessing the PowerTree DLLs.


                        PowerBASIC Support
                        mailto:[email protected][email protected]</A>

                        Home of the BASIC Gurus


                        • #13
                          Yes, Dave .. I understand what you are trying to say, however,
                          the actual license text that is used, doesn't exactly, even to a
                          layman, say the freedom to use them really exists!

                          From the license agreement:

                          ... accompanying PowerBASIC Inc. documentation. Your use of any
                          of the sample or demonstration programs provided with this
                          product are governed by the notices and restrictions of the
                          respective author or copyright holder.
                          Let's look at the specific example in the PB3.5, MOUSUNIT.BAS.
                          Copyright notices, when affixed to a specific source file, to me,
                          as a layman, mean exactly what they say:

                          ' Mouse routines for PowerBASIC
                          ' Copyright (c) 1995-97 by PowerBASIC, Inc. All Rights Reserved.
                          That, as far as my training is concerned, means EXACTLY what it
                          says. All rights are reserved by PowerBASIC, Inc. There is absolutely
                          no language that I, a layman, can see, in the license agreement that
                          one must click to install PB 3.5, which can be used to contravene the
                          express 2Copyright notice in the sample!

                          Yes, the work product from the PB 3.5 compiler is distributable!, with
                          some specific caveates. However, the source in the example, from a pure
                          technical standpoint, to me, isn't useable! It very explicitly says
                          that "All Rights Reserved."; that's the controlling requirement!

                          I think, PowerBASIC intends, that, "Use of this code is restricted
                          to a licensed user of the PowerBASIC compiler products", or something
                          such as that. Heck, I'm no lawyer, and PB surely isn't thinking of
                          this as such. But .. sight .. the words mean exactly what they say.

                          If, as you post, after spending three years grubbing around after such
                          things, I would think you would sort of feel like I read it right!
                          Heck, I'm only trying to help..

                          Mike Luther
                          [email protected]
                          Mike Luther
                          [email protected]


                          • #14
                            I'm not a lawyer Mike. And I'm certainly not going to waste any time arguing with you. I'll bring your concerns to the attention of our legal department. It could be that we need to be more explicit.


                            PowerBASIC Support
                            mailto:[email protected][email protected]</A>

                            Home of the BASIC Gurus