Have I found a FOR ... NEXT loop Simulation bug?

AllyCat

Senior Member
Hi,

Often, I try to minimise the number of variables used in my code, particularly Subroutines and General Purpose code, because PICaxe Basic doesn't support "Local" variables. So I was recently looking to re-use the variables within a For ... Next loop.

I suspected that it would be legitimate to re-use the starting value and indeed the following code works as expected. Of course, changing the loop variable (accidentally) within the loop is a well-known "gotcha", but is "permitted" and occasionally useful (but arguably, poor programming) and just used here for demonstration.

Code:
   b1 = 0
   b2 = 20
   for b1 = b1 to b2
      sertxd (#b1," ")
      inc b1
   next

; SIMULATOR RESULT =  0 2 4 6 8 10 12 14 16 18 20
But then I wondered about b2; is the loop "set up" at the start and b2 could then be re-used for another purpose? I suspected not, but tried this code in the simulator (PE5 and PE6):

Code:
   b1 = 0
   b2 = 20
   for b1 = b1 to b2
      sertxd (#b2," ")
      dec b2
   next

; SIMULATOR RESULT = 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Sadly, a "real" PICaxe doesn't behave the same way, so I haven't found the "spare" byte variable that I was looking for. :(

Cheers, Alan.
 

hippy

Technical Support
Staff member
Seems you have found an issue which will need to be investigated.
 

AllyCat

Senior Member
Hi,

They're only in PE6 and it appears only for the X2-series.

So a double-whammy for me; I'm generally writing my code for 08M2s and find PE6 intolerably slow on my netbook (and not too good on my rather ancient desktop either). :(

The particular code I was investigating is a fast string-transfer routine which might be used throughout the program (hence the aim to minimise the registers used). I quite like hippy's idea in a recent thread of using bptr, but I provisionally had that reserved for fast data handling within the interrupt routine.

Cheers, Alan.
 

lbenson

Senior Member
>hippy's idea in a recent thread of using bptr

I trust you're using the system variables, s_w1 through s_w6--see Manual 2?
 

inglewoodpete

Senior Member
When developing large programs for the PICAXE I find it is best to structure your program into function blocks (FBs). Data is transferred into a FB via an area of RAM or scratchpad. (Almost) all variables can be used within the FB and, on completion of the FB's function, the results are posted into an area of RAM/SPad. The next function block is then called as appropriate.

Depending on the project, you may need to use a range of variables for the main loop and the remainder over and over in functional blocks. You can assign several symbols to a single byte or word register, so the variable's use can be quite different in each function block.

I have used this method many times in large PICAXE projects.
 

AllyCat

Senior Member
Hi and thanks for the comments,

I trust you're using the system variables, s_w1 through s_w6--see Manual 2?
Yes indeed, and also s_w0 since I haven't yet found a use for multitasking (another opportunity for us to go OT :) ). I did try, but the complications of what the oscillator frequency actually was, were just too great (eg. when attempting to use HSER..). ;)

However,, the s_wx variables are "only" words that (AFAIK) can't be split into separate bytes, or bit-addressed.

My general target has been writing small "modules" suitable for the "code snippetts" section of the forum, which have a sufficiently small "footprint" that they don't have too much impact on the overall Program that uses them. For example my "double-word" divison routine uses only one byte in addition to the three "input/output" words (numerator, divisor, result and remainder) which can indeed all be s_wx registers (although my later, more compact, version does require a bit-addressable word).

As for assigning multiple names to a register variable (which indeed I have done occasionally), it does seem a rather easy way to produce "buggy" code; to the extent that generally now, I'm retaining the original "bx" and "wx" names for my multiply-used variables.

Cheers, Alan.
 
Top