Programming the Z80

This book has been designed as a complete self-contained text for learning programming, using the Z80. It can be used by a person who has never programmed before, and should also be of value to anyone using the Z80.
For the person who has already programmed, this book will teach specific programming techniques using (or working around) the specific characteristics of the Z80. This text covers the elementary to intermediate techniques required to start programming effectively.
This text aims at providing a true level of competence to the person who wishes to program using this microprocessor. Naturally, no book will effectively teach how to program, unless one actually practices. However, it is hoped that this book will take the reader to the point where he feels that he can start programming by himself and can solve simple or even moderately complex problems using a microcomputer.
This book is based on the author's experience in teaching more thatn 1000 persons how to program microcomputers. As a result, it is strongly structured. Chapters normally go from the simple to the complex. For readers who have already learned elementary programming, the introductory chapter may be skipped. For others who have never programmed, the final sections of some chapters may require a second reading.
The book has been designed to take the reader systematically through all the basic concepts and techniques required to build increasingly complex programs. It is, therefore, strongly suggested that the ordering of the chapters be followed. In addition, for effective results, it is important that the reader attempt to solve as many exercises as possible. The difficulty within the exercises has been carefully graduated. They are designed to verify that the material which has been presented is really understood. Without doing the programming exercises, it will not be possible to realize the full value of this book as an educational medium. Several of the exercises may require time, such as the multiplication exercise. However, by doing them, you will actually program and learn by doing. This is indispensable.
For those who hav acquired a taste for programming when reaching the end of this volume, a companion volume is planned: the Z80 application book. Other books in this series cover programming for other popular microprocessors.
For those who wish to develop their hardware knowledge, it is suggested that the reference books "From Chips to systems: an introduction to microprocessors (ref. c201A) and microprocessor interfacing techniques be consulted.
The contents of this book have been checked carefully and are believed to be reliable. However, inevitably, some typographical or other errors will be found. The author will be grateful for any comments by alert readers so that future editions may benefit from their experience. Any other suggestions for improvements, such as other programs desired, developed or found of value by readers, will be appreciated.
Read More/Download

The Second Book Of Machine Language

by Richard Mansfield
This book shows how to put together a large machine language program. All of the fundamentals were covered in my first book, Machine Language for Beginners. What remains is to put the rules to use by constructing a working program, to take the theory into the field and show how machine language is done.
Showing how to construct an assembler-written entirely in machine language would serve two useful purposes. It would illustrate advanced programming technique and also provide the reader with a powerful assembler to use in other ML programming.
This book, then, offers the reader both a detailed description of a sophisticated machine language program (the LADS assembler) and an efficient tool, a complete language with which to write other machine language programs. Every line in the LADS assembler program is described. All the subroutines are picked apart and explained. Each major routine is examined in depth.
LADS, the Label Assembler Development System, is a fast, feature-laden assembler-it compares favorably with the best assemblers available commercially. And not the least of its virtues is the fact that few programs you will ever use will be as thoroughly documented and therefore as accessible to your understanding, modification, and customization.
LADS is a learning device too. By exploring the assembler, you will learn how to go about writing your own large machine language (ML) programs. You will see how a data base is created and maintained, how to communicate with peripherals, and how to accomplish many other ML tasks. Also, because you can study the creation of a computer language, the LADS assembler, you will gain an in-depth knowledge of the intimate details of direct communication with your computer.
Most programming involves a tradeoff between three possible objectives: speed, brevity, or clarity. You can program with the goal of creating the fastest running program possible. Or you can try to write a program which uses up as little memory as possible. Or you can try to make the program as understandable as possible, maximizing the readability of the program listing with REMarks.
LADS emphasizes clarity so that its source code will serve as a learning tool and as the focus of this book. It's designed so that important events in the program can be easily explained and understood. Virtually every ML instruction, every tiny step, is commented within the source code listings following each chapter.
This doesn't mean that LADS is flabby or slow. Assembling roughly 1000 bytes a minute and taking up 5K in memory, LADS is considerably faster and more compact than most commercial assemblers. That's because, in ML, you can have the best of both worlds: You can comment as heavily as you want, but the assembler will strip off the comments when it creates the object code. In this way, clarity does not sacrifice memory or speed.
The frequent comments contribute considerably to the educational value of this assembler. Exploring LADS is a way to learn how to achieve many common programming goals and how to construct a large, significant program entirely in ML. An additional advantage of this comprehensibility is that you'll be able to modify LADS to suit yourself: Add your own pseudo-ops, define defaults, format output. All this is referred to as a language's extensibility. We'll get to this in a minute.
What BASIC is to BASIC programming, an assembler is to ML programming. LADS is a complete language. You write programs (source code) which LADS translates into the finished, executable ML (object code). Unlike less advanced assemblers, however, symbolic assemblers such as LADS can be as easy to use as higher level languages like BASIC. The source code is very simple to modify. Variables and subroutines have names. The program can be internally commented with REM-like explanations. Strings are automatic via the BYTE command. There are a variety of other built-in features, the pseudo-ops, which make it easy to save object programs, control the screen and printer listings, choose hex or decimal disassembly, and service other common programming needs.
Perhaps the best feature of LADS, though, is its extensibility. Because you have the entire source code along with detailed explanations of all the routines, you can customize LADS to suit yourself. Add as many pseudo-ops as you want. Redesign your ML programming language anytime and for any reason. Using an extensible programming language gives you control not only over the programs you design, but also over the way that they are created. You can adjust your tools to fit your own work style.
Do you often need to subtract hex numbers during assembly? It's easy to stick in a - command. Would you rather that LADS read source programs from RAM memory instead of disk files? (This makes it possible to assemble using a tape drive. It can also be a bit faster.) In Chapter 11 we'll go through the steps necessary to make this and other modifications. You'll be surprised at how easy it is.
Finally, studying the language (the LADS assembler) which produces machine language will significantly deepen your understanding of ML programming.
Read More/Download

