User Tools

Site Tools


sd-8516_user_s_guide

This is an old revision of the document!


SD-8516 User's Guide

CHAPTER 3: BEGINNING STELLAR BASIC PROGRAMMING

CHAPTER 3: BEGINNING STELLAR BASIC PROGRAMMING

Welcome to Stellar BASIC on your SD-8516! This is where the real fun begins. Stellar BASIC lets you talk directly to your machine and make it do exactly what you want. The thrill of typing in your first program and hitting RUN – the magic of watching the the screen come alive – using PETSCII graphics to play golf – the mystery of YOHO – and so much more – it's all right here, for you to discover!

Stellar BASIC is a lean, fast descendant of the famous Tiny BASIC from 1975 (the one that fit in under 4K and inspired so many early microcomputers). It's designed as a powerful yet easy to learn programming language for the everyman. There are no fancy tricks, just a straightforward list of commands. It is perfect for games, math, and little adventures, yet powerful enough to help you do taxes!

PRINT

Getting Started – The PRINT Command

The easiest way to make your SD-8516 say hello is with PRINT. You can also use the ? key as shorthand.

Type this right now (press ENTER after the line):

  ? "HELLO WORLD"

The computer should immediately display:

  HELLO WORLD

Now try:

  ? 42 + 8

It prints 50. You can do math right away, no program needed. This is called direct mode, directly entering commands on the terminal.

GOTO

Your First Program – The Never-Ending Message

Let's write a real program. Type NEW and press ENTER to clear any old stuff.

Now carefully type these lines (press ENTER after each one):

  10 ? "HELLO WORLD"
  20 GOTO 10

Type RUN and press ENTER.

Watch the screen fill up with “HELLO WORLD” scrolling forever! To stop the program, press the BREAK or STOP key (ESC on modern keyboards).

PROGRAMS

Line Numbers and How Programs Work

Every line in a program starts with a line number (any whole number from 1 to around 65000). The computer runs lines in order of those numbers, smallest to largest.

You don't have to type lines in order, the computer will sort them for you automatically. Try this:

  20 ? "LINE 20 COMES FIRST ANYWAY!"
  10 ? "BUT LINE 10 RUNS FIRST"

RUN

LIST

To see your program again, type LIST.

To erase just one line, type its number and press ENTER (e.g., 20 [ENTER] deletes line 20).

If you type NEW it will delete the current program. Make sure you SAVE “NAME” your program first.

LET (VARIABLES)

Variables – Remembering Things

Variables are like little memory boxes. In Stellar BASIC we use single letters only: A, B, Z, etc. (Version 1 does not support strings or floating point numbers).

Try this in direct mode:

  LET A = 25
  
  ? A

It prints 25.

Now a tiny counting program:

  10 LET A = 1
  20 ? A
  30 LET A = A + 1
  40 IF A <= 10 THEN GOTO 20
  50 ? "BLAST OFF!"

RUN

This program counts from 1 to 10. Can you make it count

INPUT

The INPUT Command – Asking Questions

Now let's make the computer ask questions.

Type NEW, then:

10 PRINT "WHAT IS YOUR GUESS (1-100)";
20 INPUT E
30 PRINT "YOU ENTERED: "; E

RUN

Type a number and press ENTER. Notice the semicolon (;) keeps things on the same line—no extra ? prompt needed.

IF-THEN

Making Decisions with IF…THEN

The real power comes from decisions.

Add these lines to make a simple checker:

  40 IF E = 42 THEN GOTO 70
  50 IF E <> 42 THEN ? "TRY AGAIN!"
  60 GOTO 10
  70 PRINT "YOU FOUND THE ANSWER!"

