Things about Commodore 64 BASIC

Lab Notes

Things I learned (either never knew or forgot) in the last 24 hours about Commodore 64 BASIC:

  1. I knew that the line editor has a “quote mode” so you can enter special key sequences, like “change text color,” into a string literal and they occur when the string is printed. These sequences appear in reversed-background font in the string. …

… I did not know (or forgot) that you can exit quote mode, start reversed-background mode (ctrl-9), and go back and type the corresponding symbol to get the same effect. For some effects like switching character sets, this is the only way to do it.

  1. CBM BASIC has two number types: 2-byte integers and 5-byte floating point numbers. BASIC converts ints to floats for operations, then truncates back to an integer when storing in an int var or using with funcs/statements that expect ints. This makes ints slower than floats….

@BedfordLvlExp has a good video on this subject. I’m curious to know if there are any cases where ints stay ints in transit. youtube.com/watch?v=wo14rD…

  1. CBM BASIC does not have a dedicated Boolean value type. Boolean operators evaluate to -1 for true and 0 for false. IF conditions will accept non-0 non-empty-string for true. AND, OR, NOT are bitwise operators and only act as logical operators on -1 ($FFFF) and 0 ($0000).

  2. AND and OR do not “short circuit” as in modern languages. It mostly doesn’t need to because BASIC expressions can’t have side effects, with two exceptions. #1: A sub-expression can cause a runtime error, such as divide-by-zero….

… A contrasting example in Python, where an erroneous sub-expression is only evaluated if the left of the AND is true:

  1. … #2: The only BASIC expression with a non-error side effect is the built-in function USR(num). This executes an arbitrary machine language routine whose address is stored at 785-786, using location 97 to pass num to the routine, evaluating to what the routine leaves there.

… Here is an example of a short routine that changes the border color. I poke it to $c000 (49152), then set the USR jump address to that location. Now any time USR() appears in an expression, it changes the border color.

  1. BASIC has a set of built-in functions, none of which have side effects except USR. You can define your own functions with: DEF FN <name>(<param>) = <expr> To call your function in an expression, use: FN <name>(<param>)

… You can’t define functions in direct mode, but they persist after the program has run. A user function can only have one parameter, and it must be a number. Its name does not conflict with variables.

  1. There’s a control structure I’ve never used: ON <number variable> GOTO/GOSUB <linenum>, <linenum>, … The value of the variable selects from the list of line numbers, starting at index 1. If the var is 0 or is out of range, the statement does nothing.
  1. I remember this fondly and was excited to rediscover it just now: Shift+Inst/Del inserts a space at the cursor on the current logical line. For the inserted spaces only, you are in quote mode! This makes it easier to insert control chars in a string on an existing line. …

… As shown, you can add Delete control chars in the inserted spaces. This is actually not true of regular quote mode, where Delete is just delete. To add Deletes, you either need this “insert mode,” or the aforementioned reverse-char entry trick (letter T).

… A caveat though! The deletes will execute when viewing a program listing! They are still there but will obscure program listing characters. You can use this to obfuscate your BASIC code.

Of course, I’m getting all this from the good ol’ Programmer’s Reference Guide. It’s a lovable book, with some interesting errors. Vintage tech writing is fascinating in how it may not have been obvious at the time what subjects to emphasize or how to order them…

… possibly because both the tech and the audience were so new, and/or the work was rushed and never revised. (Why is this two page discussion of the keyboard buffer in the “BASIC Language Vocabulary” chapter?)

… (Why is quote mode described in the PRINT reference entry and again at the end of the chapter, and described incorrectly?) (“CuRSoR” was cute when referring to the CRSR keys but it maintains that spelling throughout the chapter…?) Unfair to pick on it, just interesting.

(Originally posted to Twitter on April 21, 2019. It received 32 likes and 3 retweets.)