Machine Language For Beginners

by Richard Mansfield

Why Machine Language?
Sooner or later, many programmers find that they want to learn machine language. BASIC is a fine general-purpose tool, but it has its limitations. Machine language (often called assembly language) performs much faster. BASIC is fairly easy to learn, but most beginners do not realize that machine language can also be easy. And, just as learning Italian goes faster if you already know Spanish, if a programmer already knows BASIC, much of this knowledge will make learning machine language easier. There are many similarities.
This book is designed to teach machine language to those who have a working knowledge of BASIC. For example, Chapter 9 is a list of BASIC statements. Following each is a machine language routine which accomplishes the same task. In this way, if you know what you want to do in BASIC, you can find out how to do it in machine language.
To make it easier to write programs in machine language (called "ML" from here on), most programmers use a special program called an assembler. This is where the term "assembly language" comes from. ML and assembly language programs are both essentially the same thing. Using an assembler to create ML programs is far easier than being forced to look up and then POKE each byte into RAM memory. That's the way it used to be done, when there was too little memory in computers to hold languages (like BASIC or Assemblers) at the same time as programs created by those languages. That old style hand-programming was very laborious.
There is an assembler (in BASIC) at the end of this book which will work on most computers which use Microsoft BASIC, including the Apple, PET/CBM, VIC, and the Commodore 64. There is also a separate version for the Atari. It will let you type in ML instructions (like INC 2) and will translate them into the right numbers and POKE them for you wherever in memory you decide you want your ML program. Instructions are like BASIC commands; you build an ML program using the ML "instruction set." A complete table of all the 6502 ML instructions can be found in Appendix A.
It's a little premature, but if you're curious, INC 2 will increase the number in your computer's second memory cell by one. If the number in cell 2 is 15, it will become a 16 after INC 2. Think of it as "increment address two."
Throughout the book we'll be learning how to handle a variety of ML instructions, and the "Simple Assembler" program will be of great help. You might want to familiarize yourself with it. Knowing what it does (and using it to try the examples in this book), you will gradually build your understanding of ML, hexadecimal numbers, and the new possibilities open to the computerist who knows ML.
Seeing It Work
Chapters 2 through 8 each examine a major aspect of ML where it differs from the way BASIC works. In each chapter, examples and exercises lead the programmer to a greater understanding of the methods of ML programming. By the end of the book, you should be able to write, in ML, most of the programs and subroutines you will want or need.
Let's examine some advantages of ML, starting with the main one - ML runs extremely fast.
Here are two programs which accomplish the same thing. The first is in ML, and the second is in BASIC. They get results at very different speeds indeed, as you'll see:
Machine Language
169 1 160 0 153 0 128 153 0 129 153 130 153 0 131 200 208 241 96
5 FOR I=1 TO 1000: PRINT "A";: NEXT I
These two programs both print the letter "A" 1000 times on the screen. The ML version takes up 28 bytes of Random Access Memory (RAM). The BASIC version takes up 45 bytes and takes about 30 times as long to finish the job. If you want to see how quickly the ML works, you can POKE those numbers somewhere into RAM and run the ML program with a SYS (Commodore computers) or USR (Atari) or CALL (Apple). In both BASIC and ML, many instructions are followed by an argument. The instructions SYS and CALL have numbers as their arguments. In these cases, the instruction is going to turn control of the computer over to the address given as the argument. There would be an ML program waiting there. To make it easy to see this ML program's speed, we'll load it into memory without yet knowing much about it.
A disassembly is like a BASIC program's LISTing. You can give the starting address of an ML program to a disassembler and it will translate the numbers in the computer's memory into a readable series of ML instructions. See Appendix D for a disassembler that you can use to examine and study ML programs.
Read More/Download

Popular Posts