The Super-User's
BBC Micro Book
The Super-User's
BBC Micro Book
Brian James and Graham Keeler
University of Salford
with contributions from
Herbert Dunderdale
Cedric Owen
Gerald Tomlinson
ADDISON-WESLEY PUBLISHING COMPANY
Wokingham, England · Reading, Massachusetts · Menlo Park, California
Don Mills, Ontario · Amsterdam · Sydney · Singapore · Tokyo
Mexico City · Bogota · Santiago · San Juan
© 1985 Addison-Wesley Publishers Limited
© 1985 Addison-Wesley Publishing Company, Inc
All fights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior permission of the publisher.
The programs presented in this book have been included for their instructional value. They have been tested with care but are not guaranteed for any particular purpose. The publisher does not offer any warranties or representations, nor does it accept any liabilities with respect to the programs.
Cover design by Jonathan Doney.
Photoset direct from the authors' text by Quorum Technical Services Ltd.
Printed in Great Britain by The Bath Press.
British Library Cataloguing in Publication Data
James, Brian
The super-user's BBC micro book.
1. BBC Microcomputer--Programming
I. Title II. Keeler, Graham
001.64´2 QA76.8.B35
ISBN 0-201-15435-8
Library of Congress Cataloging in Publication Data
James, Brian.
The Super-User's BBC micro book.
Includes index.
1. BBC Microcomputer--Programming. 2. BASIC
(Computer program language) I. Keeler,
Graham, 1943 II. Title.
QA76.8.B35J35 1985 005.2´65 85-20148
ISBN 0-201-15435-8 (pbk. )
To wives and daughters
Econet is a trademark of Acorn Computers Limited.
Wordwise and Disc Doctor are trademarks of Computer Concepts.
Intel 8271 is a trademark of the Intel Corporation.
MC68B54 is a trademark of Motoroia Inc
CONTENTS
Preface xi
Chapter 1 Introduction 1
1.1 The BBC microcomputer 1
1.2 About this book 2
1.3 Using the book 3
Chapter 2 Programming statements and structures 5
2.1 Standard BASIC commands 13
2.2 Structured commands available on the BBC microcomputer 17
Chapter 3 Structured programming facilities 22
3.1 Introduction 22
3.2 Functions 23
3.3 Local parameters 28
3.4 Procedures (DEF PROC) 30
3.5 Other program structures 32
3.6 Recursive use of procedures and functions 34
Chapter 4 Strings 39
4.1 String variables 39
4.2 Printing with strings 40
4.3 Comparison of string variables 42
4.4 String manipulation functions 43
4.5 String arrays 50
4.6 Further string handling 52
Chapter 5 Mathematical functions 56
5.1 Standard functions 56
5.2 Random numbers 58
5.3 Logical expressions 62
Chapter 6 Graphics 69
6.1 Introduction 69
6.2 Plotting lines and curves 74
6.3 The PLOT and VDU graphics commands 81
6.4 Special graphics effects 86
Chapter 7 Programming errors 94
7.1 Syntax errors 95
7.2 Semantic errors 97
7.3 Execution errors 99
7.4 Rerunning the program 100
7.5 Error trapping 102
Chapter 8 Use of the disc filing system 106
8.1 Introduction 106
8.2 Structure of information on a disc 109
8.3 Formatting and verifying 112
8.4 Other DFS commands 115
8.5 Turnkey systems 123
Chapter 9 File handling 125
9.1 Types of file 125
9.2 Handling data files 127
9.3 Further aspects of data files 132
9.4 Random access data files 135
9.5 Single byte access to a data file 138
9.6 A general program to read or write a sequential data file 140
9.7 ASCII files 143
Chapter 10 Direct memory and system access 151
10.1 Computer memory 151
10.2 Organization of memory in the BBC microcomputer 153
10.3 The memory pointers 155
10.4 Memory access 159
10.5 A MEMZAP and DISCZAP program 162
10.6 The way BASIC programs are stored 168
10.7 Handling the filing and operating systems from
within programs 174
10.8 Using machine code programs 178
Chapter 11 Programming techniques 187
11.1 'User-friendly' programming 187
11.2 Input facilities of BBC BASIC 188
11.3 Validation 193
11.4 Display layout 193
11.5 Formatting numerical output 197
11.6 General presentation 197
11.7 Menu driven programs 201
Appendix A Special features of the BBC microcomputer 214
Appendix B Selected *FX commands 219
Appendix C Summary of the VDU commands 224
Appendix D Bugs and unexpected effects in BBC BASIC 226
Appendix E Differences between BASIC I and BASIC II 230
Appendix F The resident variable @% 232
Appendix G Pseudo-variables in BBC BASIC 236
Appendix H ASCII codes 237
Appendix I INKEY codes 239
Appendix J Error codes 241
Appendix K Printers 243
Appendix L The Econet filing system 247
Appendix M Using the DISCZAP program 262
Appendix N Answers to exercises 266
Appendix O The supplementary disc and tape 284
Index 285
PREFACE
The BBC microcomputer is in many ways outstanding for a small microcomputer in the wide range of facilities built into the basic machine and in the power and range of its language and operating system. Indeed, BBC BASIC is so much more extensive and better structured than the versions of BASIC found on most other small microcomputers that it almost needs to be considered as a separate language, of which standard BASIC is but a subset.
The aim of this book is to introduce readers to this treasure house of possibilities, although even so we have not been able fully to cover all aspects. In graphics, for instance, although we have devoted a full chapter to the topic we have but scratched the surface. A full treatment of this particular BBC microcomputer feature would take a complete book in itself - and indeed there are already several books devoted entirely to graphics and sound. Our endeavour instead has been to try and explore the full breadth of the system, and include areas which have been less widely discussed.
The book is not aimed at the complete beginner, although we have very briefly reviewed the fundamental aspects of the BBC computer and BASIC in the first two chapters. Rather, it is designed for the user who has made a start but who finds the need for help in order to progress further. It would also be ideal for users who are familiar with other microcomputers but would like a conversion course to the BBC microcomputer.
The book is based on a short course called 'Getting more from your BBC microcomputer' run jointly by the Department of Pure and Applied Physics and the Department of Electronic and Electrical Engineering at the University of Salford. Each participant is provided with an extensive manual including all the exercises for the course, and this manual was written jointly by us and our fellow contributors.
It is this original manual that we have revised and extended to produce this book, and we have included in this book revisions and adaptations in the light of our experience on the course, concentrating extra discussion and explanation on those aspects that participants have found interesting or difficult.
Following the style of our course, the exercises form an integral part of this book, and are included in the body of the text rather than at the end of each chapter. The ideal way to use the book is with a BBC microcomputer to hand, trying out the exercises as they are reached. The book also includes a large number of example programs. For those who would like to save the time taken to type in all these programs, they are also available on a supplementary disc or tape.
We would like to express our thanks to our colleagues, Bert Dunderdale, Cedric Owen and Gerry Tomlinson for their contributions to the original manual. We would also thank our wives, Wendy James and Marion Keeler, for their help with the typing of the text on the Wordwise word processor, and Wendy James for her help in reading the draft manuscript and preparing some of the programs.
Chapter 1
INTRODUCTION
1.1 THE BBC MICROCOMPUTER
The BBC microcomputer was initially produced in two versions - the Model A and the Model B. The Model B was very much more popular, and so the Model A is no longer readily available. Much more recently a third model has been launched, the Model B+. This book is written primarily for the Model B and B+ which are almost entirely compatible as far as software is concerned; though most of the contents will also apply to the Model A, there will be certain points, such as use of Modes 0 to 3, and the description of the Disc Filing System (DFS), which do not apply. Model A users should therefore be aware that there may be occasions when the comments made do not apply to their computer.
The computer's software has been revised during the life of the machine. This software comprises two parts: the BASIC interpreter and the Machine Operating System (MOS). The BASIC interpreter has appeared in two versions. The original version is now usually referred to as BASIC I, and the revised version as BASIC II. The changes are mostly very minor, except for certain changes to data file handling, which are explained in Chapter 9. The differences between the two are summarized in Appendix E.
The original MOS was called OS 0.1. This was replaced by OS 1.0, which was very quickly superseded by OS 1.2. The differences between the last two are minor, but OS 0.I was a very restricted MOS. You can find out which version your computer has by typing *FX 0. If it is OS 0.1, you should consider having it updated. This book assumes a version of at least 1.0, and Some features may not be correct for 0.1.
The computer Machine Operating System (MOS) is designed to work with a variety of different filing systems; that is, methods of storing and retrieving programs, data or other information. The original, built-in filing system is the cassette filing system, and many individual users will be using this. It suffers from a number of shortcomings. It is very slow, and being a serial recording system certain operations such as random access of data files and chaining back and forth between files are not easily possible. There is also a practical problem in that cassette recorders are not designed for digital recording, and vary widely in quality and performance. Moreover, they are not completely reliable, particularly for interchange amongst different recorders.
The Disc Filing System (DFS) is the other main system for individual users. Disc drives are precision instruments providing a very rapid form of data access, and the BBC DFS filing system is particularly fast in this respect, compared with other disc systems on microcomputers. The DFS provides a more comprehensive filing system than the cassette filing system, and includes features such as random access and the possibility of having several data files open simultaneously. A complete chapter of this book is devoted to the DFS, and certain aspects of the chapter on file handling also apply only to the use of the DFS.
A third filing system available for the BBC microcomputer is the Econet filing system. This is designed for users of groups of computers, so that they can share facilities such as disc storage and printing. One computer has to be dedicated to the Econet system, as file server, running a file server program which manages the whole network. There are two main versions of the file server, and the systems that they run are denoted as level 1 and level 2. Level 1 provides a rather primitive filing system, allowing only the transfer of complete files. Level 2 is a much more sophisticated system, allowing nearly all the facilities of the DFS, other than a few commands mainly concerned with ASCII files, such as *BUILD and *TYPE. None of these omissions is serious, and could quite easily be replaced by user-written programs. Level 2 does, however, require a computer fitted with a 6502 second processor, and as a minimum a double-sided 80 track disc drive. Appendix L discusses the Econet filing system in more detail.
1.2 ABOUT THIS BOOK
Although this book starts with a brief review of simple BASIC commands, it does not set out to teach the fundamentals of BASIC. There are many other books already available which do this. Instead, the aim here in this book is to introduce the user to many of the more advanced and sophisticated features available on the BBC microcomputer. Some of these are general features of BASIC, but others are available only on the BBC computer, which has a version of BASIC far superior to that available on most other microcomputers.
This book is therefore suitable for readers who have gained familiarity with the BBC computer, but would like to explore it in more depth and learn the full range of its capabilities. It would also be suitable for readers familiar with BASIC on a different computer, who would like to learn and become familiar with its use on the BBC computer in particular.
For users not already familiar with all the special features of the BBC microcomputer, Appendix A contains a brief summary of those features which are not dealt with in detail elsewhere in this book. It will be assumed throughout the remainder of the book that readers are familiar with these features.
Although the book can be read sequentially, it contains a wide variety of topics and many readers will want to use it for chapters dealing with topics of particular interest to them. Readers interested in file handling on disc, for instance, might turn straight to Chapters 8 and 9.

