Shop OBEX P1 Docs P2 Docs Learn Events
Can this be done in fewer instructions? — Parallax Forums

Can this be done in fewer instructions?

JonnyMacJonnyMac Posts: 9,378
edited 2025-07-12 18:24 in PASM/Spin (P1)

Reviewing old P1 laser tag code I decided to freshen it up a bit am move some code from Spin to PASM -- more as an exercise than anything. In laser tag we can have coincident signals arriving which get logically OR'd by the sensor. For this reason we measure each pulse width to ensure it's +/- 10% of what it should be.

With SIRCS-style bits there is a leader pulse (2.4ms), a "1" bit (1.2ms), and a "0" bit (0.6ms). That the 1 and 0 bits are even divisions of the leader pulse make things easy. This is what I've done. For you super PASM wonks, would you have arrived at the 10% value differently?

                        mov       t2, sbittix                   ' make copy of start bit ticks
                        shr       t2, #5                        ' t2 = 1/32
                        mov       t3, t2                        ' t3 = 1/32
                        shl       t3, #1                        ' t3 = 2/32
                        add       t2, t3                        ' t2 = 3/32 (~1/10)

Comments

  • JonnyMacJonnyMac Posts: 9,378

    Funny, just the act of posting and looking at the code in the forums made me think this might be [very marginally] better.

                            mov       t2, sbittix                   ' make copy
                            mov       t3, sbtittx                   ' make copy
                            shr       t2, #4                        ' t2 = 2/32
                            shr       t3, #5                        ' t3 = 1/32
                            add       t2, t3                        ' t2 = 3/32 (~1/10)
    
  • Christof Eb.Christof Eb. Posts: 1,345
    edited 2025-07-12 18:58

    Deleted P1

  • evanhevanh Posts: 16,585

    You could make the assumption your gear is well within the spec and go with a tighter 1/16 instead. That would get you down to a move and shift.

  • ersmithersmith Posts: 6,184

    Is the 10% figure more or less arbitrary? It sounds like it is. in which case why not use 12.5% (1/8) as the threshold?

  • JonnyMacJonnyMac Posts: 9,378

    10% seems to be what's agreed upon for SIRCS bits. That said, this is my laser tag code and I can do what I want. This was just a bit of a Saturday exercise

  • ersmithersmith Posts: 6,184
    edited 2025-07-13 12:15

    For the numbers you're using you should have quite a bit of headroom, so you could do something like:

            mov       t2, sbittix        ' make copy
            add       t2, sbittix        ' t2 = 2 * sbittix
            add       t2, sbittix        ' t2 = 3 * sbittix
            shr       t2, #5             ' t2 = (3/32) * sbittix
    

    which would save an instruction and a scratch register.

    The repeated adds seem a bit inelegant, but they're just as fast as shift+add combinations for doing multiply by 3.

  • evanhevanh Posts: 16,585

    There we go. An optimisation of what was already fast. Saturday accomplished. :)

  • JonnyMacJonnyMac Posts: 9,378

    Absolutely. Thank you, Eric. Sometimes I just want to see a different way of accomplishing something, and I appreciate the feedback.

Sign In or Register to comment.