EasyManua.ls Logo

Parallax BASIC Stamp 2e - Gosub

Default Icon
353 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
5: BASIC Stamp Command Reference GOSUB
BASIC Stamp Programming Manual 2.0c www.parallaxinc.com Page 129
GOSUB
BS1 BS2 BS2e BS2sx BS2p
GOSUB Address
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
BS1 BS2, BS2e, BS2sx and BS2p
Max. GOSUBs
per program
16 255
Max. nested
GOSUBs
4 4
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.
1
2
e
2
sx
2
p
2
Table 5.14: GOSUB Quick Facts.
GOSUB CAN SAVE EEPROM
(
PROGRAM) SPACE.

Table of Contents

Related product manuals