Shop OBEX P1 Docs P2 Docs Learn Events
XOR question — Parallax Forums

XOR question

Don MDon M Posts: 1,653
edited 2013-02-14 08:30 in Propeller 1
Using these data bytes as an example:

$01, $0A, $03, $00, $00

How do I code this to XOR these bytes for a checksum answer? I see the checksum on the logic trace is $08 and it figures with my calculator that way but I don't know how to put it into code.

Would it be as simple as chksum := ($01 ^ $0A ^ $03 ^ $00 ^ $00) ?

Comments

  • Don MDon M Posts: 1,653
    edited 2013-02-13 15:07
    Actually that works. Is there a better or more accepted way?
  • Don MDon M Posts: 1,653
    edited 2013-02-13 15:22
    How do I get this chksum calculation to work correctly?
    PUB Demo  | d, i, s, chksum
    
      ser.Start(RX, TX, 0, 9_600)                        ' communicate with 4D display
      term.start(31, 30, %0000, 115_200)                    ' start terminal using PST
      outa[RST]~~
      dira[RST]~~                                           ' Set RST pin as output
       
      
      pause(500)
      term.str(@ver)                                        ' display version     
    
    
    ' Goto Form3
    
      repeat i from 0 to 4
        s := byte[@form3][i]
        ser.tx(s)
        chksum := (chksum ^ i)
      ser.tx(chksum)
      term.hex(chksum, 4)
      
    DAT
    form3   byte  $01, $0A, $03, $00, $00  
    

    It calculates wrong. It shows $04 instead of $08.
  • Don MDon M Posts: 1,653
    edited 2013-02-13 15:24
    Sorry- i figured it out. My mistake.
    PUB Demo  | d, i, s, chksum
    
      ser.Start(RX, TX, 0, 9_600)                        ' communicate with 4D display
      term.start(31, 30, %0000, 115_200)                    ' start terminal using PST
      outa[RST]~~
      dira[RST]~~                                           ' Set RST pin as output
       
      
      pause(500)
      term.str(@ver)                                        ' display version     
    
    
    ' Goto Form3
    
      repeat i from 0 to 4
        s := byte[@form3][i]
        ser.tx(s)
        chksum := (chksum ^ s)
      ser.tx(chksum)
      term.hex(chksum, 4)
      
    
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-13 15:27
    You have to initialize chksum to zero. Global variables (VAR) are initialized to zero and the method return variable (RESULT) is initialized to zero, but the local variables (d, i, s, chksum) are not. They take on whatever was left over in the stack area from before the method (Demo) was called. Sometimes that's zero, but it's not guaranteed.
  • Don MDon M Posts: 1,653
    edited 2013-02-13 16:20
    Ok. Thanks Mike.
  • Mark_TMark_T Posts: 1,981
    edited 2013-02-13 17:35
    While on the subject of XOR I can't resist mentioning the hoary old trick for swapping two variables without using temporary storage:
      a ^= b
      b ^= a
      a ^= b
    
    (though this doesn't need XOR if your machine is 3-address or has a reverse-subtract instruction
      a -= b
      b += a
      a := b - a   ' reverse subtract
    
    (Of course PASM is 2-address and I don't think has a reverse-subtract but I bet there's a way to do this
    without XOR or an extra register - mini challenge)
  • Don MDon M Posts: 1,653
    edited 2013-02-14 06:28
    This works too:
      chksum := 0 
      repeat i from 0 to 70
        s := byte[@text][i]
        ser.tx(s)
        chksum ^= s  
      ser.tx(chksum)
    
  • AribaAriba Posts: 2,690
    edited 2013-02-14 08:30
    And this should also work:
    chksum := 0 
      repeat i from 0 to 70
        s := text[i]
        ser.tx(s)
        chksum ^= s  
      ser.tx(chksum)
    

    Andy
Sign In or Register to comment.