Skip to content
tt1542 edited this page Jun 2, 2026 · 4 revisions

Welcome to the BASCOMP BASIC Compiler wiki!

What's this about?

The BASCOMP compiler has been designed and written in early 2026, supported by experienced programmers and an AI. It is a retro computing tool designed to create COM executable files for DOS-compatible systems like FreeDOS. It is written in BASIC itself and designed to be self-compileable. Thus, the compiler itself has to fit within the 65.280 bytes provided for in the .COM model.

The compiler code follows the established principle of spaghetti code. And its spaghettiness has been optimized for size - so don't expect it to be very readable.

Getting started

In order for BASCOMP to work, you are recommended to extract all its files into one single directory. There is no management of working paths built into the compiler at the moment.

At the very least, the following files need to be present:

  • BASCOMP.COM - the very compiler you will be using
  • HEADER.ASM - header file for the assembly output of BASCOMP
  • RUNTIME.ASM - assembly runtime file. Sections of this will be added to your assembly output file as needed

For recompiling the compiler you will also need:

  • ASMPACK.COM - reduces the code generated by about 30 percent through cleverly optimizing the output of BASCOMP (recommended for all other use cases as well!)
  • TIMEIT.COM - measuring how long it takes to generate your code. Okay, you don't REALLY need this but it is included in the BUILD and BN batch files (see below).

In case you would like to run this on an 8086/8088 machine you should also use:

  • BASM.COM - a special assembler that only knows about statements generated by BASCOMP and generates x86-compatible machine code.

For your convenience, you can also use the following batch files:

  • BUILD.BAT - this will call BASCOMP, ASMPACK and BASM and take care that you will get COM files directly out of your BAS programms. Use it like this: "build program" without extension.
  • BN.BAT - (build native version) this is the script for the compiler recompiling itself. You won't probably need this, but if you like, play around with it.

REALLY getting started

You can try something like this:

10 PRINT "Hello, world!"
20 END

You might then save this in the same folder as BASCOMP, using the name of HW.BAS, for example, and then type:

C:\BASCOMP\> bascomp hw

BASCOMP will then generate HW.ASM. This is NASM compatible source code. In order to generate an executable, either type

C:\BASCOMP\> basm hw

in order to use the BASM special assembler supplied by BASCOMP itself. Or you can use NASM, provided that it is properly installed and can be found using your PATH or otherwise:

C:\BASCOMP\> nasm -o hw.com hw.asm

Having fun

BASCOMP has been built to support a certain spectrum of types of code. So, the following programs all will be valid.

Using QBasic/QuickBASIC compatible mode (will be detected automatically):

PRINT "Hello, world!"
END

Using shorthand codes for PRINT(?) and END(.):

?"Hello, world!"
.

And of course statements can be packed into one line:

?"Hello, world!":.

Since END is an optional statement, you can also use this:

?"Hello, world!"

Feel free to further try out the obfuscated code capabilities of BASCOMP... ;-)

Using the BASCOMP tool chain

Your polite developer recommends always using

C:\BASCOMP\> build progname

so that the different BASCOMP tools will be called in order. In detail, this will involve:

  • BASCOMP generating an ASM file (like PROGNAME.ASM)
  • ASMPACK packing this very file into an optimized version (like PROGNAME.AS2)
  • BASM taking the optimized file and generating a COM file (like PROGNAME.COM)

Recompiling the compiler

The compiler can be recompiled using

C:\BASCOMP\> bn

and you just need to keep one thing in mind: The resulting binary needs to be smaller than 65.281 bytes, or the COM file will not run.

Supported functions and statements

