MSX Basic tips and tricks
Latest updates:
2005-02-10
Initial version.
This article contains several tips and tricks for the Basic programmer. Note that this is just a start, a stub, I will not actively seek for new stuff to write there (although if I encounter a good tip incidentally I will add it - if I remember to do so). So people who have additional tips are encouraged to send a mail, and I will add them to the article...
- Jumping out of a
FOR
loop without increasing theFOR/NEXT
stack - Speed opportunities in MSX Basic
- Reducing RAM usage
- Konamiman’s NestorPreTer
- Useful pokes
Jumping out of a FOR
loop without increasing the FOR/NEXT
stack
When you have a FOR/NEXT
loop, Basic will place a counter on an internal stack. This counter is removed when the loop ends, however if you jump out of a loop like in the following example, the counter will never be removed. If you repeat this often enough, the stack will overflow and Basic will quit the program with an error:
10 FOR I=0 TO 1000 20 IF STRIG(0) THEN GOTO 40 30 NEXT 40 GOTO 10
This can be avoided by ending the loop manually before jumping away, like this:
20 IF STRIG(0) THEN I=1000:NEXT:GOTO 40
Alternatively, you can place the FOR/NEXT
in a subroutine use RETURN
to exit from it, because any remaining stack will be cleaned up by the RETURN
command. This is in theory actually also a good programming practice, resembling the break
command in many C-style languages.
(contributors: [D-Tail])
Speed opportunities in MSX Basic
There are several little tricks to make Basic run faster. Here are a number of them:
- Use
DEFINT
for the variables where you only use integer values (whole values). This is pretty much the biggest gain in speed you can get. For example, if your program only uses integers, useDEFINT A-Z
. - Sometimes, specifying the type directly on variables (using
%
for integers) is even faster thanDEFINT
. For example,DEFINT I:FOR I=0 TO 9000:NEXT I
takes 533 TIME ticks to complete.FOR I%=0 TO 9000:NEXT I%
takes 509 TIME ticks. In another test however I didn’t observe a difference, so this doesn’t always make a difference. - In a
FOR/NEXT
loop, you can specify the variable withNEXT
, like:FOR I=0 TO 1:NEXT I
. It is however faster to writeFOR I=0 TO 1:NEXT
, without theI
. - If you remove spaces from the sourcecode as much as possible, it will also be faster (and more difficult to read).
- An
IF 1 THEN GOTO 1000
can be abbreviated in two ways:IF 1 THEN 1000
andIF 1 GOTO 1000
. Of the two, the latter is faster.
Speed measuring
Measuring the speed of a Basic routine can be done as follows:
10 TIME=0 20 '... (put code here) 30 PRINT TIME
The unit of the shown result is interrupts, which depending on the display frequency (PAL or NTSC) is either 1/50th or 1/60th of a second. You can find out which frequency your MSX runs on by typing PRINT VDP(10) AND 2
. If the answer is ‘2’, then the frequency is 50Hz, if the answer is ‘0’, then it is 60Hz.
Reducing RAM usage
What follows are a number of methods to decrease the size (and also increase the speed) of an MSX Basic program. Note that many of these things greatly reduce the readibility of your source.
- Remove
REM
and'
comments and remarks. - Remove all spaces, unless it creates ambiguity (i.e. do not remove spaces in
AX OR BX
because Basic will then read the resultingAXORBX
asA XOR BX
). - Make the variable names as small as possible (that is, 1 character).
- If you don’t use
OPEN
, setMAXFILES=0
. - Instead of
DATA
statements, use BIN files loaded withBLOAD
. - Remove arrays you don’t use anymore from memory using the
ERASE
command. - If you use the
CLEAR
command, make sure you allocate the minimum amount of memory necessary for your strings. Similarly, if you create arrays with DIM, make sure you allocate the correct amount. - Declare integer variables as integers using
DEFINT
.
Additional tips for KUN-BASIC or any of its variants (XBasic/NestorBASIC):
- Split up your TURBO blocks, if possible. They are compiled into memory at once, and thus having smaller blocks means less memory needed for the compiled code.
- Check if NestorBASIC functions 55-57 are useful for you (note: I have no clue as to what they do).
- For more tips take a look at NestorTIPS for NestorBASIC on MCCW 93, written by Nestor himself.
(contributors: DarQ, flyguille, NYYRIKKI and manuel)
Konamiman’s NestorPreTer
Konamiman has created a program called NestorPreTer, with which you can get around many of the limitations of MSX Basic. With NestorPreTer, you can for example use labels instead of line numbers, have longer variable names, and create macros. NestorPreTer then converts that into real optimized MSX Basic code.
Useful pokes
The following pokes can be useful to the Basic programmer:
Poke | Function |
---|---|
POKE &HFBB0,1 | Enables you to use the CTRL-SHIFT-CODE-GRAPH key combination to quit a program even if you normally couldn’t. Especially useful for KUN-BASIC programs or programs with an ON STOP GOSUB handler. |
POKE &HFBB1,1 | Disable CTRL-STOP. |
POKE &HF677,&Hxx POKE &HF676,&Hyy + 1 POKE &Hxxyy,0 |
Changes Basic load address to the given value of &Hxxyy (where you can fill in xxyy to e.g. &HC000). The next program that is loaded will run from that address instead of &H8000 (the default). Useful for hybride programming where you want to use the address &H8000-&HBFFF for assembly code or data. A loader could for example check whether these values are set, and if not, set them and reload itself. |
Grauw