2
2
e
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.
5: BASIC Stamp Command Reference – GOSUB
Demo Program (GOSUB.bas)
1
' 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}
SYMBOL
Rounds
SYMBOL
NumGen
SYMBOL
MyNum
NumGen = 11500
FOR Rounds = 1 TO 3
DEBUG CLS,"Pick a number from 1 to 10", CR
GOSUB PickANumber
PAUSE 2000
DEBUG "My number was: ", #MyNum
PAUSE 2000
NEXT
END
' 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
DEBUG NumGen
MyNum = NumGen / 6550 MIN 1
RETURN
2
2
Demo Program (GOSUB.bs2)
p
sx
' 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}
Rounds
VAR
NumGen
VAR
MyNum
VAR
= B2
= W0
= B3
NIB
WORD
NIB
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 131
'STAMP directive (specifies a BS1)
' Number of reps.
' Random number holder (must be 16 bits).
' Random number, 1-10.
' Initialize random "seed"
' Go three rounds.
' Get a random number, 1-10.
' Dramatic pause.
' Show the number.
' Another pause.
' When done, stop execution here.
' Stir up the bits of NumGen.
' Scale to fit 1-10 range.
' Go back to the 1st instruction
' after the GOSUB that got us here.
'STAMP directive (specifies a BS2)
' Number of reps.
' Random-number holder (must be 16 bits).
' Random number, 1-10.
Need help?
Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?