Searching \ for 'Checksumming' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/method/errors.htm?key=checksum
Search entire site for: 'Checksumming'.

Truncated match.
PICList Thread
'Checksumming'
1996\03\15@193637 by Jim Main

flavicon
face
I need to attach checksums to strings outgoing, and check checksums attached
to strings received.

The checksum being the sum of all the data '1' s in the bytes of the string.

Is there an easy way to do this??

Jim

1996\03\15@213946 by Scott Dattalo

face
flavicon
face
Jim Main wrote:
>
> I need to attach checksums to strings outgoing, and check checksums attached
> to strings received.
>
> The checksum being the sum of all the data '1' s in the bytes of the string.
>
> Is there an easy way to do this??
>

Jim,

The generic way to do what you want is:

  check_sum = 0;
  for each byte in the string
     check_sum = check_sum + sum of bits in byte

As far as easy... Take a whack at it! I'll just address a few different ways to
count bits.

There are several ways to count the number 1's in a byte. The three routines
shown below are a few examples. The first is the one you typically see in
programs that count bits; every bit is individually checked to see if it is 0
or 1. Everytime you get a 1, increment a counter. You don't want to use this
routine.

The second routine only loops one time for each bit that is set; e.g. 00101000B
only will loop twice. It's based on a clever algorithm that can clear the
right most bit that is set in a byte.

The third one is based on a look up table. Here, the byte is broken into two
nibbles and the look up table contains 16 entries. Each entry corresponds to
the number of bits set in the offset of the table.


buffer0         equ     0x20
bits_counted    equ     0x21
a_byte          equ     0x23

       ORG     0               ;Reset Vector

       MOVF    a_byte,W
       CALL    RR_bitcount


       MOVF    a_byte,W
       CALL    bitcount

       MOVF    a_byte,W
       CALL    look_up_bits

stay    goto    stay

;*****************************************************
;RR_bitcount
;  Brute force shift and count.

RR_bitcount
       MOVWF   buffer0
       CLRF    bits_counted

RR1     CLRC
       RRF     buffer0,F       ;Get LSB and put in the carry
       SKPNC                   ;If LSB (i.e. C) is set
         INCF  bits_counted,F  ;then count it
       MOVF    buffer0,F
       SKPZ
         GOTO    RR1

       RETURN

;*****************************************************
;bitcount
;
; This algorithm loops one time for each bit that is set.
;It's based on an algorithm that deletes the right most
;bit in a byte. If the byte is 'b' then the algorithm is:
;  b = b & (b-1)
; for example if
;         b = xxxx1000
;       b-1 = xxxx0111
; b & (b-1) = xxxx0000   ;The right most bit of b is deleted
;
;This algorithm comes from a book titled "Efficient C", by
;Thomas Plum and Jim Brodie. They credit Kernighan and Ritchie
;for the original insight.
;
;     Inputs:  W - contains the byte whose bits need counting
;    Outputs:  bits_counted - # of bits that were set in W
;Memory Used:  buffer0

bitcount
       MOVWF   buffer0         ;Save a copy
       CLRF    bits_counted    ;

BC1     MOVF    buffer0,W       ;Get the byte. (affects Z too)
       SKPNZ                   ;If no more bits are set
         RETURN                ;  then we're done
       INCF    bits_counted,F  ;Found a bit
       DECF    buffer0,F       ;"b-1"
       ANDWF   buffer0,F       ;b = b & (b-1) Delete right most bit
       GOTO    BC1

;*****************************************************
;look_up_bits
;
; This routine breaks the byte into nibbles and then counts
;the number of bits in each nibble (via a look up table).
;

look_up_bits

       MOVWF   buffer0
       CLRF    bits_counted

       ANDLW   0x0f
       CALL    look_up
       ADDWF   bits_counted,F

       SWAPF   buffer0,W
       ANDLW   0x0f
       CALL    look_up
       ADDWF   bits_counted,F
       RETURN
look_up
       ADDWF   PCL,F

       RETLW   0  ;0
       RETLW   1  ;1
       RETLW   1  ;2  has only one bit set
       RETLW   2  ;3  has two bits set
       RETLW   1  ;4  ...
       RETLW   2  ;5
       RETLW   2  ;6
       RETLW   3  ;7
       RETLW   1  ;8
       RETLW   2  ;9
       RETLW   2  ;a
       RETLW   3  ;b
       RETLW   2  ;c
       RETLW   3  ;d
       RETLW   3  ;e
       RETLW   4  ;f



       END


