For Retro Computing Enthusiasts
, ,

Machine Language For Beginners: The Easy Way

By Richard Mansfield — originally published in COMPUTE!’s Gazette Issue 2, August 1983

Don’t let anyone tell you that there’s something impossibly complex about machine language. It can be harder to debug (locate and fix errors) than BASIC is, but it’s not inherently more difficult to learn or to write.

You just need to memorize some new commands; obtain and practice with some new tools (assemblers, disassemblers, monitors); and pick up a few new programming techniques. In the next several months, we’ll cover these commands, tools and techniques, step by step, in the hope that you’ll discover for yourself that machine language can be an easy – and fascinating – way to communicate with your computer.

“RAMtest,” the program below with versions for the 64 and the VIC, is an example of one of the most common ways that machine language routines are printed in magazines. This kind of program is called a BASIC loader. The value of loaders is that the user need not understand anything about the machine language program. It’s the easy way to use machine language. Just type in the BASIC program as it appears, type RUN, and the machine language (the numbers in the DATA statements) is POKEd into memory for you.

Strange Strings

RAMtest is a useful program: it tests your Random Access Memory (RAM) to be sure that every cell is operating correctly. RAM chips are generally quite reliable, but you might have one fail on you. There are various odd things that can happen during a program RUN as the result of a faulty RAM chip. One sign would be the sudden appearance of strange strings. For example, you might type A$ = “ABCDEFG” and when you asked to see A$ (by typing ?A$), you would get ABC)EFG or something.

Here’s how to use RAMtest:

1. Type in whichever version applies to your machine, VIC or 64.

2. Type SAVE (to keep a copy on tape or disk).

3. Type RUN. The data will be loaded into a safe area of your computer which is not part of BASIC RAM. We’re loading the machine language program into decimal address 828-1019. This is the cassette buffer RAM, and it’s unused by BASIC except during Datassette operations. We can’t store the machine language program into normal BASIC RAM because we’re going to fill each memory cell with all 256 possible numbers as our test. That would cause the program to test – and thereby obliterate – itself. The cassette buffer is a popular, safe place to put machine language since it is out of BASIC’s way.

4. After you see READY on screen, your machine language is sitting down in lower RAM memory (decimal 864-995), waiting for you to

activate it. You send control of the computer to a machine language program by using BASIC’s SYS command. However, machine language programs do not necessarily start with the first number in their sequence. The entry point could be anywhere within the routine. Unlike BASIC, which always begins with the lowest line number, machine language might store text messages or other information below the entry point. That’s the case in the RAMtest program. To start it going, type SYS 884.

A Vibrating Square

If all your DATA numbers were correctly typed in, you should now see two things happening on screen. Up in the left corner you’ll see a vibrating square. This is a visual demonstration of what’s happening to each of your RAM memory cells in turn. As each number from zero to 255 is POKEd into each cell of the computer, it’s also being POKEd into the first screen memory cell so you can see it happening. (Machine language POKEs are called “STA,” meaning STore the Accumulator.)

The other thing you’ll notice is that the decimal address range currently being tested appears on screen. The Commodore 64 version tests cells from 2048 up to address 40960. The VIC test is designed to run on an unexpanded VIC and will go from 4096 up to 7936. At the conclusion, the words “TEST OVER” will signify that every memory cell tested has correctly stored every possible number.

Now, type LIST. You can see the effect of our mass POKEs. For a line number you get 65535. (However, for technical reasons, you can’t actually use line numbers larger than 63999 in BASIC.) Line numbers are always stored in two-byte units, and this is the biggest number that the computer can hold within two memory bytes. Following that are more than 200 pi symbols. This is the symbol you get by typing ?CHR$(255). We’re not seeing screen RAM memory when we ask for a LIST. Instead, we see a translation of a BASIC program. The series of 255’s appear, after this translation, as pi symbols. It means that each of these cells – you’re looking at the bottom of BASIC RAM where BASIC programs start – is now holding a number 255 after having held everything from zero up to 255 during the test.

If you want to regain control and return to normal BASIC conditions after this test, you’ll need to POKE 2048,0 on the 64; POKE 4096,0 on the VIC. The very first cell in BASIC RAM must contain a zero for things to work correctly.

Most likely, your RAM memory passed the test. Just to see what would happen if there were a bad cell, we can make the test try to POKE into Read Only Memory (ROM). It will try, but ROM is protected against being written over, so the attempted POKE will fail and it will appear to the RAMtest program that there is a bad cell. To try this, LOAD RAMtest from your disk or tape. Then RUN. Then type POKE 885,245. This will set the testing to start at memory cell 62720, and you’ll see the results when you start the test with SYS 884.

Next Month: Disassembly

You could write a program in BASIC to perform this same test, but you’d need to start the test higher up in memory and you’d need to leave it RUNning overnight. The great speed of machine language execution makes it ideal for large tasks like RAM testing.

We’ve loaded in and used a machine language program; we’ve even modified it by POKEing in a different start address for the test. Save RAMtest on tape or disk. Next month we’ll take a closer look at how RAMtest was programmed. To do that, we’ll need to type in a BASIC program called a disassembler, which will translate raw machine language into a listing called a disassembly. That will let us see what RAMtest looks like and how it works.

See program listings on page 125.

Enjoyed this? It’s what we do every month.

The revived COMPUTE!’s Gazette brings the spirit of the golden era to today’s retro scene — Commodore, Atari, Amiga, and more, in full-color print and digital every month.