This lesson takes an in-depth look at declarations, and their many and varied forms. The following are examples of some types of declarations, some of which you've seen before, some of which you haven't.
Example of declarations: |
#include <asmlib.h> // contains definitions of ROM calls and system variables as well as additional ASM routines |
#include we've already seen before; not much more to say about it. STRING we've seen before as well. Now for FUNCTION! This keyword, as you might have guessed, declares the prototype for a function. The syntax goes like this:
FUNCTION <function_name> [ ARG <number_of_arguments> ]
or, for experts:
FUNCTION <function_name> [ ARG <number_of_arguments> | ROMCALL <romcall_name> ARG <arg1> <arg2> ... ]
Normally, unless you want to make a function consisting solely of a ROM call, all that you need to do to declare a function's prototype is to put in the function's name and how many arguments will be passed to it. The actual recipients of the values you passed to the function will be defined in the function's code block.
It is generally discouraged to create a function consisting solely of a ROM call; however, the feature is supported, and there might be occasions when such a form is quite useful. That said, try to stick to regular functions.
INT is another type of variable declaration. It simply declares a 2-byte variable, with the option of initializing it to a value. For the ASM programmers, all that is happening here is that a label is being created corresponding to the variable's name and a .DW statement is put after it. As a complement to INT is BYTE, which declares a 1-byte variable, again with the option of initializing it to a value.
I should put in a little note here about variable scope. That is, variables can be localized to particular functions or subroutines, such that a variable of the same name could be used in many functions, but the variable storage sites would be completely separate. C programmers, for one, should be intimately familiar with the concept of variable scope; frankly, I'm not sure if BASIC has this feature. TI-BASIC certainly does not! In any case, BASIC ASM allows for variable scope in a relatively simple way. You see, at the ASM level, a variable is simply a label with a .DB or .DW (or combinations of those) statements following it. To localize a variable, all BZC does is add the name of the function containing the particular variable at the beginning of the variable's label. This makes the variable unique to that function. So, variables defined outside the START...END code block are considered global, and can be accessed by any function or subroutine. However, variables defined inside START...END, or inside functions or even loops for that matter are considered local and can only be accessed by those functions that contain them.
Other types of declarations
Although the preceeding four declaration types will be the ones you use most often, there are still other types of variables you can declare. At the moment, those include blocks of memory, which you can use to create menus and arrays, and sprites (however sprite support is not yet finished in this alpha-release).
Certainly the simplest of these is declaring block of memory, which is done using the MEM keyword. It takes two arguments: a name, so that you can actually access the variable later; and a number, which defines how large a block of contiguous memory you want in bytes, to be located at the end of the program (e.g. MEM memory 6 causes a 6-byte block of memory to be reserved at the end of the current program).
Declaring a menu takes a bit of work. First, you have to allocate storage space to store the pointers to the menu title and options strings; this is simple enough to do with MEM (see above paragraph). Second, the strings that your menu is using must be defined before you start using them in the menu declaration; otherwise, you will surely get a compile error. Finally, you have to actually assign string variables to the menu title and entry(s). This is done with MENUOPTION. Since the whole process is rather confusing, I've taken the liberty of providing an example for you to take a look at. No doubt it will make infinitely more sense that this paragraph.
Menu Example Program: |
#include <asmlib.h> // standard BZC header START menuoption mymenu 0 mtitle // set the menu title; menu title is always the 0th element |
In any case, you can create menus of up to 255 entries (and it actually doesn't take as long as you might think to scroll through them all; this is theTI-OS menu system to the tenth power).
The SPRITE keyword allows the definition of (really, inclusion of) a sprite from another file, but because sprites are not yet available, we will not discuss it here.
In summary, the following is a list of the available declarations in BASIC ASM; simply click on any of them to jump to that declaration's entry on the BASIC ASM Commands page.
BYTE | Declares a 1-byte variable, with optional initialization |
CONST | Declares and initializes a 2-byte constant variable |
FUNCTION | Declares a function |
INT | Declares an integer-type (2-byte) variable, with optional initialization |
MEM | Reserves a block of memory and attaches it to a variable name |
ROMCALL | Declares a ROM call at a certain address |
SPRITE | Creates a sprite in the current program from the specified data file |
STRING | Declares and initializes a constant string variable |
SYSVAR | Declares a system variable |
Now we're ready to take a closer look at Functions, the real meat and potatos of BASIC ASM.