Considerations when Converting PBASIC Programs to FBASIC
Converting programs written in Parallax, Inc. PBASIC is not a trivial
matter. How easy or difficult the conversion is depends a lot upon how
well the PBASIC program was written initially. Even though FBASIC uses
BASIC style key words, the languages are fundamentally different syntactically.
The greatest area of difference is probably flow control. PBASIC uses jumps
and labels to implement logic. FBASIC uses structured blocks to implement
logic. Simple conversions can elect to use FBasic's GOTO and GOSUB directives
and not convert the jumps to blocks. This prevents FBASIC from being able
to check the structural integrity of the program, but this may not be an
issue for PBASIC programs that have proven themselves already.
Conversion breaks into a few basic steps. Each step may be quite involved,
but the process in simple.
- Convert the variable declaration or symbolic assignment parts of the
program.
- Convert the flow control of your program. You may wish to put all of
the PBASIC program into the function "main" of the FBASIC program
and use GOTO and GOSUB rather than breaking the PBASIC program into separate functions and blocks.
- Convert any DATA statements to ALLOCATIONs.
- Convert the syntax of expressions from the mid-fix notation of PBASIC
with L-values to FBasic's pre-fix notation.
- Convert any PBASIC statements to FBASIC function calls.
- Make sure all variables are declared. PBASIC only has global variables,
so your FBASIC program will use the GLOBAL directive to allocate space
for all the variables. PBASIC does not necessarily need to declare variables.
If your PBASIC program does not symbolically define variables, you may
simply create variables of the same name that PBASIC uses by default. For
example:
GLOBAL word w1
ALIAS byte b1 w1 0
ALIAS byte b2 w1 1
GLOBAL word w2
ALIAS byte b3 w2 0
ALIAS byte b4 w3 0
This prevents FBASIC from being able to do type checking and inherently
makes the program harder to read, but works fine for quick conversions.
On the STAMP II a bit type is available. FBASIC does not have a bit type,
so either use a full byte for the flag or use bit masking inside the rest
of your program to test and set just a single bit of a byte or word.
FBASIC has a LONG type which is not available in PBASIC. You may find that
slight re-writes of the program will make it work much better by taking
advantage of new capabilities found in the TICkit.
Flow control is quite different between PBASIC and FBASIC. You must
first create a function called "main" in your FBASIC program.
This is the entry point for the program. Then every IF exp GOTO must be
converted either to a structured IF...ENDIF block or you may elect to use
labels and change the conditional jump to the following:
IF exp
GOTO label
ENDIF
Line numbers are neither needed nor allowed in FBASIC. Use symbolic
labels to mark jump points. If you decide to use FUNCTIONS for subroutines
in FBASIC, be sure to place the functions above the place where the function
is called. This is referred to top-down programming and is required for
the compiler to understand the meaning of the function when it encounters
the reference to it.
PBASIC uses DATA statements to place static data into EEprom. FBASIC
has a much more powerful system for putting data into EEprom. Use the ALLOCATE
directive to create space for the data. The data can then be symbolically referred
to by the allocation name. To place initial data into the allocation,
use the INITIAL directive. In many cases, the purpose for the DATA statement
in PBASIC was to create a string of characters. This can be done in-line
with FBASIC. Simply assign the quoted string to a word value variable.
For example:
=( my_name, "Protean Logic Electronics" )
This expression automatically creates space for the characters of the
string and places the characters into that space in EEprom. After this
expression executes, the variable my_name will have the EEprom address
of the first character of the string.
Convert expressions to FBASIC syntax. FBASIC uses a pre-fix notation
and only functions, no operators. This is a very internally consistent
syntax, but is probably a bit foreign to most programmers. PBASIC uses
an L-value (left value) and an '=' to begin all expressions. In FBASIC,
the "=" begins the expression followed by a '(' and the destination
for the result of the expression (L-value). A comma then separates the
L-value from the rest of the expression. Unlike PBASIC, FBASIC is not limited
to one operation per line. You can build
up complex expressions in FBASIC, just keep track of the parentheses.
Convert any statements in your PBASIC program (Like SERIN) to function
calls in FBASIC (rs_receive or rs_block). This may require several steps,
or it may simplify your PBASIC code. Virtually all of the STAMP functionality
is duplicated in the TICkit, but things may be accomplished a bit differently.
Look for a function which does what the statement did, then make whatever
changes are necessary. The TICkit also has many capabilities the STAMP
does not have like buss I/O or console input and output. So there may be
large parts of the STAMP program which reduce to a simple function call
in FBASIC.
Protean Logic Inc. Copyright 02/07/04
Top of Page