Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Do the while loop examples in the Maestro User's Guide clean the stack?


#1

There is the following example in Maestro Servo Controller’s User’s Guide:

10 # start with a 10 on the stack
begin
  dup     # copy the number on the stack - the copy will be consumed by WHILE
  while   # jump to the end if the count reaches 0
  8000 1 servo
  500 delay
  4000 1 servo
  500 delay
  1 minus # subtract 1 from the number of times remaining
repeat

What I do not understand here is whether this leaves a zero value staying in the stack after the completion of the countdown from 10 to 0.

If this is not the case and if the stack is all clean after running this code, in which line is this stack value being consumed?

If it is left on the stack, should we add a “drop” at the end of the repeat to prevent a potential stack overflow (to prevent these 0 stack values accumulating later in the code if many while loops are used this way)?


#2

Hello.

A value of 0 is left on the stack after running that script. So, like you suggested, adding drop at the end of that while loop would be useful.

One feature you might find useful is the Step Script button (located at the bottom of the Script tab of the Maestro Control Center), which can be used to incrementally step through each command in the script in order to observe how the stack changes.

-Jon


#3

Thank you!