Figure 1.1 Links between chapters of the book.
There are links between some of the chapters, however; as far as possible, ideas needed in later chapters are introduced in earlier ones, although inevitably it is necessary in some cases to make use of techniques not fully explained until later. Figure 1.1 shows the major links between different chapters.
The book ends with a series of appendices giving tables, lists and a variety of detailed information, as well as answers to some of the exercises set throughout the book.
1.3 USING THE BOOK
The style of the book follows that of the short course on which it is based. It is written on the assumption that you can work at your computer as you read through the book. Thus exercises are not relegated to the end of the Chapter, but form an integral part of the text. Although there is no reason why you should not read the book without recourse to a computer, you will probably learn more quickly, reinforce the ideas presented and understand the comments made more fully, if you can try things out at the computer as you go along.
A lot of the examples presented are in the form of short programs. It should be emphasized that these are, in the main, provided to illustrate a point or demonstrate a principle. They are deliberately kept as short and simple as possible, and do not include 'user-friendly' features such as input validation or error protection. Some programs which contain endless loops do not even include a facility to quit! You will find it necessary to use <ESCAPE> to break out of them. The omission of the provision to quit the loop is not a mistake; the inclusion of escape options in every program would tend to obscure the points being made.
There are quite a number of programs of substantial length included in the book, and typing in all of these will be quite time-consuming. It is possible to purchase a disc which contains many of the longer programs listed in the book, as well as one or two which were too long to permit their inclusion here. Details of the disc and how to obtain it are given in Appendix O.
Note that here and throughout this book, angle brackets are used to mean that what is in the brackets should not be typed literally. Thus <ESCAPE> means the ESCAPE key, and <filename> means that an actual name of a file should be type in place of <filename>.