Shop OBEX P1 Docs P2 Docs Learn Events
PASM "REV"? — Parallax Forums

PASM "REV"?

Please help me understand what rev does in assembly. I can't make sense of the truth table or the explanation in the manual. I'm studying a quadrature encoder object from the OBEX and I'm stuck because I don't understand what that instruction does.
Thanks.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2017-02-19 05:23
    rev reverses the order of 32 - n lsbs of the destination register, where n is the lower five bits of the source argument -- ether immediate or addressed. The unreversed bits are set to zero. For example, in
            rev     dest,#27
            ...
    dest    long    %0...011000101000001011
    
    dest would be changed to %0...011010

    -Phil
  • Here's an iteration from Tachyon on the same value $12 showing the effects of REV. A value of 0 the bit order is completely reversed but the bits aren't shifted right at all. For a REV of 1 bit the result is the same but it has been shifted right 1 bit. For 16 the result is the "same" except it has been shifted right 16 bits after a bit reversal.
    ( 0005 $2D4E  ok )   0 32 ADO CR I . 4 XTAB $12 I REV .LONG LOOP
    
    0    4800.0000
    1    2400.0000
    2    1200.0000
    3    0900.0000
    4    0480.0000
    5    0240.0000
    6    0120.0000
    7    0090.0000
    8    0048.0000
    9    0024.0000
    10   0012.0000
    11   0009.0000
    12   0004.8000
    13   0002.4000
    14   0001.2000
    15   0000.9000
    16   0000.4800
    17   0000.2400
    18   0000.1200
    19   0000.0900
    20   0000.0480
    21   0000.0240
    22   0000.0120
    23   0000.0090
    24   0000.0048
    25   0000.0024
    26   0000.0012
    27   0000.0009
    28   0000.0004
    29   0000.0002
    30   0000.0001
    31   0000.0000
    ( 0006 $2D4E  ok )   
    
  • Phil and Peter, I'm like a 40 watt incandescent bulb: I can see with it but there's just not enough light. Thanks for your efforts.
    The rev instruction looks like it's stated differentially which I find confusing but I think it's becoming clearer.
    rev     turnneg, #30
    xor     turnneg, #%01
    
    The first line reverses two bits and the second line reverses the sequence order of the quadrature code...I think.
  • lardom wrote: »
    Phil and Peter, I'm like a 40 watt incandescent bulb: I can see with it but there's just not enough light. Thanks for your efforts.
    The rev instruction looks like it's stated differentially which I find confusing but I think it's becoming clearer.
    rev     turnneg, #30
    xor     turnneg, #%01
    
    The first line reverses two bits and the second line reverses the sequence order of the quadrature code...I think.

    Yes and no. The first line does reverse the two bits but the second line only inverts the lsb. Does the invert reverse the sequence order of the quadrature code? Maybe.

    The description of the operation of REV is a bit confusing but knowing how it actually does operate sheds light on the explanation :)

    So if in doubt you know that REV will shift right 30 places leaving only the 2 lsbs which were reversed.

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2017-02-19 17:12
    The first line does reverse the two bits but the second line only inverts the lsb.
    No, the second reverses the 31 lsbs, leaving the msb equal to zero. [See below.]

    -Phil
  • The first line does reverse the two bits but the second line only inverts the lsb.
    No, the second reverses the 31 lsbs, leaving the msb equal to zero.

    -Phil

    I know it's late but xor #1 only inverts the lsb or am I looking at some other code.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2017-02-19 17:13
    Peter,

    Oops! I didn't see the xor, assumed it was another rev! 'Haven't had my morning coffee yet.

    -Phil
  • Peter,

    Oops! I didn't see the xor, assumed it was another rev! 'Haven't had my morning coffee yet.

    -Phil

    Well it's 3 in the morning here, I should get some sleep :)
  • lardomlardom Posts: 1,659
    edited 2017-02-20 19:49
    It makes sense now. This object returns 'direction' for a single quadrature encoder which makes it perfect for learning how quadrature encoding works. I graphed out a 'truth' table and it's pretty clever how the object does this.
    I have a DC geared motor with an integrated quadrature encoder. It has two Hall sensors that read the edge of a disk. It is open collector which is something that made my eyes glaze over when I first got it in 2009.
    I powered the 4-pin encoder connector with 5 volts from a Quickstart board and two wires from the "A" and "B" connections to two LED's. The LED's blinked and I didn't even need to write any code.
    As easy as that was I could not have done that in 2009. Thanks for your help.
  • The rev instruction makes more sense if you understand how its implemented in hardware - the reversal
    is a fixed wired permutation that reverses all the bits in a word, the barrel shifter is then applied to the
    result. Its done this way to share CPU resources (the barrel shifter), so the instruction ends up being
    a little non-intuitive - the argument is the number of bits to shift, not the number of bits reversed.
  • Mark_T, thanks. I looked up "barrel shifter".
    I now consider rev a useful instruction which I'll be using in the future. The author of the object which I'm studying made clever use of rev. My education continues...
Sign In or Register to comment.