Specifications
BASIC Stamp II
Parallax, Inc. • BASIC Stamp Programming Manual 1.8 • Page 267
2
Demo Program
This program is a guessing game that generates a random number in a
subroutine called pickAnumber. It is written to stop after three guesses.
To see a common bug associated with Gosub, delete or comment out
the line beginning with Stop after the For/Next loop. This means that
after the loop is finished, the program will wander into the
pickAnumber subroutine. When the Return at the end executes, the
program will go back to the last known return address in the middle of
the For/Next loop. This will cause the program to execute endlessly.
Make sure that your programs can’t accidentally execute subroutines!
rounds var nib ' Number of reps.
numGen var word ' Random-number generator (must
be 16 bits).
myNum var nib ' Random number, 1-10.
for rounds = 1 to 3 ' Go three rounds.
debug cls,"Pick a number from 1 to 10",cr
GOSUB pickAnumber ' Get a random number, 1-10.
pause 2000 ' Dramatic pause.
debug "My number was: ", dec myNum ' Show the number.
pause 2000 ' Another pause.
next
stop ' When done, stop execution here.
' Random-number subroutine. A subroutine is just a piece of code
' with the Return instruction at the end. The proper way to use
' a subroutine is to enter it through a Gosub instruction. If
' you don't, the Return instruction won't have the correct
' return address, and your program will have a bug!
pickAnumber:
random numGen ' Stir up the bits of numGen.
myNum = numGen/6550 min 1 ' Scale to fit 1-10 range.
' Go back to the 1st instruction
return ' after the GOSUB that got us
here.










