"FOR...DOWNTO" command

jims

Senior Member
I just realizes that this command doesn't work with PE6.
FOR variable = start DOWNTO end
{code}
NEXT {variable}
It's still listed under the "Basic Commands"; but not in Manual 2. Why is this? Thank you, JimS
 

Technical

Technical Support
Staff member
Code:
[COLOR=Blue]for [/COLOR][COLOR=Purple]b0 [/COLOR][COLOR=DarkCyan]= [/COLOR][COLOR=Navy]5 [/COLOR][COLOR=Blue]downto [/COLOR][COLOR=Navy]1 step 1[/COLOR]
[COLOR=Blue]next [/COLOR][COLOR=Purple]b0[/COLOR]


[COLOR=Blue]for [/COLOR][COLOR=Purple]b0 [/COLOR][COLOR=DarkCyan]= [/COLOR][COLOR=Navy]5 [/COLOR][COLOR=Blue]to [/COLOR][COLOR=Navy]1 [/COLOR][COLOR=Blue]step [/COLOR][COLOR=DarkCyan]-[/COLOR][COLOR=Navy]1[/COLOR]
[COLOR=Blue]next [/COLOR][COLOR=Purple]b0[/COLOR]
Both above are exactly the same, the downto is just a bit less typing!
 
Last edited:

jims

Senior Member
Technical... This routine shows b0 decrementing from 5,4,3,2,1 in the PE6 simulator, but always shows 5,5,5,etc when routine is loaded into my 18M2 chip and run. Should it run in the 18M2 chip? Thank you, JimS

Code:
[color=Navy]#picaxe [/color][color=Black]18M2[/color]
[color=Blue]pause [/color][color=Navy]1000[/color]
[color=Black]Main:
    [/color][color=Blue]do
     for [/color][color=Purple]b0 [/color][color=DarkCyan]= [/color][color=Navy]5 [/color][color=Blue]downto [/color][color=Navy]1
      [/color][color=Blue]sertxd([/color][color=Black]#[/color][color=Purple]b0[/color][color=Black],[/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)
     pause [/color][color=Navy]1000
    [/color][color=Blue]next [/color][color=Purple]b0
    [/color][color=Blue]loop[/color]
 

jims

Senior Member
Cockpit error... should be this..( for b0 = 5 downto 1 step 1 ). Needed to include the "step" value. JimS
 

westaust55

Moderator
In PE6, the DOWNTO keyword without a "STEP" parameter passes syntax and simulates correctly for me decrementing by a count of 1 each pass.
downto2.jpgDownto1.jpg

I also noted that
Code:
for b0 = 5 to 0 step -1
next b0
uses 1 less byte than
Code:
for b0 = 5 downto 0
next b0
 

Technical

Technical Support
Staff member
There is now a compiler issue logged for downto when used without step - it will simulate correctly but not work on the chip. Adding step corrects it.
 

matchbox

Senior Member
My issue is that the 'DOWNTO STEP 1' function in real time, seems to operate 1/3 slower than the normal 'FOR NEXT' command that counts up.

I am running the two back to back as a count up/count down timer. I am using 1500 cycles on count up to, compared to 900 cycles on count down to.

Can anyone confirm this?
 

hippy

Senior Member
There should not be any difference in execution speed; the compilers convert the FOR-DOWNTO to what the equivalent FOR-TO would be then compile that so the following should all take the exact same time -

For 1 To 1500 Step 1
For 1500 To 1 Step -1
For 1500 DownTo 1 Step 1
 

AllyCat

Senior Member
Hi,

You might get a clue by checking the number of bytes created by the PE. It appears that a raw "up" loop creates 12 bytes, but the "down" loop 14 (whether using step -1 or downto 1). It's probably because the "down" may actually involve a "step up + overflow" (i.e. +255 or +65535) and these numbers use larger tokens than "1". A larger number of program bytes to process by the interpreter may take longer!

Changing to, say, "For ... 101 to 1600 step 1" (and correcting the value afterwards) might help ?

Cheers, Alan.
 

hippy

Senior Member
The memory size of equivalent FOR-TO and FOR-DOWNTO should be the same, and we believe any reported differences are down to the way the compilers calculate how many bytes have been used rather than actual differences in code size. Memory use can differ when 'from', 'to' and 'step' values are different.
 

hippy

Senior Member
If you still have problems then post your code and say which PICAXE you are using and we can check that here.
 

AllyCat

Senior Member
Hi,

Personally, I was using WinAXEpad (on a windows netbook) to employ an "up to date" compiler (but with fast and easy loading) for a "quick and dirty" test. I usually test for/with an 08M2, which does occasionally produce a slightly smaller code size than 14 and 20 pin M2s

Checking again, I see that the real difference in the number of bytes produced is actually the difference betweeen "from 1 to 1500" (typically 12 bytes) and "from 1500 to 1" (14 bytes). Whether the step is -1 or +1 (or not specified and +1 implied) appears to make no difference to the size of code produced. But using the "incorrect" step sign relative to the "from" and "to" values will, of course, produce an "unintended" result.

However, I made the observation only as a possible explanation for the (new in #9) OP's claimed timing discrepancies between "up" and "down" loops. Unfortunately I'm not in a position to easily test a real PICaxe chip at the moment, and of course the simulator is of no use for timing tests.

Cheers, Alan.
 

hippy

Senior Member
A test with a physical 08M2 shows some slight difference in results but that is likely down to code alignment in memory affecting decoding time rather than any TO / DOWNTO differences; 226.2ms high, 225.1ms low, 1.1ms difference, 0.5%.

Code:
#Picaxe 08M2
Do
  Toggle C.2
    For w0 = 1 To 150 Step 1
    Next
  Toggle C.2
    For w0 = 150 DownTo 1 Step 1
    Next
Loop
 

Attachments

AllyCat

Senior Member
Hi,

Thanks hippy. Yes,that's the type of magnitude of timing discrepancy that I would have expected.

In the absence of any example code, I think we have to assume that the reported timing difference (in #9) was due to an error in either the OP's test program code or the timing measurement method.

Cheers, Alan.
 

grim_reaper

Senior Member
Was the compiler bug for DOWNTO ever addressed/corrected? I've just come across the same thing - easily corrected by changing it to the conventional 'Step -1' and now working fine - but this thread is over four years old!
 
Top