Table of Contents

Advertisement

2
1
2
e
Table 5.14: GOSUB Quick Facts.

GOSUB

EEPROM
CAN SAVE
(
)
.
PROGRAM
SPACE
5: BASIC Stamp Command Reference – GOSUB
GOSUB
2
2
GOSUB Address
sx
p
Function
Store the address of the next instruction after GOSUB, then go to the point
in the program specified by Address; with the intention of returning to the
stored address.
• Address is a label that specifies where to go.
Quick Facts
Max. GOSUBs
per program
Max. nested
GOSUBs
Explanation
GOSUB is a close relative of GOTO, in fact, its name means, "GO to a
SUBroutine". When a PBASIC program reaches a GOSUB, the program
executes the code beginning at the specified address label. Unlike GOTO,
GOSUB also stores the address of the instruction immediately following
itself. When the program encounters a RETURN command, it interprets it
to mean, "go to the instruction that follows the most recent GOSUB." In
other words, a GOSUB makes the BASIC Stamp do a similar operation as
you do when you see a table or figure reference in this manual; 1) you
remember where you are, 2) you go to the table or figure and read the
information there, and 3) when you've reached the end of it, you "return"
to the place you were reading originally.
GOSUB is mainly used to execute the same piece of code from multiple
locations. If you have, for example, a block of three lines of code that need
to be run from 10 different locations in your entire program you could
simple copy and paste those three lines to each of those 10 locations. This
would amount to a total of 30 lines of repetitive code (and extra space
wasted in the program memory). A better solution is to place those three
lines in a separate routine, complete with it's own label and followed by a
RETURN command, then just use a GOSUB command at each of the 10
locations to access it. This technique can save a lot of program space.
BS1
BS2
BS2e BS2sx BS2p
BS1
16
4
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 129
BS2, BS2e, BS2sx and BS2p
255
4

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?

Table of Contents