REV and returning during loops

Jamster

Senior Member
Just some quick questions that passed over me while coding today:

  • How does one go about using the "REV" operator in the X2 parts, no matter what I try in the PE I cant get what I'm excpecting and searching "rev*" in the forum brings up allsorts :confused:.
  • Is it bad practice in PICAXE basic to exit a do : loop with a return like so:
Code:
badloop:
do 
      return
loop
I presume that the PICAXE handles the fact you would have a pointer (pointing to the "do") floating around each time the function was called because it didn't pass the "loop" command to exit.

Just wondering,
Jamster :)
 

eclectic

Moderator
First question.

I may be wrong, but play with

Code:
#rem
From Manual 2.  p.24
REV
The REV (reverse) command reverses the order of the specified number of bits of
a 16 bit number. Therefore to reverse the 8 bits of %10110000 (to %00001101)
the command would be
let b1 = %10110000 REV 8
#endrem

#Picaxe 28X2


b0 = %10000000 ; 128
b1 = b0 REV 8
sertxd (#b1, " ",cr,lf)


b2 = b1 REV 8
sertxd (#b2, cr,lf)

b3 = b2 REV 8


sertxd (#b3, cr,lf)
e
 

Jamster

Senior Member
Thanks ecelectic, I hadn't noticed that section, only saw the brief line on it 2 pages up. That bit makes sense now :)
 

nick12ab

Senior Member
  • Is it bad practice in PICAXE basic to exit a do : loop with a return like so:
Code:
badloop:
do 
      return
loop
You can use the exit command instead.

If this do : loop is part of a gosub then using return in the loop will be acceptable - treat the do as a label and the loop as goto label or if condition = true then label.
 

westaust55

Moderator
While placing a RETURN sighing the loop is possible folks can give alternate solutions if you advise more about the intended program structure.
Is Badloop: a subroutine or within a subroutine?
What follows the LOOP program line?

Maybe a test at the LOOP program line is sufficient rather than IF...THEN RETURN in the middle of the loop structure.
But guessing without more information.
 

geoff07

Senior Member
Best to have a logical flow that humans can understand. So I suggest to exit the loop (any loop) with exit if you dont fall out of the end (you could use e.g. While) and then return from the sub program, if that is where you are. The most understandable code has simple clear control structures that are very obvious when a year or two later you come to rework the code.
 

Jamster

Senior Member
Thanks all :)

@Nick I phrased the question badly, rather I meant would it use up memory. I'm aware what it means but just wondered if the interpreter would remove the pointer to label when you exit the gosub or whether it would just keep it and forget about it. Yes, Exit would be a lot simpler if there was a return statement at the end of the loop but wouldn't work if the loop had a condition at the end which went to code after (although I never knew that command existed so thanks :) ). Presumably if it does operate in exactly the same way as program labels do then it would be a permenant pointer so the answer to the question is that it wouldn't matter if you did that.

@WestAust There is no code to this, this was just a thought I had while programming, it has no relevance to the code. :) I was intending for "Badloop" to be classed as a subroutine and that the return would be to exit from within code in the loop (via an if for instance). I cant give the rest of the code or tell you whats after as its purely hypothetical...

@geoff You're right this isn't the best of coding practices but as I noted it was purely hypothetical, I highly doubt I'll ever need it :)

Interestingly the PE doesn't complain about the code so I might stuff it in a PICAXE and see how it reacts...

Thanks all,
Jamster (I wasn't allowed a smiley here)
 
Top