RUN and try a guess until you hit 42. (42 is a reference to the Hitchhiker's Guide – a great book).

Your First Game

A Classic: Number Guessing Game

Here's a complete small game you can type in; it's the first program ever written in Stellar BASIC.

It uses everything we've covered: PRINT/?, variables, INPUT, IF-THEN, GOTO, and introducing a new function: RAND() for random numbers.

After typingNEW, then enter this program:

10 ? ""
20 ? "NUMBER GUESSING GAME"
30 ? "GUESS MY NUMBER 1-100!"
40 LET A = 1 
50 LET B = 100
60 LET D = RAND( B - A + 1 ) + A - 1   : REM SECRET NUMBER!
70 LET F = 0
80 LET F = F + 1
90 PRINT ""
100 PRINT "ROUND";F
110 PRINT "LOW:";A;" HIGH:";B
120 INPUT "YOUR GUESS:"; E
130 IF E > B THEN PRINT "TOO LARGE!" : GOTO 90
140 IF E < A THEN PRINT "TOO SMALL!" : GOTO 90
150 IF E < D THEN PRINT "HIGHER!" : LET A = E : GOTO 80
160 IF E > D THEN PRINT "LOWER!" : LET B = E : GOTO 80
170 PRINT "YOU GOT IT!"
180 PRINT "SCORE:";101-F
190 PRINT "PLAY AGAIN? ( 1=Y / 0=N ) ";
200 INPUT G
210 IF G = 1 THEN GOTO 50
220 PRINT "BYE FOR NOW!"

RUN and play! (Note: RAND(N) gives 0 to N-1, so we adjust +A-1 to get the range.)

This is the spirit of 8-bit BASIC—type it in, play it, tweak it, make it yours. Just like the old days when you'd stay up late copying listings from magazines.

IF-THEN EXAMPLE

Here is an example program:

  10 LET A = 5  
  20 PRINT A  
  30 LET A = A - 1  
  40 IF A > 0 THEN GOTO 20  
  50 PRINT "GOAL!"

Next, type RUN. You will see a countdowns to 1, then “GOAL!”

GOSUB and RETURN

A useful feature of BASIC is GOSUB and RETURN. Think of a subroutine as a little helper routine you can call from anywhere. GOSUB jumps to it (and remembers where you came from), and RETURN sends you right back to the next line.

This saves tons of typing—perfect when you want the same message or calculation in multiple spots.

Here is an example of a GOSUB helper function:

  10 REM GOSUB SQUARE DEMO
  20 LET A = 2
  30 GOSUB 1000
  40 LET A = 3
  50 GOSUB 1000
  60 LET A = 4
  70 GOSUB 1000
  80 LET A = 5
  90 GOSUB 1000
  100 GOTO 9000
  9000 REM END
  1000 REM THIS HELPER FUNCTION WILL PRINT THE SQUARE OF A
  1010 PRINT A * A
  1020 RETURN

After you enter this program and type RUN, you will see the result 4, 9, 16, 25. As you can see, every time the program calls GOSUB 1000,* it runs the code at LINE NO 1000 and then RETURNs to continue in the main program.

You can nest up to 8 GOSUB calls in one program.

Improved Number Guessing Game with GOSUB

Let's level up the guessing game. Now we use GOSUB for separate “display instructions” and “show result” routines—cleaner code, easier to tweak.

NEW then type:

10 ? “” 20 GOSUB 900 : REM SHOW INSTRUCTIONS 30 A=1 : B=100 40 D=RAND(B-A+1)+A : REM SECRET NUMBER (1-100) 50 F=0

60 F=F+1 70 ? “” 80 ? “ROUND”;F 90 ? “LOW:”;A;“ HIGH:”;B 100 INPUT “YOUR GUESS:”; E 110 IF E>B THEN GOSUB 800:GOTO 70 120 IF E<A THEN GOSUB 700:GOTO 70 130 IF E<D THEN GOSUB 600:A=E:GOTO 60 140 IF E>D THEN GOSUB 500:B=E:GOTO 60

150 GOSUB 400 160 ? “SCORE:”;101-F 170 ? “PLAY AGAIN? (Y=1/N-0)” 180 INPUT G 190 IF G = 1 THEN GOTO 30 200 ? “THANKS FOR PLAYING!” 210 GOTO 5000

400 ? “YOU GOT IT IN”;F;“ GUESSES!” 410 RETURN

500 ? “LOWER!” 510 RETURN

600 ? “HIGHER!” 610 RETURN

700 ? “TOO SMALL!” 710 RETURN

800 ? “TOO LARGE!” 810 RETURN

900 ? “NUMBER GUESSING GAME V1.1” 910 ? “BY APPLEDOG (C) 2026” 920 ? “GUESS THE NUMBER BETWEEN 1 AND 100.” 930 ? “I'LL TELL YOU HIGHER OR LOWER.” 940 RETURN 5000 REM END

Ty to RUN this program and play a few rounds! The subroutines make it modular. If you want fancier win/lose messages later, just edit those GOSUB lines.

Negative Numbers in Action

Since negatives are fully supported, try tweaking the game or make a countdown timer:

10 PRINT “COUNTDOWN FROM ZERO…” 20 LET A = 0 30 PRINT A 40 LET A = A - 1 50 IF A >= - 10 THEN GOTO 30 60 PRINT “BLAST OFF INTO NEGATIVE SPACE!”

WHILE, DO-WHILE, and FOR-NEXT Part I

Stellar BASIC V1 keeps things simple and fast; no built-in FOR-NEXT or WHILE (yet! –they're coming in future updates). But you can create powerful repeating loops using just IF…THEN and GOTO, plus a counter variable when needed. This section will demonstrate the kind of clever thinking you will need to write advanced programs in TinyBASIC.

The key tricks:

  • WHILE – A while loop checks the condition first, so place the IF check before the loop body.
  • DO-WHILE – A do loop runs at least once, so place the IF check at the end of the loop body.
  • FOR-NEXT – A for-next is essentially a WHILE loop (see below).

Let's see these in action with short examples you can type in right now.

WHILE

WHILE is short for WHILE-DO. The loop check is at the front of the do-loop (so it may skip the loop entirely). This is like “while something is true, keep doing the body.”

Example: Print numbers from 1 to 5, checking first.

10 LET I = 1
20 IF I > 5 THEN GOTO 60
30 ? I
40 LET I = I + 1
50 GOTO 20
60 ? "DONE!"

This prints 1 through 5. If you change line 10 to LET I=10, it skips the printing entirely, since the condition was false when we checked the loop condition (at the front of the loop body).

DO-WHILE

In this style the do-loop is executed and then if the condition (the while) passes, it executes the loop again. This style of loop always runs at least once.

Example: Keep asking for a positive number until you get one.

10 INPUT "ENTER A POSITIVE NUMBER ", N
20 IF N > 0 THEN GOTO 40
30 PRINT "TRY AGAIN – MUST BE POSITIVE!"
40 IF N <= 0 THEN GOTO 10
50 PRINT "THANKS! YOU ENTERED "; N

This program runs the INPUT at least once. If you enter -5, it complains and loops back. If positive, it exits. The check is at the bottom; no skip on first pass.

FOR-NEXT

Here, the FOR idea is to iterate over a range, and the NEXT check usually occurrs at the front of the loop (but may also occurr at the end of the loop).

Example: Countdown from 10 to 0, check at top.

10 LET C = 10
20 IF C >= 0 THEN GOTO 40
30 GOTO 70
40 PRINT C
50 LET C = C - 1
60 GOTO 20
70 PRINT "IGNITION!"

Observe that sometimes flipping the logic of the test makes the code cleaner (no extra check):

10 LET C = 10
20 IF C < 0 THEN GOTO 60
40 PRINT C
50 LET C = C - 1
60 GOTO 20
70 PRINT "IGNITION!"

In this second case, inverting the logic falls-through to the do-loop. This is more efficient, even though the idea is “if C is greater than or equal to zero”. Know your logic operators!

FOR-NEXT part II

Here is another example of a similar loop, that amounts to a FOR-NEXT loop.

Example: Print even numbers 2 to 20 (step +2).

10 LET X = 2
20 IF X > 20 THEN GOTO 60
30 PRINT X
40 LET X = X + 2
50 GOTO 20
60 PRINT "EVEN NUMBERS DONE!"

Or, to simulate STEP -1 (countdown), as before,

10 LET Y = 20
20 IF Y < 1 THEN GOTO 60
30 PRINT Y
40 LET Y = Y - 1
50 GOTO 20
60 PRINT "BLAST OFF!"

These methods of program flow control are from old-school TinyBASIC programming. This was the norm – inventive uses of code. There is a LOT you can do with TinyBASIC. It's understandable, and very efficient.

An Exercise for the Reader

You can practice your BASIC skills with these exercises:

  • Make the WHILE example count backwards.
  • Turn the guessing game's round counter into a DO-style loop (run at least one round?).
  • Add a “Play again?” wrapper around your game.

When FOR-NEXT arrives in a future update, you'll appreciate how these hand-built loops taught you control flow. Until then, don't forget to “think differently!”

NEXT STEPS

What's Next?

You've now got the core toolkit: PRINT, LET variables, INPUT, IF-THEN, GOTO, RAND(), and GOSUB/RETURN for structured programs. Experiment wildly, change freely– add more subroutines, make the range -50 to 50 for negative guesses, introduce more features!

You are in the driver's seat now! Worlds of adventure await your discovery!

Check the SD-8516 Programmer's Reference Guide for deeper details including a roadmap for upcoming FOR-NEXT loops, PEEK/POKE for peeking at memory (classic retro fun), string variables (A$), sound commands, and more.

sd-8516_user_s_guide.1771669014.txt.gz · Last modified: by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki