The Commodore 128 has a built-in machine language monitor which makes it ideal for ML development. However, most (or pretty much all) documentation on this subject is geared towards the Commodore 64, making it slightly difficult to get a head start in writing ML code for the 128.
Before I forget how to do it, here are a few pointers – courtesy of Jim Butterfield’s book “Machine Language – Expanded Edition”.
Let’s begin by typing MONITOR in C128 mode. It’ll take us to the machine language monitor. We’ll start our programme at $0B00. To begin assembling our code, we’ll type A 0B00 (A for Assemble), followed by these lines:
The MONITOR will turn this text into the output you’ll see in the screenshot above (the lines starting with a . dot). Here’s what this code will do when called:
First we’ll load the X register with a value of zero. We’ll use this register as a counter. In the next line we’ll load the accumulator with whatever is stored in address $0C10 plus whatever is stored in the X register. So if X has a value of zero, then the contents of $0C10 will be loaded. If X was 1, then the value in $0C11 would be loaded, and so forth.
We’re using this as ASCII representation of our text (Hello World in a box in this case). With JSR $FFD2 we’ll call a Kernal routine that prints a single character onto the screen. Now we’re incrementing X by one and ask if it’s 45 yet (CPX #$2D). This would indicate that we’ve printed all the characters we need. If that’s not the case, we’ll return to line 2 and keep printing. Otherwise, we’ll stop the programme.
Storing ASCII characters
You’d think it was possible to simply type in text in the MONITOR. But of course that would be too easy. Instead we need to grab one of those massive tables and hack in each character’s ASCII code in hex. How convenient!
Type M 0C10 (or whichever location in memory you’d like to store your text string at) and overtype the numbers at the start of the line, each one representing a single byte of our ASCII text. At the end of each line you’ll see what those characters look like when converted.
In my case it’s a total of 45 characters, beginning with a return, followed by the top of the box, HELLO WORLD, and the bottom of the box.
Running from the MONITOR
To start the programme from the monitor, we’ll type G F0B00. We’ll end up with a SYNTAX ERROR and back on the BASIC screen though due to the RTS command at the end of the listing. If we replace it with a BRK command instead, we’ll end up back in the MONITOR.
The important thing to remember is the five digit addressing mode on the C128 (i.e. G for GO, followed by F0B00). Our programme starts at $0B00 in memory, but to make it run properly we’ll have to specify which BANK it should be called from. Anything other than BANK 0 or BANK 1 is fine, otherwise we won’t reach the print routine at $FFD2. In my example I’m choosing F, but E would work fine too (as we’ll see in a moment).
Running from BASIC
Type X to exit the monitor and go back into the land of BASIC. First we’ll need to choose a BANK. We’ll have 16 to choose from (0 to 15), so perhaps let’s try BANK 15. Now we’ll need to type the start of our programme in decimal:
or we can use the DEC command to convert hex to decimal on the fly:
Saving the programme
From the MONITOR, we can save the programme using the S command. It needs to be followed by a name (in double quotes), followed by the drive number, memory start and memory end plus one byte – all separated by a comma. It’s probably easier to show than to write:
S "HELLO WORLD",8,0B00,0C40
We’re saving more bytes than strictly necessary here due to the large gap between our code and the beginning of the ASCII string. Our string could go up to $0C3F. The last byte in $0C40 is NOT saved to disk (or tape).
We can do the same from BASIC using the BSAVE command (for Binary SAVE). The syntax is BSAVE “FILE NAME”, P1234 TO P5678. Sadly the DEC command doesn’t work inline with this command, which would make it extremely useful. We’ll have to convert the values into decimal manually instead.
BSAVE "HELLO WORLD", P2816 TO P3136
Loading the programme
To bring our masterpiece back into the computer from the MONITOR, the L command works a treat:
From BASIC we can load the programme the usual way, making sure we load it with ,DEVICE,1 at the end. This is to make sure it is loaded into the same memory it was saved from, rather than the start of BASIC:
Podcast: Download (Duration: 21:57 — 20.1MB)
Watch the full course in one convenient playlist:
Catch this episode on my Commodore Podcast: