How To Use This Book
Although anyone wishing to learn 6502 machine language (ML) will likely find this book instructive and worthwhile, the specific example programs are written to work on five popular personal computers: Apple, Atari, VIC, Commodore 64, and the PET/CBMs. If your computer uses the 6502 microprocessor, but is not one of these machines, you will need to find a "memory map" for your particular machine. These maps - widely available in books and magazines, and from user groups - will allow you to follow and practice with the examples of 6502 machine language throughout this book.
In particular, there are several memory addresses which are used in many of the examples presented in this book. Their addresses are given for the five computers mentioned above, but if you have a different computer, you should look them up in a map of your machine:
1. "Which key is pressed?" This is an address, usually somewhere in the first 256 addresses, which is always holding the value of the most recently pressed key on the keyboard.
2. Starting Address of RAM Screen Memory. This is the address in your computer where, if you POKEd something into it from BASIC, you would see the effect in the upper left-hand corner of your screen.
3. Print a Character. This address is within your BASIC ROM memory itself. It is part of the BASIC language, but written in ML. It is the starting address of a routine which will put a character on the screen.
4. Get a Character. Also part of BASIC in ROM memory, this ML routine accepts a character from the keyboard and stores it.
5. A safe place. You must know where, in your computer, you can construct ML programs without interfering with a BASIC program or anything else essential to the computer's normal operations. The best bet is often that memory space designed to serve the cassette player called the cassette buffer. While practicing, you won't be using the cassette player and that space will be left alone by the computer itself.
Here are the answers to give the Simple Assembler (Appendix C) when it asks for "Starting Address." These are hexadecimal numbers about which we'll have more to say in the next chapter. For now, if you've got an Atari, type in 0600. If you use a PET/CBM, answer 0360. For VIC or Commodore 64, type: 0340. If you have an Apple, use 0300. For other computers, you'll need to know where there are about 100 RAM memory addresses that are safe.
All through this book, the examples will start at various arbitrary addresses (1000, 2000, 5000, for example). You should substitute the addresses which are safe in your computer. Just as it doesn't matter whether you start a BASIC program at line number 10 or line 100, it makes no difference whether a ML program starts at address 1000 or 0340, as long as you are putting it in a safe memory zone.
So, start all of the examples you assemble for practice in the same convenient, safe memory location for your machine. In fact, the Simple Assembler (SA) was designed to be modified and customized. See the introduction to Appendix C for more detailed instructions on customizing. Because you can make the SA conform to your needs, you might want to replace the line with the INPUT that requests the starting address (variable SA) with a specific address. In this way, you can work with the examples in the book without having to specify the safe address each time.
The First Step: Assembling
Throughout this book there are many short example ML programs. They vary in length, but most are quite brief and are intended to illustrate a ML concept or technique. The best way to learn something new is most often to just jump in and do it. Machine language programming is no different. Machine language programs are written using a program called an assembler, just as BASIC programs are written using a program called "BASIC."
In Appendix C there is a program called the "Simple Assembler." Your first step in using this book should be to type in the Microsoft version; it will work correctly on all personal computers using Microsoft BASIC. (If you have an Atari, type in the Atari version.)
Once you've typed this program into your computer, you can save it to tape or disk and use it whenever you want to construct a ML program. The example ML routines in this book should be entered into your computer using the Simple Assembler and then modified, examined, and played with.
Frequently, the examples are designed to do something to the screen. The reason for this is that you can tell at once if things are working as planned. If you are trying to send the message "TEST STRING" and it comes out "test string" or "TEST STRIN" or "TEST STRING@" - you can go back and reassemble it with the SA until you get it right. More importantly, you'll discover what you did wrong.
What you see on the screen when you POKE a particular number to the screen will differ from computer to computer. In fact, it can vary on different models of the same computer. For this reason, the examples in the book are usually given in standard ASCII codes (explained later).
Chances are that your computer uses a particular code for the alphabet which is not ASCII. The Commodores use what's called "PET ASCII" and the Atari uses ATASCII, for ATari ASCII. It's not that bad, however, since once you've found the correct number to show the letter "A" on screen, the letter "B" will be the next higher number. If you don't have a chart of the character codes for your computer's screen POKES, just use this BASIC program and jot down the number which is used to POKE the uppercase and lowercase “A.”
10 FOR I=0 TO 255: POKE (your computer's start-of-screen-RAM address), I: NEXT
With that knowledge, you can easily achieve the exact, predicted results for the examples in the book by substituting your computer's code.
A Sample Example
The following illustrations will show you how to go about entering and testing the practice examples in the book. At this point, of course, you won't recognize the ML instructions involved. The following samples are only intended to serve as a guide to working with the examples you will come upon later in the text.
After you've typed in and saved the SA, you can RUN it (it's a BASIC program which helps you to write ML). The first thing it does is ask you where you want to start your ML program - where you want it stored in memory. This is why you need to know of a safe place to put ML programs in your computer.
Of course you use line numbers when creating a BASIC program. Line numbers are not used in ML programming. Instead, you can think of memory addresses as "line numbers." So, if you are using the Atari, you will tell the SA that you are going to start your ML program at 0600. It will then print 0600 on the screen as if it were a line number, and you enter a ML program instruction, one per line, like this:
0600 PLA (This
PLA is always required in the Atari when you use USR.)
0601 LDY #00 (Stay in the hexadecimal mode for this example.)
0603 LDA #21
0605 STA (58)Y
The SA will automatically print each "line number" address when you are programming. You just type in those now mysterious ML instructions. This program will put the letter "A" on screen. After you are finished with an example, you type the word "END" and the SA will tell you the starting address of your ML program in RAM memory.
The next step is to try out the ML program you've written to see that it will work as planned. On the Atari, you could type:
and this will "RUN" your ML program. You will have sent control of the computer from BASIC to your new ML program via the USR command. Be sure to remember that the Atari requires the PLA as the first instruction of each ML program that you plan to go to from BASIC by using the USR command. In all the examples in this book, type in a PLA as the first instruction before continuing with the rest of the example if you use an Atari.
Most personal computers use Microsoft BASIC, and the PLA is not necessary. Here's how the same example would look on a PET/CBM after you answered 0360 as the starting address when the SA asked for it:
0360 LDY #01
0362 LDA #41
0364 STA 8000
(The word "END" isn't a 6502 ML instruction; it's a special signal to the SA to stop constructing a program and exit the SA program. Such special words are called pseudo-ops.)
Then you could test it in direct mode (just typing in the instruction onto the screen with no line number and not as part of a BASIC program) by typing:
SYS 864 and you should see the "A" on the screen.
Notice that the Atari and PET versions are similar, but not identical. All 6502 based computers will work with the same "instruction set" of commands which the 6502 chip can understand. The major differences occur when you need to specify something which is particular to the design of your computer brand. An example would be the location in memory of your computer's screen. The instructions at 0605 in the Atari example and 0364 in the PET example send the code for the letter "A" to the different screen locations for these two computer brands. Also, the letter "A" itself is signified by the number 41 on a PET and by the number 21 on an Atari.
But we'll go into these things further on. The main thing to learn here is how to use the SA to practice the examples. If you type in 0600 as the starting address as in the Atari example above, the SA will print the number 0600 on screen and wait for you to type in a 6502 instruction (PLA in this case) and hit RETURN. Then it will print the next memory address just as if you were using an automatic line numbering routine when programming in BASIC. After you hit RETURN, the SA will print 0601 and wait for you to type in LDY #00.
Return to Table of Contents | Previous Chapter | Next Chapter