The following list applies to the 2.0 version and will (hopefully) be updated in the future.

  • REM and apostrophe comments (' comment)
  • LET and implicit assignment without LET
  • INPUT var
  • INPUT "prompt", var
  • INPUT var$
  • INPUT "prompt", var$
  • ! var as shorthand for INPUT var
  • LINE INPUT #channel, var$
  • PRINT
  • ? as shorthand for PRINT
  • PRINT #channel, ...
  • IF ... THEN ...
  • IF ... THEN ... ELSE ...
  • multi-line IF ... THEN, ELSEIF ... THEN, ELSE, END IF
  • GOTO
  • GOSUB
  • RETURN
  • FOR ... TO ... [STEP ...] / NEXT, END FOR
  • WHILE ... / WEND
  • DIM
  • OPEN ... FOR INPUT AS #channel
  • OPEN ... FOR OUTPUT AS #channel
  • CLOSE #channel
  • KILL
  • GET #channel, [position,] variable
  • PUT #channel, [position,] variable
  • CLS
  • LOCATE row, column
  • COLOR foreground, background
  • DEF SEG
  • POKE address, value
  • SELECT CASE, CASE, CASE ELSE, END SELECT
  • RANDOMIZE
  • RANDOMIZE TIMER / numeric-expression
  • XSET handle, index, string-expression
  • XFREE handle
  • END / STOP / . as shorthand

Supported Control Structures

IF / ELSE

BASCOMP supports single-line and multi-line IF forms.

Single-line form:

IF A > 10 THEN PRINT "HIGH" ELSE PRINT "LOW"

Multi-line form:

IF A > 10 THEN
  PRINT "HIGH"
ELSEIF A = 10 THEN
  PRINT "TEN"
ELSE
  PRINT "LOW"
END IF

Boolean IF expressions support AND, OR, and NOT in supported comparison contexts.

SELECT CASE

SELECT CASE supports numeric selection logic:

SELECT CASE A
  CASE 1
    PRINT "ONE"
  CASE 2
    PRINT "TWO"
  CASE ELSE
    PRINT "OTHER"
END SELECT

FOR / NEXT / END FOR

Counted loops are supported, including nested loops and optional STEP values.

FOR I = 1 TO 10 STEP 2
  PRINT I
NEXT I

END FOR is also recognized as an explicit loop terminator form.

WHILE / WEND

WHILE A < 10
  A = A + 1
WEND

Subroutines

Subroutines are supported through GOSUB and RETURN.

Supported Data Types

Numeric Variables

Numeric variables store signed 16-bit integer-style values.

String Variables

String identifiers end with $ and use counted string representation in the runtime.

Indexed Numeric Arrays

DIM creates indexed numeric arrays using syntax such as A(I). Small arrays are stored internally; larger arrays can be backed by external DOS memory through the runtime array support.

DIM A(10)
LET A(1) = 5
LET X = A(1)

External String Arrays

External string arrays are available through:

  • XALLOC(count)
  • XALLOC(count, maxLength)
  • XSET handle, index, string-expression
  • XGET$(handle, index)
  • XFREE handle

Supported Expressions

Numeric Expressions

Numeric expressions support:

  • decimal constants and variables
  • hexadecimal constants with &H...
  • numeric array elements such as A(I)
  • parentheses
  • unary + and unary -
  • +, -, *, /, \
  • MOD
  • XOR
  • AND
  • OR
  • exponentiation with ^
  • numeric function calls

String Expressions

String expressions support:

  • string variables
  • string literals
  • string concatenation with +
  • string function calls
  • DATE$, TIME$, INKEY$, and INPUT$

BASCOMP 2.0 continues the expanded internal string-expression handling introduced before version 2.0. The compiler uses a larger internal string context stack and additional temporary string buffers for nested string functions and concatenations.

Supported Comparisons

The following comparison operators are supported in IF expressions: =, <>, <, >, <=, >=.

Comparisons work for both numeric values and supported string expressions, depending on context.

Supported Functions

Numeric and Conversion Functions

  • LEN(string)
    Returns the length of a string.
  • VAL(string)
    Converts a string to a numeric value.
  • ASC(string)
    Returns the character code of the first character.
  • EOF(channel)
    Returns the end-of-file state for a file channel.
  • EXIST(string)
    Tests whether a file exists.
  • INSTR(string, pattern)
    Searches for a substring and returns its position.
  • INSTR(start, string, pattern)
    Searches for a substring starting at a specific position.
  • ABS(number)
    Returns the absolute value.
  • SGN(number)
    Returns -1, 0, or 1 depending on the sign of the value.
  • RND(number)
    Returns a pseudo-random number in the supported runtime range.
  • INT(number)
    Returns the integer value.
  • SQR(number)
    Returns the integer square root.
  • MIN(a, b)
    Returns the smaller value.
  • MAX(a, b)
    Returns the larger value.
  • TIMER()
    Returns the number of seconds since midnight modulo 65536.
  • EXEC(program$, args$)
    Starts a DOS child program and returns 0 on success or a DOS error code.
  • CPUID()
    Detects the CPU class and returns the runtime CPU identifier.
  • PEEK(address)
    Reads a byte from the active DEF SEG segment.
  • XALLOC(count) / XALLOC(count, maxLength)
    Allocates an external string array and returns a handle.

String-Producing Functions

  • CHR$(number)
    Builds a one-character string from a character code.
  • LEFT$(string, count)
    Returns the left part of a string.
  • MID$(string, start, count)
    Returns a substring.
  • RIGHT$(string, count)
    Returns the right part of a string.
  • LTRIM$(string)
    Removes leading spaces.
  • RTRIM$(string)
    Removes trailing spaces.
  • TRIM$(string)
    Removes leading and trailing spaces.
  • UCASE$(string)
    Converts ASCII lowercase letters a to z to uppercase.
  • STR$(number)
    Converts a numeric value to a string.
  • XGET$(handle, index)
    Reads a string from an external string array.
  • DATE$
    Returns the DOS date in mm-dd-yyyy format.
  • TIME$
    Returns the DOS time in hh:mm:ss format.
  • INKEY$
    Returns a non-blocking keyboard input string.
  • INPUT$
    Reads a string from standard input.

File I/O

Text file I/O is supported through:

  • OPEN
  • CLOSE
  • PRINT #
  • LINE INPUT #

Binary file I/O is supported through:

  • GET
  • PUT

File channels 1 to 9 are supported.

OPEN supports:

  • FOR INPUT
  • FOR OUTPUT

KILL deletes a file. EXIST() checks whether a file is present.

The runtime retains buffered byte output for PUT; buffered binary output is flushed when required by handle changes, seek operations, close operations, and mixed text/binary output paths.

Screen Output

Standard text output is supported through PRINT.

Additional screen control is provided by:

  • CLS
  • LOCATE
  • COLOR

When screen output support is active, runtime text output to the console can use BIOS text output with the active COLOR attribute.

Memory Access

BASCOMP 2.0 supports a compact BASIC-style memory access subset:

  • DEF SEG = expression sets the active segment used by PEEK and POKE.
  • DEF SEG resets the active segment to the program segment.
  • PEEK(address) returns the byte at the active segment and offset.
  • POKE address, value writes a byte to the active segment and offset.

Values written by POKE must fit into a byte (0..255).

Date, Time, Keyboard, EXEC, and CPU Support

BASCOMP 2.0 includes runtime support for:

  • DATE$ using DOS date services,
  • TIME$ and TIMER() using DOS time services,
  • INKEY$ using enhanced BIOS keyboard calls where available,
  • EXEC() for launching DOS child programs,
  • CPUID() for CPU-class detection.

Clone this wiki locally