Running BBC BASIC

Perfect for beginners, BBC BASIC can run on old and new computers. Learn more about using this classic computer language.

Beginner’s All-purpose Symbolic Instruction Code (BASIC), a classic computer language, dates back to 1964. Originally intended for beginners, BASIC was easier to use than the cryptic Fortran, which was popular for solving science and engineering problems. BASIC became extremely successful in the 1980s when every kid who owned a home computer (e.g., the Commodore 64 or the ZX Spectrum) was greeted with a BASIC prompt whenever they turned on their machine.

Still popular today, you’ll find BASIC used as a macro language in many office suites, among other uses. Two popular BASIC dialects were developed by Microsoft and the British Broadcasting Corporation (BBC). After some background information on BASIC, I’ll take a closer look at BBC BASIC, the BBC version.

BASIC Fundamentals

All the early versions of BASIC used line numbers, making it possible to code without using a proper editor; code was simply entered as a line number followed by a command, such as

10 PRINT "Hello world!"

Adding code before or after a specific line meant typing in a line with a lower or higher number. In order to build the classic, infinitely scrolling “Hello, World!” program, the line above needs the following line: 

20 GOTO 10

appended to make the BASIC interpreter jump back to the PRINT statement.

The line numbers in this example have a second purpose: They function as GOTO targets. However, not every BASIC dialect uses line numbers; several BASIC versions have made line numbers optional or eliminated them completely. Instead, labels were introduced as alternative GOTO targets, such as in OpenOffice Basic, the macro language used in LibreOffice. Modern BASIC dialects support object-oriented programming, but traditionally BASIC was a purely procedural language.

Compatibility Issues

BASIC programs written for one machine are often incompatible with BASIC interpreters on a different machine. Some reasons for this incompatibility include:

  • The BASIC dialects are too different, and the program uses unknown keywords.
  • The program uses hardware-specific features (e.g., to access video memory or call ROM routines) that are missing on the current computer.
  • The computers run on different processor architectures.
  • The BASIC program was stored in a binary format not recognized by the current BASIC interpreter.

You can overcome some of these obstacles by porting a BASIC program to a different BASIC interpreter. Often, you can save a program as an ASCII text file by using

SAVE "program.bas", a

Another workaround is to stick with a specific dialect, such as Microsoft BASIC or BBC BASIC. Many of the Microsoft BASIC versions are somewhat compatible. For example, GW-BASIC, which is part of many MS-DOS versions, knows all the commands from MBASIC, the CP/​M version.

BBC BASIC

Started by the BBC and Acorn Computers, BBC BASIC arose from porting an older BASIC version (Acorn BASIC) to the Acorn Proton computer, which was then renamed the BBC Micro and used in the BBC’s Computer Literacy Project. The BBC Micro (Figure 1) was powered by a MOS 6502 running at 2MHz, a predecessor of the MOS 6510 used in the Commodore 64. This new BASIC dialect became BBC BASIC.

Figure 1: The BBC Micro (1981) was the first computer to run BBC BASIC. © public domain

BBC BASIC is a powerful BASIC dialect. You can use functions and procedures with parameters to make your program more structured. Also, BBC BASIC has separate integer and floating-point arithmetic functions that make integer operations faster.

One of its unique features is that BBC BASIC allows inline assembler code. Other BASIC interpreters can also execute machine code routines, but they are typically supplied as long sequences of DATA statements. Figure 2 shows a few of the DATA lines in a program that I wrote in 1988. Figure 3 shows a BBC BASIC program with embedded Intel x86_64 assembler code and its execution: It is a short loop that outputs individual characters via a built-in OSWRCH (OS write character) function. This “Hello, World!” program actually contains assembler code for more than one processor architecture: It can detect the platform and pick the right code, making BBC BASIC programs portable even when they contain assembler statements.

Figure 2: These DATA lines contain hexadecimal numbers that need to be “poked” into memory.
Figure 3: BBC BASIC programs allow embedded assembler code.

BBC BASIC for SDL

Later versions of BBC BASIC let you write code with or without line numbers. One of these modern versions, BBC BASIC for SDL, runs on Windows, Linux, macOS, the Raspberry Pi, and iOS and Android smartphones (however, you cannot write or edit programs on the mobile devices). In addition, a browser version lets you try out the test programs.

