Shop OBEX P1 Docs P2 Docs Learn Events
PropBasic: Signed Two's complement? — Parallax Forums

PropBasic: Signed Two's complement?

skynuggetskynugget Posts: 172
edited 2010-08-27 05:39 in Propeller 1
hi all, I'm trying to get the prop to talk to the roomba. i have it talking great, but i am having trouble reading the WORDS that are signed.

the roomba spits out two bytes per word highbyte first with a dec range of -32768 to 32768

i have come up with the following, but it only gives me the absolute value. does anyone have any ideas how i could make this work?
        RDBYTE rooBuffer(12), vDist
	vDist = vDist << 8 
	RDBYTE rooBuffer(13), temp
	vDist = vDist | temp

im using jons tx_dec routing to debug the output to the terminal. ive attached the rest of my code. thanks in advance!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-08-26 17:02
    You need to sign extend the 16 bit value. The easiest way to do this is to use 2 shifts like this:
    vDist = vDist << 16 ' This positions the sign bit at bit 31
    vDist = vDist ~> 16 ' This moves it back, but fills the upper word with the sign
    
  • AribaAriba Posts: 2,690
    edited 2010-08-26 17:56
    PropBasic does not know the ~> operator.
    You can replace it with:
    \  sar vDist,#16
    
    but there is better alternative:
    RDSBYTE rooBuffer(12), vDist
      vDist = vDist << 8
      RDBYTE rooBuffer(13), temp
      vDist = vDist | temp
    
    This does the sign extend while reading the first byte

    Andy
  • skynuggetskynugget Posts: 172
    edited 2010-08-26 18:10
    thanks guys,

    i tried both of those solutions before to no avail. either with the pasm instruction, or starting off with RDSBYTE results in absolute value.
  • kuronekokuroneko Posts: 3,623
    edited 2010-08-26 18:23
    skynugget wrote: »
    i tried both of those solutions before to no avail. either with the pasm instruction, or starting off with RDSBYTE results in absolute value.

    If I use RDSBYTE I get extra code added which deals with sign-extension. Do you mind attaching the generated SPIN code?
    mov           __temp1,#12                  '	RDSBYTE rooBuffer(12), vDist
                      add           __temp1,#rooBuffer_ofs      
                      add           __temp1,par                 
                      rdbyte        vDist,__temp1               
                      shl           vDist,#24                   
                      sar           vDist,#24                   
    
                                                                 '	vDist = vDist << 8 
                      shl           vDist,#8
    
  • skynuggetskynugget Posts: 172
    edited 2010-08-26 18:32
    yep i get that code in my spin as well kuroneko. (attached). could it be something in the TX_DEC routine causing me trouble?
  • kuronekokuroneko Posts: 3,623
    edited 2010-08-26 18:43
    skynugget wrote: »
    yep i get that code in my spin as well kuroneko. (attached). could it be something in the TX_DEC routine causing me trouble?

    Possibly, what values do you get when you display both byte values separately (without sign extension)? Preferably for a negative word.
  • BeanBean Posts: 8,129
    edited 2010-08-26 19:26
    Try this code:

    RDBYTE rooBuffer(12), vDist, temp
    vDist = vDist << 8
    vDist = vDist | temp
    vDist = vDist << 16
    \ SAR vDist,#16


    If the bytes would be in the other order you would just use RDSWORD.

    Bean
  • skynuggetskynugget Posts: 172
    edited 2010-08-27 05:39
    hey all, taking a fresh look at it this morning i realized i was testing the variable wrong. The roomba only reports -dist if it does it under its own power, not by pushing it backwards as i was doing, doh! (im pretty sure ive had this revelation before, and forgotten)
    it turns out the code works and im a bone head! thanks everyone!
Sign In or Register to comment.