Advanced BASIC Programming for the Commodore 64
Total Page:16
File Type:pdf, Size:1020Kb
Advanced BASIC programming for the Commodore 64 and Other Commodore Computers Editor-in-Chief: David Culverwell Executive Editor: Terrell Anderson Production Editor: Sandra Tamburrino Text Design: Paula Huber Cover Design: Don Sellers Indexer: Leah Kramer Typesetter: Action Comp Co., Inc., Baltimore, MD Printer: R. R. Donnelley & Sons Co., Harrisonburg, VA Typefaces: Trump Mediaeval (text), Antique Olive (display), OCR-A (programs) Advanced BASIC programming for the Commodore 64 and Other Commodore Computers Michael Richter Robert J. Brady Co., Bowie, MD 20715 A prentice-Hall Publishing and Communications Company Note to Authors Do you have a manuscript or a software program re lated to personal computers? Do you have an idea for developing such a project? If so, we would like to hear from you. The Brady Co. produces a complete range of books and applications software for the per sonal computer market. We invite you to write to David Culverwell, Editor-in-Chief, Robert J. Brady Co., Bowie, Maryland 20715. Advanced BASIC Programming for the Commodore 64 and Other Commodore Computers Copyright © 1984 by Robert J. Brady Co. All rights reserved. No part of this publication may be reproduced or transmit ted in any form or by any means, electronic or mechanical, including photo copying and recording, or by any information storage and retrieval system, without permission in writing from the publisher. For information, address Robert J. Brady Co., Bowie, Maryland 20715. Library of Congress Cataloging in Publication Data Richter, Michael, 1939- Advanced BASIC programming for the Commodore 64 and other Commodore computers. Includes index. 1. Commodore 64 (Computer)-Programming. 2. Commodore computers-Programming. 3. Basic (Computer program language) I. Title. QA76.8.C64R52 1983 001.64'2 83-15604 ISBN 0-89303-302-2 Prentice-Hall International, Inc., London Prentice-Hall Canada, Inc., Scarborough, Ontario Prentice-Hall of Australia, Pty., Ltd., Sydney Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Prentice-Hall of Southeast Asia Pte. Ltd., Singapore Whitehall Books, Limited, Petone, New Zealand Editora Prentice-Hall Do Brasil LTDA., Rio de Janeiro Printed in the United States of America 84 85 86 87 88 89 90 91 92 93 10 9 8 7 6 5 4 3 2 1 CONTENTS 1 Introduction 1 1.1 Prerequisites and purposes 2 1.2 Principles of BASIC 4 1. 3 Interoperability 5 2 Writing for the User 9 2.1 Program planning 11 2.2 Program organization 14 2.3 Structured programming 17 2.4 Program documentation 19 2.5 Testing 21 3 Mechanics of a Program 25 3.1 Line numbers 26 3.2 Commands 28 3.3 Variables 29 3.4 Arrays 31 3.5 Bits, bytes, characters, and numbers 33 3.6 Keyboard input 35 3.7 FOR ... NEXT loops 38 3.8 Machine-language interface 40 3.9 Odds and endings 41 3.10 Coding tricks-DEF and ON 43 4 Devi.ces 47 4.1 Un-conventions 49 4.2 Printers 51 4.3 Disks and drives 53 4.4 Disk files 55 4.5 Disk commands 57 4.6 Tokens 59 4.7 Using files 61 4.8 Sorts and searches 63 5 SUPERLIST -an Example &. a Tool 67 5.1 SUPERLIST initialization 68 5.2 SUPERLISTutilities 70 5.3 StartingaSUPERLIST 71 5.4 Processing a line 72 v 5.5 Prin ting the references 74 5.6 What doesn't work 75 5.7 SUPERLISTing 76 5.8 SYS CHECKSUM 81 5.9 TURTLEW ALK 85 5.10 DOMINOES 90 6 Pursuing a Project 97 6.1 The first call 98 6.2 First meeting 100 6.3 Structuring the system 102 6.4 Inventory control (customer description) 104 6.5 Design 105 6.6 Using the warehouse program 107 6.7 Design review meeting 109 6.8 Coding 110 6.9 Maintenance 111 6.10 Bigger and better 114 Programs Supplied on the Diskette 116 Index 117 vi LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY The author and publisher of this book have used their best efforts in preparing this book and the programs contained in it. These efforts include the develop ment, research, and testing of the programs to determine their effectiveness. The author and the publisher make no warranty of any kind, expressed or implied, with regard to these programs, the text, or the documentation contained in this book. The author and the publisher shall not be liable in any event for claims of incidental or consequential damages in connection with, or arising out of, the fur nishing, performance, or use of the text or the programs. The programs contained in this book and on any diskettes are intended for use of the original purchaser user. The diskettes may be copied by the original purchaser-user for backup pur poses without requiring express permission of the copyright holder. TRADEMARKS OF MATERIAL MENTIONED IN THIS TEXT Apple machines: Apple Computer, Inc. C-BASIC: Compiler Systems, Inc. CBM, Commodore 64, Name Machine, Pet, SuperPet, Vic-20, Word Machine: Commodore Business Machines, Inc. CP/M: Digital Research Corporation M-BASIC, Microsoft: Microsoft Corporation Petspeed: Small Systems Engineering Tandy machines: Tandy Corporation WordPro: Professional Software, Inc. vii I 1 Introduction Advanced BASIC Programming is not a contradiction in terms. Tradi tionally, advanced software has been developed in other languages: FOR TRAN, COBOL, and, more recently, PASCAL. BASIC was designed to be an introductory language, not one for high-powered needs. Yet, BASIC can be used for those needs, often as well as or better than the traditional languages. This text has two interrelated functions: helping you develop advanced software and teaching you concepts of programming that will carry over into professional levels. In general, software quality is independent of the language used, so learning to write good programs will serve you well if you later move into the conventional languages. The great advantage of using BASIC for learning is that the computer you need for it is inexpen sive and readily available. This text is aimed at Commodore BASIC, and most of it can be applied on the VIC 20. This means that you can learn advanced BASIC on a machine selling for less than $200. By comparison, even a terminal to a computer capable of running FORTRAN will cost several times that amount, typically a few thousand dollars instead of a few hundred. Another advantage of basing this text on the Commodore version of BASIC is that it is applicable to all of their machines, from the VIC 20 and 64 through the business machines which, with peripherals, may cost $5-10,000 and offer substantial business capabilities. So, you may learn on an inexpensive starter system, then transfer that knowledge to ad vanced applications. You don't have to use a Commodore computer to learn advanced pro gramming in BASIC-in fact, most of the material in this book is relevant to Apple or Tandy machines as well. In addition, the philosophy and prin ciples of programming apply to all interpretive languages. Most of the 1 2 1: Introduction material on BASIC itself is common to all dialects, even those most dis tant from Commodore's. But the versions of BASIC from Microsoft (Ap ple, Tandy, M- and C-BASIC for CP/M) are close enough to Commo dore's for most operations not dealing with peripherals to apply directly. Finally, all the ideas and most of the examples should be valuable to you regardless of the hardware you use. 1.1 prerequisites and purposes This book is intended to help you learn to write professional software. It is not a BASIC primer, and it assumes that you already know the elementary uses of the language. The rules for using BASIC are provided with most computers. Other sources of elementary information are books, programs, and courses. For all of those and for other current information, see your dealer-the micro computer field changes too fast to provide current references in a book! When you have finished with the elementary materials, you should know enough to write a simple game or to read most BASIC programs. The next step is to read and to write until you understand the mechanics of programming. One way to combine the two tasks is to buy some soft ware on disk or tape and change it. What program you use is almost irrel evant. Any routine can be written in many different ways; its author chose some to write the program, but you can look for others. Take the program apart from its listing so that you understand how it was built. Think about ways to change small parts of it and try them out. The point is not to get a "better" version (although you may), but to un derstand how it was written and why. You will quickly discover that it is much more fun to write programs than to run them. Make sure that you don't start with too ambitious or complicated an example. Tic-tac-toe or a simple slot machine will do as well for this pur pose as ADVENTURE, and it will be a lot easier to learn. A good starting point would be an inexpensive game-but make sure you get one that's written entirely in BASIC. The example program in 5.10, DOMINOES, will do on a PET or a 64, although it is more complex than necessary for the job. After you know how and why simple software is built, you're ready to learn advanced programming. Again, it helps to use an example. Com modore's Word Machine is a good example for most purposes.