Shop OBEX P1 Docs P2 Docs Learn Events
Repeating negative numbers. — Parallax Forums

Repeating negative numbers.

KyeKye Posts: 2,200
edited 2011-03-16 10:09 in Propeller 1
Quick Question: How many times does this repeat?
 
  repeat -1
    ' statements
 

Comments

  • RaymanRayman Posts: 14,877
    edited 2011-03-15 18:42
    I'd guess 2^32 - 1 ...
  • kuronekokuroneko Posts: 3,623
    edited 2011-03-15 18:53
    I'd guess 2^32 - 1 ...
    Yup, think tjz/djnz.
  • RaymanRayman Posts: 14,877
    edited 2011-03-15 19:02
    Hard to tell with Spin repeat though...
    A long time ago I tried repeat... step -1 and got completely unexpected behavior....
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-03-15 19:11
    I looked at the interpreter source, but didn't see any comments related to non-variable finite loops. But I suspect they're the same as variable-based finite loops, but with an internal variable. So it's probably necessary to examine the byte code produced for a definitive answer. Either that, or set up a counter and wait a few hours for the result.

    In any event, I suspect the short answer is, "One more time than repeat -2." :)

    -Phil
  • KyeKye Posts: 2,200
    edited 2011-03-15 19:22
    Mmm, no quick answer for me...
  • Heater.Heater. Posts: 21,230
    edited 2011-03-16 00:09
    "repeat -n" causes the statements in the following indented block to be executed n times in reverse order.

    As far as I know this is a feature that is unique to Spin:-)
  • kwinnkwinn Posts: 8,697
    edited 2011-03-16 00:22
    I would expect this to depend on how the spin interpreter handles the negative number. Totally dependent on the interpreter program.
  • Martin_HMartin_H Posts: 4,051
    edited 2011-03-16 07:05
    Nyamekye, I never would have thought to try a negative repeat. I'd bet you would be good at computer security if you have an interest in it.
  • KyeKye Posts: 2,200
    edited 2011-03-16 07:57
    Yeah, its just a computer security problem. I want to know if I have to limit the input value or not.

    @heater - your joking right?
  • Heater.Heater. Posts: 21,230
    edited 2011-03-16 08:14
    Kye,
    @heater - your joking right?

    Might be:)
  • lonesocklonesock Posts: 917
    edited 2011-03-16 09:19
    Yep, it behaves exactly like a DJNZ...check out the attached source.

    * An empty repeat loop takes about 224 clocks per pass
    * So, at 80MHz, the counter variable would be changing at a rate of ~357000 per second
    * Monitoring Hub RAM for values that changed at that rate found the counter
    * watching the counter showed that it starts at whatever value you specify, so
    * starting at -1 will keep going negative at that specified rate, terminating one the counter reaches 0
    * at 80MHz, it will take about 3.34 hours to complete an empty "repeat (-1)"

    Jonathan
  • KyeKye Posts: 2,200
    edited 2011-03-16 09:29
    Thank you very much lonesock! This thread will add to the forum's knowledge.

    (Why doesn't parallax document this...)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-03-16 09:50
    Kye wrote:
    Why doesn't parallax document this...
    Probably for the same reason it's taken one of us five years to discover it. They didn't think of it either. :)

    -Phil
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-03-16 10:02
    I think I had assumed the loop would be skipped if the repeat amount was negative. This is good to know.

    So far I know the repeat occurs more than 100,000,000 times (I left a program runing while reading the forums). I think lonesocks answer is better.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-03-16 10:09
    I ran this under SpinSim, and the debug listing is shown below. It loads a -1 on the stack, and first tests for zero. It then executes a djnz until it reaches zero. The stack pointer and the value at the top of the stack are shown in columns 2 and 3. The columns after the "-" are the program counter, bytecodes and the instruction mnemonic. The number after the tjz and djnz instructions is a jump offset.

    Dave
    Cog 0: 002c 00000000 - 0018 34              ldlim1
    Cog 0: 0030 ffffffff - 0019 08 02           tjz 2
    Cog 0: 0030 ffffffff - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffffe - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffffd - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffffc - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffffb - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffffa - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffff9 - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffff8 - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffff7 - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffff6 - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffff5 - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffff4 - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffff3 - 001b 09 7e           djnz -2
    Cog 0: 0030 fffffff2 - 001b 09 7e           djnz -2
    
Sign In or Register to comment.