5: BASIC Stamp Command Reference – GOSUB
BASIC Stamp Programming Manual 2.0c
•
www.parallaxinc.com
•
Page 131
Demo Program (GOSUB.bas)
' 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 beginning of the program. This will cause the program to execute endlessly. Make
' sure that your programs can't accidentally execute subroutines!
'{$STAMP BS1} 'STAMP directive (specifies a BS1)
SYMBOL Rounds = B2 ' Number of reps.
SYMBOL NumGen = W0 ' Random number holder (must be 16 bits).
SYMBOL MyNum = B3 ' Random number, 1-10.
NumGen = 11500 ' Initialize random "seed"
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: ", #MyNum ' Show the number.
PAUSE 2000 ' Another pause.
NEXT
END ' When done, stop execution here.
' Random-number subroutine. A subroutine is just a piece of code with the RETURN
' instruction at the end. Always make sure your program enters subroutines with a GOSUB.
' If you don't, the RETURN won't have the correct address, and your program will have a bug!
PickANumber:
RANDOM NumGen ' Stir up the bits of NumGen.
DEBUG NumGen
MyNum = NumGen / 6550 MIN 1 ' Scale to fit 1-10 range.
RETURN ' Go back to the 1st instruction
' after the GOSUB that got us here.
Demo Program (GOSUB.bs2)
' 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 beginning of the program. This will cause the program to execute endlessly. Make
' sure that your programs can't accidentally execute subroutines!
'{$STAMP BS2} 'STAMP directive (specifies a BS2)
Rounds VAR NIB ' Number of reps.
NumGen VAR WORD ' Random-number holder (must be 16 bits).
MyNum VAR NIB ' Random number, 1-10.
2
e
2
sx
2
p
2
NOTE: This is written for the BS2
but can be used for the BS2e,
BS2sx and BS2p also. Locate the
proper source code file or modify
the STAMP directive before
downloading to the BS2e, BS2sx or
BS2p.
1
Summary of Contents for BASIC Stamp 1
Page 1: ...BASIC Stamp Programming Manual Version 2 0c...
Page 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...