checksum

johnlong

Senior Member
Hi all found a little explanation for a checksum online and replicated it
To possible use it for sending data between the 2 Dorji drf1278dm I have to test if there is any coruption
So I wrote a test rig
the thing is that the values that I preload for b4 to b10 alter after passing through the sub routines
I can not understand why as all that occurs is 1 word value holds the total value of them and b20 to b26 should just be an equal to

Code:
;Bytes total 1,151
   ; 1,151 / 256 = 4.496 (round to 4)
   ; 4 x 256 = 1,024
   ; 1,151 - 1,024 = 127 checksum
   ;the checksum to try out fron the net
symbol checksum=w1
symbol total=w2
symbol bytes=w6
symbol rbytes=w15;received byte
symbol rtotal=w16;received
symbol rchecksum=w17
  b4=23:b5=36:b6=78:b7=96:b8=109:b9=15:b10=43;load some value for transmission
   ;would be the transmission via dorji
  do
sertxd("sent data ",#b4," ",#b5," ",#b6," ",#b7," ",#b8," ",#b9," ",#b10,cr,lf)
  bytes=bytes+b4
  bytes=bytes+b5
  bytes=bytes+b6
  bytes=bytes+b7
  bytes=bytes+b8
  bytes=bytes+b9
  bytes=bytes+b10;
  ;bytes=1151
  sertxd("bytes = ",#bytes,cr,lf)
  total=bytes/256
  sertxd("total/256 = ",#total,cr,lf)
  total=total*256
  sertxd("total*256 = ",#total,cr,lf)
  sertxd("bytes = ",#bytes," total = ",#total,cr,lf)
  checksum=bytes-total
  sertxd("checksum = ",#checksum,cr,lf)
  gosub tell
  gosub increase
  pause 2000
  loop


tell: ;recieved transmission
b20=b4:b21=b5:b22=b6:b23=b7:b24=b8:b25=b9:b26=b10
sertxd("received data ",#b20," ",#b21," ",#b22," ",#b23," ",#b24," ",#b25," ",#b26,cr,lf);?? why are value diffrent b20 to b26 sould reflect b4 to b10 values
rbytes=rbytes+b20
  rbytes=rbytes+b21
  rbytes=rbytes+b22
  rbytes=rbytes+b23
  rbytes=rbytes+b24
  rbytes=rbytes+b25
  rbytes=rbytes+b26
  sertxd("rbytes = ",#rbytes,cr,lf)
  rtotal=rbytes/256
  sertxd("rtotal/256 = ",#rtotal,cr,lf)
  rtotal=rtotal*256
  sertxd("rtotal*256 = ",#rtotal,cr,lf)
  sertxd("rbytes = ",#rbytes," rtotal = ",#rtotal,cr,lf)
  rchecksum=rbytes-rtotal
  sertxd("rchecksum = ",#rchecksum,cr,lf)
return
increase:;alter values for transmission
b4=b4+10
b5=b5+10
b6=b6+10
b7=b7+10
b7=b7+19
b8=b8+11
b9=b9+10
b10=b10+10
return
can anyone see any were I have mis understood the program as normal
regards john
 

Buzby

Senior Member
I've not checked your code completley, but the first thing is that b4 is part of W2.

This line writes to b4, but you have defined W2 as 'Total'.

Is this what you want ?

b4=23:b5=36:b6=78:b7=96:b8=109:b9=15:b10=43;load some value for transmission
 

AllyCat

Senior Member
Hi,
Code:
symbol total=w2
...
  do   ....
  bytes=bytes+b4
  total=bytes/256
Yes, w2 and b4 use the same memory location, so they are bound to corrupt each other.

Also, "bytes" and "rbytes" are not initialised (to zero) at the start of the loop / subroutine, so there may be "issues" on subsequent passes.

Cheers, Alan.
 

Aries

New Member
Two thoughts:
(1) your complicated calculation ( - essentially (sum bytes) - INT((sum bytes) / 256) * 256) is just (sum bytes) // 256,
or just (sum bytes) in byte arithmetic, so you can simply sum up the bytes in a byte variable.
(2) You are using w2 for total, so you are over-writing b4 and b5
 

johnlong

Senior Member
Hi All
Thanks all you were right over writing the b4 and b5 as you say altered total to w1 and bytes to w6 cleared up the issue nicely and cleared bytes and rbytes at the bottom of loop
Tried bytes//256 gave a result of total 144 checksum 256 the reverse of what the original produced
 

Aries

New Member
I'm not sure what you mean ...

Tried bytes//256 gave a result of total 144 checksum 256 the reverse of what the original produced
Adding a bit of code after your original addition
Code:
  bytes=bytes+b4
  bytes=bytes+b5
  bytes=bytes+b6
  bytes=bytes+b7
  bytes=bytes+b8
  bytes=bytes+b9
  bytes=bytes+b10;
 
    b36 = b4 + b5 + b6 + b7 + b8 + b9 + b10
    sertxd("byte addition = ",#b36,cr,lf)
    w19 = b4 + b5 + b6 + b7 + b8 + b9 + b10 // 256
    sertxd("modulo 256 addition = ",#w19,cr,lf)
shows that the byte addition in b36 and the modulo 256 addition in w19 both give exactly the same result as your more complex modulo-256 calculation.
 

johnlong

Senior Member
I'm not sure what you mean ...



Adding a bit of code after your original addition
Code:
  bytes=bytes+b4
  bytes=bytes+b5
  bytes=bytes+b6
  bytes=bytes+b7
  bytes=bytes+b8
  bytes=bytes+b9
  bytes=bytes+b10;

    b36 = b4 + b5 + b6 + b7 + b8 + b9 + b10
    sertxd("byte addition = ",#b36,cr,lf)
    w19 = b4 + b5 + b6 + b7 + b8 + b9 + b10 // 256
    sertxd("modulo 256 addition = ",#w19,cr,lf)
shows that the byte addition in b36 and the modulo 256 addition in w19 both give exactly the same result as your more complex modulo-256 calculation.
Thanks Aries like you said a much simpler and more code efficient solution
played with it to add an error to see if it would pick up the error
Code:
symbol bytes=w6
symbol rbytes=w15;recieved byte

main:
  b4=23:b5=36:b6=78:b7=96:b8=109:b9=15:b10=43;load some value for transmission
   ;would be the transmission via dorji
  do 
sertxd("sent data ",#b4," ",#b5," ",#b6," ",#b7," ",#b8," ",#b9," ",#b10,cr,lf)
 
    bytes = b4 + b5 + b6 + b7 + b8 + b9 + b10 // 256
    sertxd("modulo 256 addition = ",#bytes," checksum result",cr,lf)
    inc b19
    if b19=4 then
        b5 =b5+2
        b19=0
        endif
    gosub tell
  gosub increase
  
  pause 2000
  bytes=0
  rbytes=0
  loop 


tell: ;recieved transmission 
 b20=b4:b21=b5:b22=b6:b23=b7:b24=b8:b25=b9:b26=b10
 sertxd("recieved data ",#b20," ",#b21," ",#b22," ",#b23," ",#b24," ",#b25," ",#b26,cr,lf)
  rbytes = b20 + b21 + b22 + b23 + b24 + b25 + b26 // 256
    sertxd("recieved modulo 256 addition = ",#rbytes," rchecksum result",cr,lf)
  if bytes<>rbytes then
      sertxd("error",cr,lf)
      endif
 return
 increase:;alter values for transmission
 b4=b4+10
b5=b5+10
b6=b6+10
b7=b7+10
b7=b7+19
b8=b8+11
b9=b9+10
b10=b10+10
return
 

inglewoodpete

Senior Member
I have calculated checksums in the past by simply adding the data values together into a byte-register. Surely that is the same as summing into a word register and using the lower byte of that word (wX // 256)?
 

hippy

Technical Support
Staff member
I have calculated checksums in the past by simply adding the data values together into a byte-register. Surely that is the same as summing into a word register and using the lower byte of that word (wX // 256)?
That is correct; storing the result into a byte variable will automatically apply the modulo 256, effectively an "& 255"

One of the problems with a simple summation is the checksum is zero if all data is zero and that is generally considered less than optimal.

One option is to end a byte checksum summation with "^ $FF" which inverts, or "^ $FF + 1" which negates.
 

johnlong

Senior Member
Hi I finally see what you mean using the byte variable automatically does the //256 thereby no need for the extra coding and a saving of a variable,
welcome back Hippy good to see you are fine been a concern for alot of folks
regards john
 
Top