On all platforms, BBC BASIC for SDL uses the Simple DirectMedia Layer (SDL) library, making it possible to create interesting graphical applications. Check out the BBCSDL Games page, which has a short list of games, including the beautiful Alien Eliminator (Figure 4) and Forces of Darkness, that you can download and run in the interpreter.

Figure 4: The graphics in Alien Eliminator are very smooth for a BASIC program.

I will only cover the desktop version of BBC BASIC for SDL, which is basically the same for Windows, Linux, and Apple. Note that the Windows version does not provide an installer; instead you can download a ZIP archive (bbcsdl20.zip), unpack it, and run the bbcsdl.exe binary from the new folder bbcsdl20. On macOS, you download a DMG image (BBCBasic.dmg), move the included program to the Applications folder, and try to run it. You’ll need to give it permissions in the System Settings because Apple does not check for malicious software. On Linux, you can download the bbclinux.zip archive and unpack it. Before launching it, make sure that you have the SDL packages installed, as well. On Ubuntu, Linux Mint, or Debian run these commands:

sudo apt‑get install libsdl2‑2.0‑0
sudo apt‑get install libsdl2‑ttf‑2.0‑0
sudo apt‑get install libsdl2‑net‑2.0‑0

Whenever you start BBC BASIC for SDL, you’ll be greeted with a choice between two IDEs: BBCEdit and SDLIDE, which are both BBC BASIC programs. SDLIDE looks like a typical Windows application, so it might be the better option. BBC BASIC programs use a .bbc file extension, and they are binary files. You cannot open the programs in a text editor. All keywords have been converted into tokens. For example PRINT gets encoded as 0xf1 (241), and GOTO becomes 0xe5 (229). Strings and numbers (except line numbers) occur verbatim in the file. Consequently,

10 PRINT "Hello MakerSpace"
20 GOTO 10

becomes the code shown in Listing 1.

Listing 1: String and Number Conversion

$ xxd ‑c8 ‑g1 ~/Downloads/test123.bbc
00000000: 13 0a 00 f1 20 22 48 65  .... "He
00000008: 6c 6c 6f 20 57 6f 72 6c  llo Worl
00000010: 64 22 0d 0a 14 00 e5 20  d".....
00000018: 8d 54 4a 40 0d 00 ff ff  .TJ@....

Some BBC BASIC versions use slightly different token tables than others, resulting in the BASIC keywords being replaced with other keywords as shown in Figure 5. Here, a program written and saved with a recent macOS version of BBC BASIC and then opened in BBC BASIC for MS-DOS interprets the keywords WHILE and ENDWHILE as DELETE and PUT in the DOS version – this makes no sense. The older DOS version does not support WHILE loops and uses the tokens for those other commands.

Figure 5: The older BBC BASIC version does not recognize the WHILE and ENDWHILE keywords.

Special Features

For a general introduction to the BASIC language, see MakerSpace #2. In this article, I’ll instead look at some of the features that you don’t find in every (classic) BASIC dialect.

Functions and procedures allow you to give a BASIC program more structure than with traditional BASIC subroutines that need to be called with GOSUB. It’s helpful that functions and procedures have names (so you don’t need to use a line number), but calling them with parameters is even better. Listing 2 implements a function that calculates the diagonal length in a rectangle with sides a and b, but without a named function.

Listing 2: Using Parameters

10 INPUT "Side a: " a
20 INPUT "Side b: " b
30 x = a : y = b : REM prepare params
40 GOSUB 1000  : REM result in ret
50 PRINT "Diagonal: "; ret
60 END
1000 REM diagonal function
1010 REM parameters: x, y two sides
1020 REM returns diagonal in ret
1030 ret = SQR(x*x+y*y)
1040 RETURN

Because the subroutine expects parameters in variables named x and y in line 1030, you can’t directly pass the variables a and b to the subroutine; you must copy a and b to x and y first. The same is true for the computed result that the function returns: The caller must read the result from the ret variable. Notice also that these variables are all global, which makes it hard to use recursive GOSUB calls.

BBC BASIC lets you define a proper function that takes two arguments and returns the result (Listing 3), just like in C or Java. The underscore character between FN and diag is not necessary; the function name needs to start with FN, but it could also be FNdiag (however, the underscore improves readability).

Listing 3: A Function that Takes Two Arguments

