PC Magazine's BASIC Techniques and Utilities by Ethan Winer
Total Page:16
File Type:pdf, Size:1020Kb
PC Magazine's BASIC Techniques and Utilities by Ethan Winer In memory of my father, Dr. Frank Winer Freeware Book, downloaded from http://www.ethanwiner.com TABLE OF CONTENTS INTRODUCTION Part I: UNDER THE HOOD Chapter 1. An Introduction to Compiled BASIC Chapter 2. Variables and Constant Data Chapter 3. Programming Methods Part II: HANDS-ON PROGRAMMING Chapter 4. Debugging Strategies Chapter 5. Compiling and Linking Chapter 6. File and Device Handling Chapter 7. Database and Network Programming Chapter 8. Sorting and Searching Part II: BEYOND BASIC Chapter 9. Program Optimization Chapter 10. Key Memory Areas in the PC Chapter 11. Accessing DOS and BIOS Services Chapter 12. Assembly Language Programming Ethan Winer: PC Magazine's BASIC Techniques and Utilities Book - 2 - ACKNOWLEDGEMENTS Many people helped me during the preparation of this book. First and foremost I want to thank my publisher, Cindy Hudson, for her outstanding support and encouragement, and for making it all happen. I also want to thank "black belt" editor Deborah Craig for a truly outstanding job. Never had I seen someone reduce a sentence from 24 words to less than half that, and improve the meaning in the process. [Unfortunately, readers of this disk version are unable to benefit from Deborah's excellent work.] Several other people deserve praise as well: Don Malin for his programming advice, and for eliminating all my GOTO statements. Jonathan Zuck for his contribution on database and network programming, including all of the dBASE file access routines. Paul Passarelli for unraveling the mysteries of floating point emulation, and for sharing that expertise with me. Philip Martin Valley for his research and examples showing how to read Lotus 1-2-3 binary files. Jim Mack for his skillful proof-reading of my manuscript, and countless good ideas. My wife Elli for her support and encouragement during the eight long months it took to write this book. ABOUT THE AUTHOR Ethan Winer is the founder of Crescent Software, Inc. located Ridgefield Connecticut. He has programmed in BASIC and assembly language since 1980, and is the author of Crescent's QuickPak Professional and P.D.Q. products. Ethan has also served as a contributing editor for both PC Magazine and BASICPro (now Visual Basic Programmer's Journal), and has written numerous feature articles for other popular computer publications. In 1992 Ethan retired from writing software professionally, and now spends his free time writing and performing music. PREFACE INTRODUCTION ============ BASIC has always been the most popular language for personal computers. It is easy to learn and use, extremely powerful, and some form of BASIC is included for free with nearly every PC. Although BASIC is often associated with beginners and students, it is in fact ideally suited for a wide range of programming projects. Because it offers the best features of a high- level language coupled with direct access to DOS and BIOS system services, BASIC is fast becoming the language of choice for beginners and professional developers alike. This book is about power programming using Microsoft compiled BASIC. It is intended for people who already possess a fundamental understanding of BASIC programming concepts, but want to achieve the best performance possible from their BASIC compiler. Power programming is knowing when and how to use BASIC commands such as CALL INTERRUPT, VARSEG and VARPTR, and even PEEK and POKE effectively. It Ethan Winer: PC Magazine's BASIC Techniques and Utilities Book - 3 - involves understanding the PC's memory organization sufficiently to determine how much stack space is needed for a recursive subprogram or function. A power programmer knows how to translate a time-critical portion of a BASIC program into assembly language when needed. Finally, and perhaps most importantly, power programming means knowing enough about BASIC's internal operation to determine which sequence of instructions is smaller or faster than another. This book will show you how to go beyond creating programs that merely work. Because it explains how the compiler operates and how it interacts with the BASIC runtime language library, this book will teach you how to write programs that are as small and fast as possible. Although the emphasis here is on Microsoft QuickBASIC and the BASIC Professional Development System (PDS), much of the information will apply to other BASIC compilers such as Power Basic from Spectra Publishing. Despite what you may have read, BASIC is the most capable and easy to learn of the high-level languages. Modern BASIC compilers are highly optimizing, and can thus create extremely efficient executable programs. In addition, you can often achieve with just a few BASIC statements what would take many pages of code in another high-level language. Moreover, beginners can be immediately productive in BASIC, while serious programmers have a wealth of powerful capabilities at their disposal. Microsoft BASIC has many capabilities that are not available in any other high-level language. Among these are dynamic (variable-length) strings, automatic memory allocation and heap management, built-in support for sophisticated graphics, and interrupt-driven communications. Add to that huge arrays, network file handling, music and sound, and protection against inadvertently overwriting memory, and you can see why BASIC is so popular. This book aims to provide intermediate to advanced programmers with information that is not available elsewhere. It does not, however, cover elementary topics such as navigating the QuickBASIC editor, loading and saving files, or using the Search and Replace feature. That information is readily available elsewhere. Rather, it delves into previously uncharted territory, and examines compiled BASIC at its innermost layer. Besides the discussions and programs in the text, this book includes a companion disk [separate ZIP file] that contains all of the subroutines and other code listed in this book, including several useful utilities. Installing these programs is described in the Appendix. CONVENTIONS USED IN THIS BOOK ============================= This book uses the terms QuickBASIC and QB to mean the Microsoft QuickBASIC 4.x and 7.x editing environments. BC and Compiler indicate the BC.EXE command-line compiler that comes with QuickBASIC, Microsoft BASIC PDS, and the now-discontinued BASIC 6.0. When a distinction is necessary, QBX will refer to the QuickBASIC Extended editor that comes with the BASIC Professional Development System (PDS). In most cases, the discussions will be the same for all of these versions of BASIC. When a difference does occur, the PDS and QBX exceptions will be indicated. [Because there is no way to indicate italics in a disk file, where they would have been used for emphasis or clarity the words are instead surrounded by asterisks (*).] HOW THIS BOOK IS ORGANIZED ========================== This book is divided into parts, and each part contains several chapters that discuss a specific aspect of BASIC programming. You needn't fully understand an entire chapter before moving on to the next one. Each topic will be covered in great depth, and in many cases you will want to return to a given chapter as your knowledge and understanding of the subject matter increases. Ethan Winer: PC Magazine's BASIC Techniques and Utilities Book - 4 - Part 1 is "Under the Hood," and its three chapters describe in detail how your BASIC source code is manipulated throughout the compiling and linking process. Chapter 1 presents an overview of compilers in general, and BASIC compilers in particular, It discusses what BASIC compilers are all about and how they work, and how the compiled code that is generated interacts with routines in the runtime libraries. Chapter 2 discusses variables, constants, and other program data, and how they fit within the context of the PC's memory organization. This chapter also covers bit manipulation using AND, OR, and XOR. Chapter 3 examines the various control flow methods available in BASIC, showing which statements and procedure constructs are appropriate in different situations. In particular, you will learn the relative advantages and disadvantages of each method, based on their capabilities, code size, and speed. Part 2, "Programming Hands On," examines programming techniques, and shows specific examples of writing effective code and also making it work. Chapter 4 explores program debugging using the facilities built into the QuickBASIC editing environment, as well as the CodeView utility that comes with Microsoft BASIC PDS. This chapter also discusses common programming problems, along with the appropriate solutions. Chapter 5 explains compiling and linking, both from within the QB environment, and directly from DOS. A number of compiler options are inadequately documented by Microsoft, and each is discussed here in great detail. A thorough discussion of the LIB.EXE utility program included with BASIC explains how libraries are manipulated and organized. Chapter 6 covers all aspects of file and device handling, and discusses the many different ways in which data may be read and written. The emphasis here is on speeding file handling as much as possible, and storing data on disk efficiently. Because input/output (I/O) devices are accessed similarly, they too are described here in detail. Chapter 7 explains the basics of writing database and network applications, and discusses file locking strategies using practical programming examples. A series of subroutines show how to read and write files using the popular dBASE format, and these may be incorporated into program that you write. Chapter 9 shows how to sort and search array data as quickly as possible. Several methods are examined including conventional and indexed sorting, and many useful subroutines are presented. The final part, "Beyond BASIC," includes information that is rarely covered in books about BASIC. Its three chapters go far beyond the information provided in any of the Microsoft manuals. Chapter 10 identifies many of the key memory areas in the PC, and shows when and how they can be manipulated in a BASIC program.