-
Notifications
You must be signed in to change notification settings - Fork 0
BASCOMP programming examples
Since you are here, you will probably know the basics about BASIC. BASCOMP implements a subset of statements and functions found in GW-BASIC, MS QuickBASIC and other dialects.
In a nutshell: Many easy traditional and QB BASIC programs should "just work", while larger and/or more specific programs will need smaller or larger modifications. If you are looking for some inspiration, this page is designed to give you bits and pieces of code showing what BASCOMP can do.
This little programm will execute the ROT13 algorithm on a given text string.
10 REM ===============================================================
20 REM ROT13.BAS
30 REM ROT13 ENCODER/DECODER FOR BASCOMP
40 REM ===============================================================
50 CLS
60 PRINT "ROT13 TEST"
70 PRINT "ENTER TEXT:"
80 INPUT T$
90 R$ = ""
100 I = 1
110 IF I > LEN(T$) THEN GOTO 300
120 C$ = MID$(T$, I, 1)
130 A = ASC(C$)
140 REM ----- A..Z -----
150 IF A >= 65 AND A <= 90 THEN GOTO 200
160 REM ----- a..z -----
170 IF A >= 97 AND A <= 122 THEN GOTO 240
180 R$ = R$ + C$
190 GOTO 280
200 A = A + 13
210 IF A > 90 THEN A = A - 26
220 R$ = R$ + CHR$(A)
230 GOTO 280
240 A = A + 13
250 IF A > 122 THEN A = A - 26
260 R$ = R$ + CHR$(A)
270 GOTO 280
280 I = I + 1
290 GOTO 110
300 PRINT "ROT13:"
310 PRINT R$
320 PRINT "APPLYING ROT13 AGAIN RETURNS THE ORIGINAL TEXT."
330 ENDInitially, BASCOMP programs were restricted to using just the 64k of memory they initially got as COM files. This has changed. The BASCOMP runtime offers some capabilities to request more memory from DOS during program execution. The following program shows what can be done right now.
10 REM ===============================================================
20 REM ARRAYS.BAS
30 REM Demonstrates large numeric arrays and external string arrays.
40 REM BASCOMP, version 2.1 and up.
50 REM ===============================================================
60 CLS
70 PRINT "LARGE ARRAY AND STRING ARRAY DEMO"
80 PRINT
90 REM ---------------------------------------------------------------
100 REM Large numeric array demo.
110 REM Arrays with numerical elements are handled by BASCOMP as EXTERNAL
120 REM integer arrays when they exceed the size of 127 elements.
130 REM ---------------------------------------------------------------
140 DIM BIG(6000): REM Integer arrays up to 32767 elements are supported.
150 REM Store values at the beginning, middle, and end of the array.
160 BIG(1) = 100
170 BIG(3000) = 200
180 BIG(6000) = 300
190 REM Read values back, including one value through a variable index.
200 I = 3000
210 SUM = BIG(1) + BIG(I) + BIG(6000)
220 PRINT "BIG(1) = "; BIG(1)
230 PRINT "BIG(3000) = "; BIG(I)
240 PRINT "BIG(6000) = "; BIG(6000)
250 PRINT "SUM = "; SUM
260 PRINT
270 REM ---------------------------------------------------------------
280 REM External string array demo.
290 REM BASCOMP 2.1 supports direct string array syntax:
300 REM DIM name$(count) AS STRING * maxLength
310 REM This creates an external string array without explicit XALLOC.
320 REM ---------------------------------------------------------------
330 DIM TXT$(4) AS STRING * 20
331 REM ---------------------------------------------------------------
332 REM This provides four strings at 20 characters each.
336 REM The runtime still needs an additional byte per string for length.
337 REM External string arrays are currently limited to 64k per array.
338 REM So calculate: max_number_of_strings = 64k / (length + 1)
339 REM ---------------------------------------------------------------
350 TXT$(1) = "ALPHA"
360 TXT$(2) = "BETA"
370 TXT$(3) = "GAMMA"
380 TXT$(4) = "DELTA"
390 PRINT "STRING ARRAY CONTENTS:"
400 PRINT "1: "; TXT$(1)
410 PRINT "2: "; TXT$(2)
420 PRINT "3: "; TXT$(3)
430 PRINT "4: "; TXT$(4)
440 PRINT
450 REM Use a variable index for string array access.
460 I = 3
470 S$ = TXT$(I)
480 PRINT "ENTRY AT VARIABLE INDEX "; I; " IS "; S$
490 REM Overwrite one entry and read it again.
500 TXT$(2) = "UPDATED"
510 PRINT "ENTRY 2 AFTER UPDATE: "; TXT$(2)
520 REM No explicit XFREE is needed with the direct DIM syntax.
540 PRINT
550 PRINT "DEMO COMPLETE."
560 ENDThis program will need BASCOMP 2.0 and a suitable 256 colors display (VGA), showing of the graphical abilities (and some math functions) of BASCOMP. The execution speed will depend on your (physical/emulated) cpu.
10 REM ============================================
20 REM BASCOMP GRAPHICS DEMO: FULL 320x200 MANDELBROT
30 REM Requires SCREEN and PSET support
40 REM SCREEN 13 = 320x200, 256 colors
50 REM ESC aborts after each completed row
60 REM Fixed-point integer arithmetic, scale S=48
70 REM ============================================
80 SCREEN 13
90 S = 48: LIM = 192: MAXIT = 32
100 FOR PY = 0 TO 199
110 CY = (PY - 100) * 12 / 25
120 FOR PX = 0 TO 319
130 CX = PX * 9 / 20 - 96
140 ZX = 0: ZY = 0: I = 0
150 IF ABS(ZX) > 95 THEN GOTO 270
160 IF ABS(ZY) > 95 THEN GOTO 270
170 ZX2 = ZX * ZX / S
180 ZY2 = ZY * ZY / S
190 IF ZX2 + ZY2 > LIM THEN GOTO 270
200 IF I >= MAXIT THEN GOTO 270
210 NZX = ZX2 - ZY2 + CX
220 NZY = 2 * ZX * ZY / S + CY
230 ZX = NZX
240 ZY = NZY
250 I = I + 1
260 GOTO 150
270 CLR = I + 1
280 IF I < MAXIT THEN GOTO 300
290 CLR = 0
300 PSET PX, PY, CLR
310 NEXT PX
320 K$ = INKEY$
330 IF K$ = CHR$(27) THEN GOTO 380
340 NEXT PY
350 REM Wait for any key before returning to text mode
360 K$ = INKEY$
370 IF K$ = "" THEN GOTO 360
380 SCREEN 0
390 COLOR 7, 0
400 CLS
410 PRINT "DONE."
420 ENDThis will need BASCOMP 2.2 and demonstrate the constant folding abilities. BASCOMP will calculate the expression internally and output a very short binary file containing just the result (299), not the calculation.
100 A = ((&H7FFF - 32767) + (((181 * 181) \ 181) - 181) + (((180 * 180) / 180) - 180) + ((((32767 \ 181) * 181) - 32761)) + (((+32767 - (32760 \ 8)) - (181 * 158)) + ((12345 - 12300) * (9 - 4))))
110 PRINT A