PBASIC Programming/Labels and GOTO
Computers are only able to perform one action at a time. This means that if we write up a large program with many instructions, that the BasicStamp will execute those instructions one at a time, starting at the top and moving to the bottom. The order in which the BasicStamp executes instructions is called the control flow. There are several tools that we can use to change the control flow, so that it doesnt just start at the top and move to the bottom.
Tokens and the Instruction Pointer
When we compile our program, the computer changes the code that we have written into a series of tokens that the BasicStamp can understand. This process is very complicated, so we won't discuss it any further in this book. When we upload our program to the BasicStamp, all of the tokens from the program are stored, in order, in the BasicStamp's FLASH memory unit. The tokens are stored in an array. A special address variable, called the instruction pointer points at the different elements in the token array, and whatever the instruction pointer points at is the instruction that the BasicStamp will perform at that moment.
We can create special tags called labels that are like addresses or anchors. If we create a label in our code, we can jump to it from other parts of our program by using special instructions called GOTO, GOSUB, or branches called IF / THEN. Labels must have a name, just like a variable. They must:
- Start with a letter
- Contain letters or numbers
- End with a colon ":"
The most simple way to jump to a label that we have defined is through the use of the GOTO command. The GOTO command causes the instruction pointer to point at the label, instead of the next instruction.