6502 Assembly Language Routines
Here's a book containing over 40 subroutines to do some of the most common jobs in 6502 Assembly language.
The subroutines correspond to some of a programmer's most commonly needed tasks. Input, output, arrays, arithemetic and string functions are dealt with. The end result is a handy recipe book of frequently needed operations.
The emphasis is on utility functions. Complete packaged programs are not offered here; the reader is supplied with the coding chunks he needs for the bread and butter activities. Want to input a decimal number in ASCII and convert it to binary? You'll find the information here. The same is true if you want to output, to use hexadecimal numbers, or convert to the EBCDIC character set.
The first 160 pages or so might be termed, "A 6502 review manual." The characteristics of the 6502 are reviewed in the first chapter, entitled General Programming Methods. The second chapter seems directed to users from other processor environments: it shows how to implement features they might view as "missing" from the 6502's instruction set. For example, how do you negate the value in the A register? Exclusive OR with hexadecimal FF and then add 1, that's how. Users without experience from other environments will still find this a fascinating collection of simple techniques.
Chapter 3 is a beauty. Entitled "Common Programming Errors," it itemizes in detail some of the most frequent mistakes that 6502 programmers make. It's valuable reading. Indeed, perhaps the authors should have read it themselves one last time: they might have spotted their minor coding error on page 182, where they use BPL after a comparison. As page 136 sternly points out, "In comparing unsigned numbers, the Carry flag indicates which number is larger."
The program section starts on page 157. At this point, there are details on how the subroutines are organized. There's also a list of the subroutines themselves, which might have more properly been given in or near the Table of Contents at the start of the book.
The programs are broken into categories: Code conversion, Array Manipulation, Arithmetic, Bit Manipulation, String Manipulation, Array Operations, Input/Output and Interrupts. The book concludes with three appendixes containing general reference material, a Glossary, and an Index.
It should be emphasized that the programs are supplied in assembly language, not machine language. The reader will not see the final machine code; he's expected to take the "source coding" and put it through his own assembly process. Similarly, the user will choose the location of the program and its variables when he performs the assembly. This gives the book's programs a rather abstract feel even though they can be assembled into working code very quickly.
The assembly language is non-standard for the 6502 environment. Where most coders would write hexadecimal FF as $FF, the book writes 0FFH. Where the standard assembler code to set aside two locations should be * = * + 2, the book uses .BLOCK 2. This seems to be an offshoot of Adam Osborne's determination to hew to a universal assembly language standard. Unfortunately, there doesn't seem to be a 6502 assembler using such a standard; as a result, the book becomes a little more difficult to read and use.
Each subroutine comes with excellent formalized documentation. Memory space used, execution time and many other relevant factors are carefully noted. There is a variety of styles, particularly in the area of passing values to and from the subroutine: sometimes it's a common area, sometimes the registers, sometimes the stack. The knowledgeable reader should have no trouble adapting this to the particular style wanted. It's all well spelled out.
The coding is straightforward and non-clever. You won't find any elegant and obscure algorithms in this collection; and that's probably good. If you decide to try your hand at adapting a program to your own special requirements, it will be unlikely to jump up and bite you. The code takes the obvious approaches; and this is particularly good for learning.
Sometimes the authors err on the side of caution. In Multiple-Precision Binary Addition and Subtraction, for example, they specify that the routines add and subtract unsigned binary numbers. In fact, the same routines work on signed numbers.
The emphasis is on integer arithmetic. Reasonable enough considering that the vast majority of machine language applications are integer-oriented. Some readers might wish for information on floating-point or even fixed-point fractions; they won't find it here.
Arithmetic is limited to the four basic functions. This is to be expected where numbers are fixed point. Don't look for square root routines or more advanced material here ... it's all fundamentals.
Many of the arithmetic routines assume that all values have the same size (same number of bytes). This may need to be adjusted by the user: for example, it is often useful in division to have a small divisor (or quotient).
Strings are "conventional," limited to a maximum of 255 bytes. Users may find it necessary to shift the coding around to suit their own particular organization. The book assumes that string length is supplied as a value. Other systems – signalling the end of a string with a zero byte or a carriage return – would need some minor adjustments.
The only sort given is a bubble sort. That has the advantage of simplicity, but perhaps readers should be warned that it's rather inefficient and they should keep their eyes open for other, more efficient, techniques.
The RAM Test program is naive. The most common RAM failure is not caused by storing information and not being able to read it back. It's a result of interference between addresses: store something at one address and it will also appear in another location. You'll never catch this kind of thing by storing and checking a single byte at a time.
I missed random numbers. There are several easy techniques, and at least one might have been usefully supplied.
It's a highly useful reference book. The introduction is a good summary of the 6502's working characteristics; the programs are useful ones that can be put to work.
The book is especially well adapted to a user who is moving to the 6502 from other processors. Complete beginners might find that the book is too fast for them, but the information is all there.
6502 Assembly Language Subroutines
by Lance A. Levanthal and Winthrop Saville
Osborne/McGraw Hill, 630 Bancroft Way
Berkeley, CA 94710
550 pages. $16.99