Shop OBEX P1 Docs P2 Docs Learn Events
Trivia: make this PASM snippet more efficient/elegant — Parallax Forums

Trivia: make this PASM snippet more efficient/elegant

agsags Posts: 386
edited 2013-07-16 15:18 in Propeller 1
In PASM I'd like to stop all cogs other than the one the code is executing in ("me"). I have this:
          mov      idx, #7
          cogid    cog
:loop     cmp      cog, idx      wz
   if_nz  cogstop  idx
          djnz     idx, #:loop
          cmp      cog, #0       wz
   if_nz  cogstop  idx

Surely there's a more elegant way to handle the 7-to-0 index loop without separately testing after exit. I must have some mental block because I don't see it. I see I can index 8-to-1, and subtract 1 in the loop. Or I can not use djnz but roll my own loop (with index 7-to-0) and stop the cog at the current index, then decrement separately (wz) and then "if_nz jmp". But these actually take more instructions to complete.

Offered as an exercise to the motivated reader.

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2013-07-16 14:20
    You don't need a cmp at all. Simply read the cog-ID into a variable and add 1. Repeat 6 times: cogstop and add 1.
    The cogstop will only use the 3 least significant bits.

    Example:
    Code is running in cog 3
    After adding 1 your cogstop will first stop cog 4
    The loop then stops cog 5-7
    After the next add the variable contains 8 which will lead to a cogstop 0 effectively
    So the rest of the loop will then stop cogs 0-2
  • agsags Posts: 386
    edited 2013-07-16 15:18
    Now that's slick. Very nice. I knew there had to be a better way. I still don't think in the way long-time assembly coders do. I was going to ask if you were certain that only the three least significant bits were used. I read the man pages for cog* several times already. Then I read it again to be sure:
    5 Only the lowest 3 bits of Destination In are utilized, so a value of 8 is seen as cog 0.
    I really need to do a better job of reading all those footnotes.

    Thanks for a nice solution.
Sign In or Register to comment.