INPUT "Side a: " a
INPUT "Side b: " b
ret = FN_diag(a,b)
PRINT "Diagonal: "; ret
END

REM diagonal function
REM parameters: two sides
REM returns diagonal
DEF FN_diag(x,y)
= SQR(x*x+y*y)

The function in Listing 3 returns a result value via the line starting with =; this also ends the function execution and returns to the caller. You can have more than one = statement in a function and make them conditional – that’s also the normal method for writing a recursive function. For example, the factorial function can be implemented as follows:

DEF FN_fact(n)
IF n = 1 THEN =1 ELSE =n*FN_fact(n‑1)

When you define or call a function, the opening parenthesis needs to follow right after the function name, without a space character.

Working with Files

BBC BASIC lets you open files for reading or writing with the built-in OPENIN and OPENOUT functions. These functions return a file descriptor (a positive number) if successful and 0 otherwise. You can write text to an open file with the PRINT #fd statement like this:

fd = OPENOUT "output.txt"
PRINT #fd, "Test output"
CLOSE #fd

Figure 6 shows an example program that writes logging information both to the terminal and into a logfile. It first builds a log string from the date (available via DATE$), the log source (<basic>), and the procedure parameter, and then it uses a regular PRINT command for displaying the message and a PRINT# command for writing to the file. Note that the PRINT statement lets you concatenate several arguments with a semicolon, which prints them close to each other, but that’s not possible when writing to a file. If you need to integrate numbers, convert them to strings with STR$(n) and add the string with the + operator. The logfile needs to be open, which is handled by the PROC_openlog procedure when the program starts.

Figure 6: Using BBC BASIC to write logging information into the terminal and into a logfile.

Lines are terminated with a \r (carriage return) character (ASCII code 13) which is compatible with neither traditional DOS line breaks (\r\n) nor with Unix/​Linux line breaks (\n). In order to read such a file in the shell, you can use the Unix command sed to replace \r with \n:

sed 's:\r:\n:g' < output.txt

Make Your Own Symbols

BBC Basic supports several text and graphics modes. To check out the modes, use the MODE command with a number argument between 0 and 15. When in graphics mode, all symbols need to be rendered, which lets you change the look of symbols. The following VDU command (which stands for visual display unit) will change the shape of character 128 into a Space Invaders symbol:

VDU 23,128,24,60,126,219,126,36,66,129

In the code above, 23 starts a character definition, 128 is the ASCII code, and the last eight numbers describe the eight lines of an 8x8 pixel symbol. Listing 4 uses a Unix command line and the bc calculator to show you how this is done.

Listing 4: Create a Space Invader’s Symbol

for i in 24 60 126 219 126 36 66 129; \
  do echo ‑e "obase=2; $i" | bc | xargs \
  printf "%8s\n" | tr "01" " X"; done
   XX
  XXXX
 XXXXXX
XX XX XX
 XXXXXX
  X  X
 X    X
X      X

Figure 7 shows what you can do with a modified font – a very simple text-mode game is just a few lines of game logic code away.

Figure 7: Change the system font with a few VDU commands.

Other Resources

If you want to learn more about BBC BASIC, you should start with – pun intended – the basics. You can follow the beginner’s tutorial for BBC BASIC for Windows and then inspect some of the example programs that are part of the package. These programs are not documented, but in many cases function and variable names are sufficiently self-explanatory, making it possible to understand how a program works.

If you find a .bbc file that you cannot load with your BBC BASIC interpreter, try installing an older or newer interpreter or perhaps try opening the file in an emulator. If you can open the file there, save it in ASCII format. This is a bit tricky: You need to use the special *SPOOL command to make the system log all output in a file, then run LIST to display the listing, and call *SPOOL once more, as follows:

*SPOOL "OUTPUT.BAS"
LIST
*SPOOL

You can then inspect the BASIC program. It’s also possible to load it in this form, but unknown BASIC commands (which were the reason for not loading in the first place) will cause syntax errors.

The Internet Archive offers a huge collection of books and articles about BBC BASIC; you can spend days just browsing through the collections. Some of the books require a registration (you can then digitally “borrow” them), while some are free to download and keep as PDF files. If you’re feeling nostalgic, grab one of the old type-in magazines and copy all the program lines by hand. Just like in the past, the question will then be: Will it run?