Lecture 05
Applesoft BASIC
Applesoft is the floating-point BASIC burned into every Apple II Plus and later. It's the language the machine speaks the moment you turn it on, and it can reach every corner of the hardware through PEEK, POKE and CALL. This lecture makes you conversant; the exercises make you fluent.
1. Immediate mode vs. programs
At the ] prompt, anything you type without a line number runs immediately:
]PRINT 2+2
4
]PRINT "HELLO"
HELLO
Type a line with a number and it's stored instead. LIST shows the program, RUN executes it from the lowest line number, NEW erases it:
]10 PRINT "APPLE ][ FOREVER"
]20 GOTO 10
]RUN
APPLE ][ FOREVER
APPLE ][ FOREVER
APPLE ][ FOREVER
...
Press Ctrl+C to break out of a running program. Line numbers double as labels for GOTO/GOSUB; the convention is to number by tens so you can insert lines later.
2. The core language in one sitting
Variables
10 X = 3.14159 : REM real number
20 N% = 42 : REM integer (% suffix)
30 NAME$ = "WOZ" : REM string ($ suffix)
40 DIM SCORES(9) : REM array of 10 reals, indices 0-9
SCORE and SCREEN are the same variable SC. This has ruined many a program. Also avoid names containing reserved words: TOTAL contains TO and won't parse.Decisions and loops
10 INPUT "YOUR AGE? "; A
20 IF A >= 18 THEN PRINT "WELCOME" : GOTO 50
30 PRINT "COME BACK IN "; 18 - A; " YEARS"
40 GOTO 10
50 FOR I = 1 TO 5
60 PRINT "ITERATION "; I
70 NEXT I
There is no WHILE, no ELSE block, no procedures with parameters — you build everything from IF … THEN, GOTO, GOSUB … RETURN, and FOR … NEXT (which supports STEP, including negative steps).
Subroutines
10 GOSUB 1000 : REM draw title
20 GOSUB 2000 : REM play game
30 END
1000 PRINT "*** MY GAME ***" : RETURN
2000 PRINT "(GAME GOES HERE)" : RETURN
END before your subroutines matters — otherwise execution falls through into them.
Strings and text-screen control
10 HOME : REM clear screen, cursor to top
20 A$ = "APPLE" + " " + "][" : REM concatenation
30 PRINT LEN(A$); " "; LEFT$(A$,5); " "; MID$(A$,7,2)
40 VTAB 12 : HTAB 18 : REM position cursor (1-based!)
50 PRINT "CENTERED-ISH";
60 GET K$ : REM wait for one keypress
Useful built-ins
| Function | What it does |
|---|---|
RND(1) | Random real in [0,1). INT(RND(1)*6)+1 rolls a die. |
INT(X), ABS(X), SGN(X), SQR(X) | Floor, absolute value, sign, square root |
ASC(A$), CHR$(N), STR$(X), VAL(A$) | Convert between characters, codes, numbers and strings |
SPC(N), TAB(N) | Spacing inside PRINT |
FRE(0) | Bytes of memory free (also forces garbage collection) |
3. Graphics from BASIC
100 GR : REM lo-res mode
110 FOR C = 0 TO 15
120 COLOR= C
130 VLIN 0, 39 AT C * 2 : REM stripe per color
140 NEXT
200 HGR : HCOLOR= 3 : REM hi-res, white
210 FOR X = 0 TO 279 STEP 4
220 HPLOT 140,0 TO X,159 : REM fan of lines
230 NEXT
Lo-res colors 0–15: 0 black, 1 magenta, 2 dark blue, 3 purple, 4 dark green, 5 grey, 6 medium blue, 7 light blue, 8 brown, 9 orange, 10 grey, 11 pink, 12 green, 13 yellow, 14 aqua, 15 white. Hi-res HCOLOR= 0–7: black, green, purple, white, black, orange, blue, white (the two palettes from Lecture 04).
4. Reaching the hardware: PEEK, POKE, CALL
This trio is what makes Applesoft feel like a systems language:
PRINT PEEK (49200) : REM read address $C030 (clicks the speaker)
POKE 1024, 1 : REM write $01 to $0400: inverse 'A' top-left
CALL 64600 : REM jump to $FC58: the ROM's clear-screen routine
CALL -151 : REM enter the Monitor (Lecture 06!)
Everything from Lecture 03's soft-switch table is available: flip display modes, read the keyboard directly, click the speaker. And CALL lets BASIC hand control to machine-language routines — yours or the ROM's.
CALL -151 is the most famous number in Apple II lore: −151 = 65536 − 151 = 65385 = $FF69, the Monitor's entry point. Programmers typed it so often it became muscle memory.5. Anatomy of a real program
A complete, runnable mini-game — number guessing with sound and screen formatting. Type it in (or paste it into your emulator):
10 REM *** GUESS THE NUMBER ***
20 HOME
30 SECRET = INT ( RND (1) * 100) + 1
40 TRIES = 0
50 VTAB 2: HTAB 8: PRINT "GUESS MY NUMBER (1-100)"
60 INPUT "YOUR GUESS? "; G
70 TRIES = TRIES + 1
80 IF G = SECRET THEN 200
90 IF G < SECRET THEN PRINT "TOO LOW"
100 IF G > SECRET THEN PRINT "TOO HIGH"
110 FOR I = 1 TO 10: X = PEEK ( - 16336): NEXT : REM click
120 GOTO 60
200 PRINT : PRINT "GOT IT IN "; TRIES; " TRIES!"
210 FOR J = 1 TO 15: FOR I = 1 TO J * 3
220 X = PEEK ( - 16336)
230 NEXT : NEXT : REM victory buzz
240 INPUT "PLAY AGAIN (Y/N)? "; A$
250 IF A$ = "Y" THEN 20
260 END
Things to notice:
- Program structure by line-number bands: setup (10–50), main loop (60–120), win handler (200+).
- Hardware access mixed freely with logic — the speaker clicks are just PEEKs.
RND,INPUT, string comparison, screen positioning: most of the language in 26 lines.
6. Saving your work
With DOS 3.3 or ProDOS booted (any disk-image-equipped emulator):
SAVE GUESS : REM write program to disk
CATALOG : REM list the disk
LOAD GUESS : REM read it back
RUN GUESS : REM load and run in one step
DELETE GUESS
Check your understanding
Q1. What's wrong with using the variables SCORE and SCREEN in one program?
Q2. What does CALL -151 do?
Q3. Which statement puts an inverse "A" in the screen's top-left corner?