For random data, the look up table is by far the fastest: 13 cycles.

The number of cycles the "bitcount" routine takes is 5 + 7*number of
bits that are set i.e. between 5 and 61 cycles, or on average about 33
cycles. While this is slower than the look up table, it uses less code
space. A typical compromise.

Anyway, this is part of the answer to your question.


Scott

1996\03\16@001545 by Steve Childress

flavicon
face
Usually, all you do is zero an n bit summation variable then
add to it each 8 bit entity. Treat as unsigned. Ignore roll-over
beyond 2^n. That's it. Doesn't detect errors such as a whole
character of zeros lost, or complementary errors.

----------
From:   Jim Main[SMTP:spam_OUTjmainTakeThisOuTspamCQM.CO.UK]
Sent:   Friday, March 15, 1996 4:36 PM
To:     Multiple recipients of list PICLIST
Subject:        Checksumming

I need to attach checksums to strings outgoing, and check checksums attached
to strings received.

The checksum being the sum of all the data '1' s in the bytes of the string.

Is there an easy way to do this??

Jim



M`P`.````S <#``\`%0`0`#0`!0!#`0$@@ ,`#@```,P'`P`/`!4`#@`I``4`
M-@$!"8 !`"$````P-T$Y1#(X-T$T-T5#1C$Q.30U030T-#4U,S4T,# P, #G
M!@$#D 8`4 0``!(````+`",```````,`)@``````"P`I```````#`#8`````
M`$ `.0! V6G)]Q*[`1X`< `!````$0```%)%.B!#:&5C:W-U;6UI;F<`````
M`@%Q``$````6`````;L2]\EIA]*I"'ZD$<^46D1%4U0`````'@`># $````%
M````4TU44 `````>`!\,`0```! ```!S=&5V96- <F%I;BYO<F<``P`&$$]8
M[R(#``<0WP$``!X`"! !````90```%5354%,3%DL04Q,64]51$])4UI%4D]!
M3DY"251354U-051)3TY605))04),151(14Y!1$143TE414%#2#A"251%3E1)
M5%E44D5!5$%354Y324=.141)1TY/4D523TQ,+4]615(``````@$)$ $```#=
M`@``V0(```\%``!,6D9U^'H[L?\`"@$/`A4"J 7K`H,`4 +R"0(`8V@*P'-E
M=#(W!@`&PP*#,@/%`@!P<D)Q$>)S=&5M`H,S=P+D!Q,"@'T*@ C/"=D[\18/
M,C4U`H *@0VQ"V#@;F<Q,#,44 L*%%&%"_)C`$ @57-U!T"0;'DL(!LA('D(
M8) @9&\@! `@>@209QP0`Y$#H&)I!4 ;`&W)`,!T:0(@('8*P <P80)@92!T
M: GP"H5A7&1D'F <$05 90#0:!P@.!SS"? =D'1Y+KP@5!80'8 ;< 0@=0"!
M[&=N"8 @T$DAP 6P'E!C`V ;,"UO=@20"H5BBF4;P&X?0#)>;B#1.1& ="<$
M(!T0(-!$;[D'D&XG!4 -L!/08Q^A/G(#8!&@'3$?X2%182#T=V@&\&4*A1%R
M`- 3T/D%P&]F'%,$(!6@$\ ;8'L%L06@;0M0$^ @<0K >0TEY2X*A0J+;&DQ
M.((P`M%I+3$T- WPYPS0++,+63$V"J #8"6COBTNUPJ'+8L,,"Y61@-ASCHO
MWBY6#((@2@=P!= !"W%;4TU44#IJ`P# "X! 0U%-+D/@3RY52UTO?S"-!F"/
M`C QOS++,7!I9&$;424ST'(?X3$U&V Q.4 Y-B T.C,ZX%!F335_,(U4;S>_
M,LM-_'5L'9 IL2* !9 %( B0/P(P!" H82P0$\ [0$E#,$Q)4U0[?S:.=6(N
M:B6Q/9\RRT,>@&-K^QU""X!G*M\KXSL@+5<:1?TN5DD<T G@'T,=@ &0'^'_
M$7!&% 0@'V$3P 40&1! D?!U=&=O1H$;82/12L,_2KE*5 F "H5+:4 !96G_
M(O A\$:\)% >4$K&(X%&@7\>8ATR*%(;@E(R.< !D"!X)S$G'3 <( .@4C)B
MOGD3T$"34C-+HRK-24M!KQZ (F$#D1_ <RI =SG0SQ]2' $>< 0`/S]&O#.1
MOT:\1R](/RYE"H45,0!=X ````,`$! ``````P`1$ ````! ``<PX*(K>_<2
DNP% ``@PX*(K>_<2NP$>`#T``0````4```!213H@`````/8U
`
end
Attachment converted: wonderlandtwo:WINMAIL.DAT (????/----) (0000258D)

1996\03\16@180727 by Dan Goodwin

picon face
Hi All,

I am going through the code Scott Dattalo posted and need help. At label
"RR1" is the instruction "CLRC". What does this instuction do?

Thanks
Dan




On Fri, 15 Mar 1996, Scott Dattalo wrote:

{Quote hidden}

different ways to > count bits.
>
> There are several ways to count the number 1's in a byte. The three routines
> shown below are a few examples. The first is the one you typically see in
> programs that count bits; every bit is individually checked to see if it is 0
> or 1. Everytime you get a 1, increment a counter. You don't want to use this
> routine.
>
> The second routine only loops one time for each bit that is set; e.g.
00101000B
{Quote hidden}

1996\03\16@205052 by Michael S. Hagberg

flavicon
face
CLRC is a parallex command for clear carry use  BCF STATUS,C as a replacement.

michael

>Hi All,
>
>I am going through the code Scott Dattalo posted and need help. At label
>"RR1" is the instruction "CLRC". What does this instuction do?
>
>Thanks
>Dan
>

1996\03\18@123657 by Scott Dattalo

face
flavicon
face
Dan Goodwin wrote:

> >I am going through the code Scott Dattalo posted and need help. At label
> >"RR1" is the instruction "CLRC". What does this instuction do?

To which Michael S. Hagberg responded:
>
> CLRC is a parallex command for clear carry use  BCF STATUS,C as a replacement.
        ...parallax

Perhaps. But, it is also supported by Microchip's MPLAB assembler. There are
several
instruction aliases in addition to this one that are supported. If you're
running
MPLAB, navigate through help by

1) clicking on Help
2) clicking on MPASM Help
3) clicking on PIC16CXX Instruction Set ( or whatever instruction set you are
  interested in).

There you will find the instruction set and all of the supported aliases.

Scott

1996\03\19@013815 by Thomas Coonan

flavicon
face
>I need to attach checksums to strings outgoing, and check checksums attached
>to strings received.
For Error Detection purposes, I assume.  You're concerned about bit errors,
error bursts, etc.  (is it a noisey link or a relatively good link?)

>The checksum being the sum of all the data '1' s in the bytes of the string.
You sure?  Folks typically say "checksum" to mean all kinds of things, including
CRC.  Your above method is simple algorithmically, but won't catch all kinds
of burst errors, or even double bit errors spaced out.  Many of us can pass
you CRC algorithms, if you need CRC.

1996\03\19@134905 by Jim Main

flavicon
face
>>I need to attach checksums to strings outgoing, and check checksums attached
>>to strings received.

>For Error Detection purposes, I assume.  You're concerned about bit errors,
>error bursts, etc.  (is it a noisey link or a relatively good link?)

It's a good link - but I have to conform to a given spec to communicate with
a machine.  (The format is Sony 9-pin serial format for professional vtr's)

>>The checksum being the sum of all the data '1' s in the bytes of the string.
>You sure?
No - the book says "The checksum is the sum of the data (D0 to D7) contained
in each data word from the first to the last data word before Checksum."
The checksum is one byte long, so I assumed it would be the sum of all the
data 1's - the data is typically 5 or 6 bytes long.

Apologies for drifting slightly off topic


Jim

More... (looser matching)
- Last day of these posts
- In 1996 , 1997 only
- Today
- New search...