Shop OBEX P1 Docs P2 Docs Learn Events
loop x times in pasm — Parallax Forums

loop x times in pasm

bennettdanbennettdan Posts: 614
edited 2011-03-30 23:02 in Propeller 1
Hello how would you go about looping a certain number of time in a pasm loop?

Comments

  • mynet43mynet43 Posts: 644
    edited 2011-03-30 12:48
    Try looking up the djnz command. It's pretty simple. If you need an example, just ask.

    Try it, it's fun!

    Jim
  • JasonDorieJasonDorie Posts: 1,930
    edited 2011-03-30 12:48
      mov LoopCount, #16
    
    :Loop
      ' do something useful here
      djnz LoopCount, #:Loop
    

    djnz = Decrement <variable> and jump if not zero.

    For any loop count greater than 511 you need to generate the value another way, as an immediate can't hold a value that large. Most people just create a variable in the DAT section and set it to the value needed, then copy it out, like this:

      mov LoopCount, NumberOfLoopsToRun
    
    :Loop
      ' do something useful here
      djnz LoopCount, #:Loop
    
    DAT
      NumberOfLoopsToRun   long    1000
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-03-30 12:49
            mov count,#NUM_LOOPS
    loop    ....
            djnz count,#loop
    
  • doggiedocdoggiedoc Posts: 2,245
    edited 2011-03-30 12:49
    I'd use a decrement loop and jump on zero.
  • doggiedocdoggiedoc Posts: 2,245
    edited 2011-03-30 12:52
    doggiedoc wrote: »
    I'd use a decrement loop and jump on zero.

    Uh... I'm a little slower than Dave Hein and JasonDorie. :D
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-03-30 12:57
    Or you could do it the hard way
            mov count,#(NUM_LOOP>>27)
            shl count,#9
            or count,#(NUM_LOOPS>>18)
            shl count,#9
            or count,#(NUM_LOOP>>9)
            shl count,#9
            or count,#NUM_LOOP
    loop    ....
            sub count,#1 wz
      if_nz jmp #loop
    
  • bennettdanbennettdan Posts: 614
    edited 2011-03-30 17:49
    Doogiedoc,Dave Hein and JasonDorie thanks for the tips!
  • JasonDorieJasonDorie Posts: 1,930
    edited 2011-03-30 18:15
    Dave Hein wrote: »
    Or you could do it the hard way
            mov count,#(NUM_LOOP>>27)
            shl count,#9
            or count,#(NUM_LOOPS>>18)
            shl count,#9
            or count,#(NUM_LOOP>>9)
            shl count,#9
            or count,#NUM_LOOP
    loop    ....
            sub count,#1 wz
      if_nz jmp #loop
    

    For clarity, each of the preceding instructions takes a long value in cog memory, so doing it this way works, but it's very wasteful compared to the single MOV instruction and DAT statement version, which is 2 longs total compared to Dave's 7.

    The djnz instruction is also one long, compared to the sub + conditional jmp of Dave's method. I'm quite sure Dave knows all this and is just being cheeky, but I thought it was worth pointing out because someone just starting out isn't likely to know the difference.

    And hence, I have out-cheeky'd Dave.
  • tonyp12tonyp12 Posts: 1,951
    edited 2011-03-30 18:54
    if you want higher than 511, say 1000

    mov counter, #500
    shl counter, #1 ' double the value.
  • AribaAriba Posts: 2,690
    edited 2011-03-30 20:02
    Here is how I load constants > 511 but max 18 bits:
    mov  counter,#1477
      movd counter,#1477>>9
    
    It's very readable and needs not more longs than with a long in a constant pool

    Andy
  • bennettdanbennettdan Posts: 614
    edited 2011-03-30 20:16
    tonyp12 and Andy thanks I actually need to loop 1024 times.
  • kuronekokuroneko Posts: 3,623
    edited 2011-03-30 20:20
    [COLOR="red"]mov  counter,#1477[/COLOR]
      movd counter,#1477>>9
    
    That doesn't even compile. Besides, you get extra runtime as opposed to using a constant pool.
  • AribaAriba Posts: 2,690
    edited 2011-03-30 23:02
    You' re right, it does not compile (I thought PropTool does not test it). So you need to write it like that:
    mov  counter,#1477 & $1FF
      movd counter,#1477 >> 9
    

    Andy
Sign In or Register to comment.