MSX Assembly Page

MSX Basic tips and tricks

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 the FOR/NEXT stack

When you have a FOR/NEXT loop, Basic will place the FOR location on its internal stack, so that NEXT knows where the loop begins. This location is removed from the stack when the loop ends, however if you jump out of a loop with GOTO like in the following example, the FOR location 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
40 GOTO 10

This can be avoided by ending the loop manually before jumping away, like this:


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:

Speed measuring

Measuring the speed of a Basic routine can be done as follows:

10 TIME=0
20 '... (put code here)

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.

Additional tips for KUN-BASIC or any of its variants (XBasic/NestorBASIC):

(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 &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.