Firearms

Manual / Guide · 1983

Commodore 64 Programmer's Reference Guide

Read the PDF ↗

Control port 1 pin 6 (BUTTON A/LP) and the VIC-II LPX/LPY light-pen latch. Downsampled to 150 dpi.

Author
Commodore Business Machines
Year
1983
Type
Manual / Guide
Credit
Commodore Business Machines. Copy via the Internet Archive (item 'Commodore_64_Programmers_Reference_Guide_1983_Commodore')
  • commodore 64
  • vic-ii
  • light pen
  • control port

← Back to the Reference Library

Commodore 64 Programmer's Reference Guide

(It commodore ==- COMPUTER FIRST EDITION THIRD PRINTING-1983 Copyright @ 1982 by Commodore Business Machines, All rights reserved. Inc. This manual is copyrighted and contains proprietary information. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, eledronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of COMMODORE BUSINESS MACHINES, Inc. ii TABLE OF CONTENTS INTRODUCTION. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What's Included? .................................... . How to Use This Reference Guide. . . . . . . . . . . . . . . . . . . . " ix x xi . 64 ApplicationsGuide. . . . . . . . . . . . . . . . . . . .. xii . Commodore Information Network. . . . . . . . . . . . . . . . . . . . . .. xvii Commodore 1. BASIC PROGRAMMING RULES 1 . Introduction 2 . ScreenDisplay Codes (BASIC Character . Set) ............. The Operating System(OS) Programming Numbers and Variables Integer, Floating-Point and String Constants.. . . . . . . . . . Integer, Floating-Point and String Variables. . . . . . . . . . . Integer, Floating-Point and String Arrays 2 2 4 4 7 8 . Expressions and Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Arithmetic Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arithmetic Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Relational Operators 12 .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. ~3 Hierarchy of Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Stri ng Operations 16 Logical String Operators Expressions . Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 17 18 Techniques Data Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Using the INPUT Statement 18 Using the GET Statement. . . . . . . . . . . . . . . . . . . . . . . . . .. 22 How to Crunch BASIC Programs 24 2. BASIC LANGUAGE . Introduction . 10 VOCABULARY... . . . . . . . . . . . . . .. 29 30 BASIC Keywords, Abbreviations, and Function Types . Description of BASIC Keywords (Alphabetical) . The Commodore 64 Keyboard and Features . Screen Editor.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. iii 31 35 93 94 3. PROGRAMMING GRAPHICS ON THE ... COMMODORE64 · Graphics Overview Character Display 99 100 Modes . . . . . . . . . . . . . . . . . . . . . . . . . .. 100 Bit Map Modes Sprites 100 100 · GraphicsLocations 101 VideoBankSelection.. . . . . . .. ... . . ... . .. . . ... . .. .. 101 Screen Memory 102 ColorMemory .1.. . . .. . . .. . . .. 103 Character Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 103 Character 107 108 115 115 120 . Standard Character Mode. . . . . . . . . . . . . . . . . . . . . . . . . . .. 107 Definitions · Programmable Characters . Multi-Color Mode Graphics Multi-Color Mode Bit . Extended Background Color Mode . BitMappedGraphics... . ... . ... ... . . . .. . . ... . '" . ... 121 Standard High-Resolution Bit Map Mode. . ... . ... . 122 HowIt Works. . . .. . . ... ... . ... . . .. . . . . . . ... . .. 122 . SmoothScrolling . Sprites 128 131 . Multi-ColorBitMap Mode. . .. . .. . . . . . . . . . . .. . . .. . . ... 127 Defining a Sprite 131 Sprite Poi nters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 133 Turning Sprites On 134 Turning Sprites Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 135 Colors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 135 Multi-Color Mode 135 Setting a Sprite to Multi-Color Mode. . . . . . .. . . ... . ... Expanded Sprites Sprite Positioning Sprite PositioningSummary. . . . . . . . . . . . . . . . . . . . . . . .. SpriteDisplayPriorities.. . . . . . . . . . . . . . . . . . . . . . . . . .. Collision Detects . Other Graphics Features 136 136 137 143 144 144 150 Screen Blanking 150 Raster Reg ister . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 150 Interrupt Status Register. . . . . . . . . . . . . . . . . . . . . . . . . . .. 151 Suggested Screen and Character iv Color Combinations... 152 . Programming Sprites-Another Look Making Sprites in BASIC-A Short Program. Crunching Your Sprite Programs Positioning Sprites on the Screen Sprite Priorities ... . .. . . .. 153 153 156 157 .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 161 Drawing a Sprite 162 Creati ng a Sprite . . . Step by Step 163 Moving Your Sprite on the Screen. . .. . . .. . . .. . ... . . .. 165 VerticalScrolling.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. The Dancing Mouse-A 166 Sprite Program Example. . . . . .. 166 EasySpritemakingChart.. . ... . ... . . .. . ... . . ... 176 SpritemakingNotes. . . .. . . .. . ... . . .. . . .. . . .. . ... . .. 177 4. PROGRAMMING SOUND AND MUSIC ON YOUR COMMODORE 64 . 183 Introduction 184 Volume Control Frequencies of Sound 186 Waves. . UsingMultipleVoices... . . . . . . . . . . . . . . . . . . . . . . .. 186 .. . . ... . . .. . . ... . .. . Controlling Multiple Voices 187 191 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 192 Understanding Waveforms.... . ... . . . .. . ... 194 . The EnvelopeGenerator. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. Changing Waveforms 196 . Filtering 199 . Advanced Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . Synchronization and Ring Modulation 207 202 5. BASICTOMACHINELANGUAGE 209 . What is Machine Language? . .. . .. . . ... . . .. . . ... ... . .. 210 What Does Machine Look Like? . . . . . . . . . . . . . . . .. 211 Code . . . . . . . . . . .. 212 Simple Memory Map of the Commodore 64 The Registers Inside the 6510 Microprocessor How Do You Write Machine Language Programs? 64MON . . Hexadecimal Notation. ... . .. . . .. . . . . . 213 214 215 . . .. . ... .. 215 Your First Machine Language Instruction Writing Your First Program . Addressing Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. Zero Page The Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. v 218 220 221 221 222 . Indexing 223 223 224 Indirect Indexed Indexed Indirect Branches and Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . Subroutines 226 . Useful Tips for the Beginner 228 229 . Approaching a Large Task. . . . . . . . . . . . . . . . . . . . . . . . . . .. 230 . MCS6510 Microprocessor Instruction SetAlphabetic Sequence "'. . . . . . . . . . . . . . . . . . . . . . . . .. 232 Instruction Addressing Modes and . Related Execution Times. . . . . . . . . . . . . . . . . . . . . . . . .. Memory Management on the Commodore 64 254 . . . . . . . . . . . .. 260 . The KERNAL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 268 . KERNALPower-Up Activities. . . . . . . . . . . . . . . . . . . . . . . . . .. 269 How to Use the KERNAL. . . . . . . . . . . . . . . . . . . . . . . . . .. . User Callable KERNALRoutines Error Codes Using Machine Language From BASIC Where to Put Machine Language Routines.. How to Enter Machine Language 270 272 306 307 . . . . . . . . . .. 309 309 . Commodore 64 Memory Map ... . . . .. . ... .. 310 Commodore 64 Input/Output Assignments. . . . . . . . . . . . .. 320 6. INPUT/OUTPUT GUIDE... . . . . . . . . . . . . . . . . . . . . . . . . . .. . Introduction 335 336 . Output to the TV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 336 . Output to Other Devices. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 337 Output to Printer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 338 Output to Modem 339 WorkingWithCassetteTape. . . . . . . . . . . . . . . . . . . . . . .. 340 Data Storage on Floppy Diskettes. . . . . . . . . . . . . . . . . . .. . The Game . Ports 342 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 343 Paddles Light Pen RS-232 Interface Description General Outline Opening an RS-232 Channel Getting Data From an RS-232 Channel Sending Data to an RS-232 Channel Closing an RS-232 Data Channel Sample BASIC Programs. . . . . . . . . . . . . . . . . . . . . . . . . . .. vi 346 348 348 348 349 352 353 354 356 Receiver/Transmitter Zero-Page Buffer Base Location Pointers 357 Memory Locations and Usage for RS-232 System Interface. . . . . . . . . . . . . . . . . . . . . .. Nonzero-Page 358 Memory Locations and Usage for RS-232 System Interface.. . . . . . . . . . . . . . . . . . . . .. 358 . The User Port. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 359 Port Pin Description 359 . The Serial Bus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 362 . Serial Bus Pinouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 363 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 366 . Z-80MicroprocessorCartridge. . . . . . . . . . . . . . . . . . . . . . . .. 368 The Expansion Port . Using Commodore CP/M@ . . . . . . . . . . . . . . . . . . . . . . . . .. 369 Running Commodore CP/M@ . . . . . . . . . . . . . . . . . . . . . . . .. 369 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. A. Abbreviations for BASICKeywords. . . . . . . . . . . . . . . . . . .. APPENDICES 373 374 B. Screen Display Codes 376 C. D. E. F. ASCIIand CHR$ Codes. . . . . . . . . . . . . . . . . . . . . . . . . . . .. Screen and Color MemoryMaps. . . . . . . . . . . . . . . . . . . . .. Music Note Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 379 382 384 388 G. VIC Chip Register Map H. Deriving Mathematical Functions 391 394 I. Pinouts for Input/Output Devices. . . . . . . . . . . . . . . . . . . . .. 395 J. Converting Standard BASIC Programs to Commodore 64 BASIC 398 K. Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 400 L. 6510 Microprocessol Chip Specifications M. 6526 Complex Interface Adapter (CIA) 402 Chip Specifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. N. 6566/6567 (VIC-II)Chip Specifications. . . . . . . . . . . . . . . .. 419 O. P. 6581 Sound Interface Device (SID) Chip Specifications. Glossary 436 .. 457 482 INDEX 483 COMMODORE 64 QUICK REFERENCE CARD 487 SCHEMATIC DIAGRAM OF THE COMMODORE 64 . . . .. 491 vii INTRODUCTION The COMMODORE64 PROGRAMMER'SREFERENCEGUIDE has been developed as a working tool and reference source for those of you who want to maximize your use of the built-in capabilities of your COMMODORE64. This manual contains the information you need for your programs, from the simplest example all the way to the most complex. The PROGRAMMER'SREFERENCEGUIDE is designed so that everyone from the beginning BASIC programmer to the professional experienced in 6502 machine language can get information to develop his or her own creative programs. At the same time this book shows you how clever your COMMODORE 64 really is. This REFERENCEGUIDE is not designed to teach the BASIC programming language or the 6502 machine language. There is, however, an extensive glossary of terms and a "semi-tutorial" approach to many of the sections in the book. If you don't already have a working knowledge of BASIC and how to use it to program, we suggest that you study the COMMODORE 64 USER'SGUIDE that came with your computer. The USER'SGUIDE gives you an easy to read introduction to the BASICprogramming language. If you still have difficulty understanding how to use BASIC then turn to the back of this book (or Appendix N in the USER'S GUIDE) and check out the Bibliography. The COMMODORE 64 PROGRAMMER'SREFERENCEGUIDE is just that; a reference. Like most reference books, your ability to apply the information creatively really depends on how much knowledge you have about the subject. In other words if you are a novice programmer you will not be able to use all the facts and figures in this book until you expand your current programming knowledge. ix What you can do with this book is to find a considerable amount of valuable programming reference information written in easy to read, plain English with the programmer's jargon explained. On the other hand the programming professional will find all the information needed to use the capabilities of the COMMODORE64 effectively. WHAT'S INCLUDED? . Our complete "BASIC dictionary" includes Commodore BASIC language commands, statements and functions listed in alphabetical order. We've created a "quick list" which contains all the words and their abbreviations. This is followed by a section containing a more detailed definition of each word along with sample BASIC programs to illustrate how they work. . If you need an introduction to using machine language with BASIC programs our layman's overview will get you started. . A powerful feature of all Commodore computers is called the KERNAL. It helps insure that the programs you write today can also be used on your Commodore computer of tomorrow. . The Input/Output Programming section gives you the opportunity to use your computer to the limit. It describes how to hook-up and use everything from lightpens and joysticks to disk drives, printers, and telecommunication devices called modems. You can explore the world of SPRITES,programmable characters, and high resolution graphics for the most detailed and advanced animated pictures in the microcomputer industry. . You can also enter the world of music synthesis and create your own songs and sound effects with the best built-in synthesizer available in any personal computer. . If you're an experienced programmer, the soft load language section gives you information about the COMMODORE64's ability to run CP/M* and high level languages. This is in addition to BASIC. . Think of your COMMODORE64 PROGRAMMER'SREFERENCEGUIDE as a useful tool to help you and you will enjoy the hours of programming ahead of you. .CP/M is a registered x INTRODUCTION trademark of Digital Research, Inc. HOW TO USE THIS REFERENCEGUIDE Throughout this manual certain conventional notations are used to describe the syntax (programming sentence structure) of BASIC commands or statements and to show both the required and optional parts of each BASIC keyword. The rules to use for interpreting statement syntax are as follows: 1. BASIC keywords are shown in capital letters. They must appear where shown in the statement, entered and spelled exactly as shown. 2. Items shown within quotation marks (" ") indicate variable data which you must put in. Both the quotation marks and the data inside the quotes must appear where shown in each statement. 3. Items inside the square brackets ([ ]) indicate an optional statement parameter. A parameter is a limitation or additional qualifier for your statements. If you use an optional parameter you must supply the data for that optional parameter. In addition, ellipses ( . . . ) show that an optional item can be repeated as many times as a programming line allows. 4. If an item in the square brackets ([ ]) is UNDERLINED,that means that you MUST use those certain characters in the optional parameters, and they also have to be spelled exactly as shown. 5. Items inside angle brackets «» indicate variable data which you provide. While the slash ( / ) indicates that you must make a choice between two mutually exclusive options. EXAMPLE OF SYNTAX FORMAT: OPEN<file-num> ,<device> name>] [,<mode>]" [,<address>], ["<drive>: <file- EXAMPLES OF ACTUAL STATEMENTS: 10 OPEN 2,8,6,"0:STOCK FOLlO,S,W" 20 OPEN 1,1 ,2,"CHECKBOOK" 30 OPEN 3,4 When you actually apply the syntax conventions in a practical situation, the sequence of parameters in your statements might not be exactly the same as the sequence shown in syntax examples. The examples are not meant to show every possible sequence. They are intended to present all required and optional parameters. INTRODUCTION xi Programming examples in this book are shown with blanks separating words and operators for the sake of readability. Normally though, BASIC doesn't require blanks between words unless leaving them out would give you an ambiguous or incorrect syntax. Shown below are some examples and descriptions of the symbols used for various statement parameters in the following chapters. The list is not meant to show every possibility, but to give you a better understanding as to how syntax examples are presented. SYMBOL <file-num> <device> <address> EXAMPLE 50 4 15 <drive> <file-name> <constant> 0 " ABCDEFG" <variable> X145 <string> <number> AB$ 12345 <line-number> <numeric> 1000 1.5E4 "TEST. DATA" DESCRIPTION A logical file number A hardware device number A serial bus secondary device address number A physical disk drive number The name of a data or program file Literal data supplied by the programmer Any BASIC data variable name or constant Use of a string type variable required Use of a numeric type variable required An actual program line number An integer or floating-point variable COMMODORE 64 APPLICATIONS GUIDE When you first thought about buying a computer you probably asked yourself, "Now that I can afford to buy a computer, what can I do with it once I get one?" The great thing about your COMMODORE 64 is that you can make it do what YOU want it to do! You can make it calculate and keep track of home and business budget needs. You can use it for word processing. You can make it play arcade-style action games. You can make it sing. Youcan even create your own animated cartoons, and more. The best part of owning a COMMODORE 64 is that even if it did only one of the things listed below it would be well worth the price you paid for it. But the 64 is a complete computer and it does do EVERYTHINGlisted and then some! xii INTRODUCTION By the way, in addition to everything here you can pick up a lot of other creative and practical ideas by signing up with a local Commodore Users' Club, subscribing to the COMMODORE and POWER/PLAY magazines, and joining the COMMODOREINFORMATIONNETWORKon CompuServe™ . COMMENTS/REQUIREMENTS APPLICATION ACTION PACKED GAMES You can get real Bally Midway arcade games like Omega Race, Gorf and Wizard of Wor, as well as "play and learn" games like Math Teacher I, Home Babysitter and Commodore Artist. ADVERTISING & MERCHANDISING Hook your COMMODORE64 to a TV, put it in a store window with a flashing, animated, and musical message and you've got a great point of purchase store display. ANIMATION Commodore's Sprite Graphics allow you to create real cartoons with 8 different levels so that shapes can move in front of or behind each other. BABYSITTING The COMMODORE 64 HOME BABYSITTER cartridge can keep your youngest child occupied for hours and teach alphabet/ keyboard recognition at the same time. It also teaches special learning concepts and relationships. BASIC PROGRAMMING Your COMMODORE 64 USER'SGUIDE and the TEACH YOURSELF PROGRAMMING series of books and tapes offer an excellent starting point. BUSINESS SPREADSHEET The COMMODORE 64 offers the "Easy" series of business aids including the most powerful word processor and largest spreadsheet available for any personal computer. COMMUNICATION Enter the fascinating world of computer "networking." If you hook a VICMODEM to your COMMODORE 64 you can communicate with other computer owners all around the world. INTRODUCTION xiii Not only that, if you join the COMMODORE INFORMATIONNETWORK on CompuServe™ you can get the latest news and updates on all Commodore products, financial information, shop at home services, you can even play games with the friends you make through the information systems you join. COMPOSING SONGS The COMMODORE 64 is equipped with the most sophisticated built-in music synthesizer available on any computer. It has three completely programmable voices, nine full music octaves, and four controllable waveforms. look for Commodore Music Cartridges and Commodore Music books to help you create or reproduce all kinds of music and sound effects. CP/M* Commodore offers a CP/M* add-on and access to software through an easy-to-Ioad cartridge. DEXTERITY TRAINING Hand/Eye coordination are aided by several including "Jupiter simulation. and manual Commodore lander" dexterity games ... and night driving EDUCATION While working with a computer is an education in itself, The COMMODORE Educational Resource Book contains general information on the educational uses of computers. We also have a variety of learning cartridges designed to teach everything from music to math and art to astronomy. FOREIGN LANGUAGE The COMMODORE 64 programmable character set lets you replace the standard character set with user defined foreign language characters. GRAPHICS AND ART In addition to the Sprite Graphics mentioned above, the COMMODORE 64 offers highresolution, multi-color graphics plotting, pro- .CP/M is a Registered trademark of Digital Research, xiv INTRODUCTION Inc. grammable characters, and combinations of all the different graphics and character display modes. INSTRUMENT CONTROl Your COMMODORE 64 has a serial port, RS-232 port and a user port for use with a variety of special industrial applications. An IEEE/488 cartridge is also available as an optional extra. JOURNALS AND CREATIVE WRITING The COMMODORE 64 will soon offer an exceptional word processing system that matches or exceeds the qualities and flexibilities of most "high-priced" word processors available. Of course you can save the information on either a 1541 Disk Drive or a Datassette TM recorder and have it printed out using a VICPRINTERor PLOTTER. LlGHTPEN CONTROL Applications requiring the use of a lightpen can be performed by any lightpen that will fit the COMMODORE 64 game port connector. MACHINE CODE PROGRAMMING Your COMMODORE 64 PROGRAMMER'S REFERENCE GUIDE includes a machine language section, as well as a BASIC to machine code interface section. There's even a bibliography available for more in-depth study. PAYROLL & FORMS The COMMODORE 64 can be programmed to handle a variety of entry-type business applications. Upper~lower case letters combined with C64 "business form" graphics make it easy for you to design forms which can then be printed on your printer. PRINTOUT PRINTING The COMMODORE 64 interfaces with a variety of dot matrix and letter quality printers as well as plotters. RECIPES You can store your favorite recipes on your COMMODORE 64 and its disk or cassette storage unit, and end the need for messy recipe cards that often get lost when you need them most. INTRODUCTION xv SIMULATIONS Computer simulations let you conduct dangerous or expensive experiments at minimum risk and cost. SPORTS DATA The Source™ and CompuServe™ both offer sports information which you can get using your COMMODORE64 and a VICMODEM. STOCK QUOTES With a VICMODEM and a subscription to any of the appropriate network services, your COMMODORE64 becomes your own private stock ticker. These are just a few of the many applications for you and your COMMODORE64. As you can see, for work or play, at home, in school or the office, your COMMODORE64 gives you a practical solution for just about any need. Commodore wants you to know that our support for users only STARTS with your purchase of a Commodore computer. That's why we've created two publications with Commodore information from around the world, and a "two-way" computer information network with valuable input for users in the U.S. and Canada from coast to coast. In addition, we wholeheartedly encourage and support the growth of Commodore Users' Clubs around the world. They are an excellent source of information for every Commodore computer owner from the beginner to the most advanced. The magazines dnd network, which are more fully described below, have the most up-to-date information about how to get involved with the Users' Club in your area. Finally, your local Commodore dealer is a useful source of Commodore support and information. POWER/PLAY The Home Computer Magazine When it comes to entertainment, learning at home and practical home applications, POWER/PLAYis THEprime source of information for Commodore home users. Find out wh,ere your nearest user clubs are and what they're doing, learn about software, games, programming techniques, telecommunications, and new products. POWER/PLAYis your personal connection to other Commodore users, outside software and hardware developers, and to Commodore itself. Published quarterly. Only $10.00 for a year of home computing excitement. xvi INTRODUCTION COMMODORE The Microcomputer Magazine Widely read by educators, businessmen and students, as well as home computerists, COMMODORE Magazine is our main vehicle for sharing exclusive information on the more technical use of Commodore systems. Regular departments cover business, science and education, programming tips, "excerpts from a technical notebook," and many other features of interest to anyone who uses or is thinking about purchasing Commodore equipment for business, scientific or educational applications. COMMODOREis the ideal complement to POWER/ PLAY. Published bi-monthly. Subscription price: $15.00 per year. ... AND FOR EVEN MORE INFORMATION DIAL UP OUR PAPERLESS USER MAGAZINE ... COMMODORE INFORMATION NETWORK The magazine of the future is here. To supplement and enhance your subscription to POWER/PLAYand COMMODOREmagazines, the COMMODORE INFORMATIONNETWORK-our "paperless magazine"-is available now over the telephone using your Commodore computer and modem. Join our computer club, get help with a computing problem, "talk" to other Commodore friends, or get up-to-the-minute information on new products, software and educational resources. Soon you will even be able to save yourself the trouble of typing in the program listings you find in POWER/PLAYor COMMODORE by downloading direct from the Information Network (a new user service planned for early 1983). The best part is that most of the answers are there before you even ask the questions. (How's that for service?) To call our electronic magazine you need only a modem and a subscription to CompuServe™, one of the nation's largest telecommunications networks. (To make it easy for you Commodore includes a FREE year's subscription to CompuServe™ in each VICMODEM package.) Just dial your local number for the CompuServe™ data bank and connect your phone to the modem. When the CompuServe™ video text appears on your screen type G CBM on your computer keyboard. When the COMMODORE INFORMATION NETWORK'Stable of contents, or "menu," appears on your screen choose from one of our sixteen departments, make yourself comfortable, and enjoy the paperless magazine other magazines are writing about. INTRODUCTION xvii For more information, visit your Commodore dealer or contact CompuServe™ customer service at 800-848-8990 (in Ohio, 614-457-8600). COMMODORE INFORMATION NETWORK Main Menu Description Direct Access Codes Commodore Educational Special Commands User Questions Public Bulletin Board User Groups Descriptions Questions and Magazines and Newsletters Products Announced Software Technical Directory Commodore xviii News INTRODUCTION Direct Dealers Resources Answers Tips Tips Descriptions INTRODUCTION This chapter talks about how BASIC stores and manipulates topics include: data. The 1) A brief mention of the operating system components and functions as well as the character set used in the Commodore 64. 2) The formation of constants and variables. What types of variables there are. And how constants and variables are stored in memory. 3) The rules for arithmetic calculations, relationship tests, string handling, and logical operations. Also included are the rules for forming expressions, and the data conversions using BASIC with mixed data types. necessary when you're SCREEN DISPLAY CODES (BASIC CHARACTER SET) THE OPERATING SYSTEM (OS) The Operating System is contained in the Read Only Memory (ROM) chips and is a combination of three separate, but interrelated, program modules. 1) The BASIC Interpreter 2) The KERNAL 3) The Screen Editor 1) The BASIC Interpreter is responsible for analyzing BASIC statement syntax and for performing the required calculations and/or data manipulation. The BASIC Interpreter has a vocabulary of 65 "keywords" which have special meanings. The upper and lower case alphabet and the digits 0-9 are used to make both keywords and variable names. Certain punctuation characters and special symbols also have meanings for the Interpreter. Table 1-1 lists tl1e special characters and their uses. 2) The KERNALhandles most of the interrupt level processing in the system (for details on interrupt level processing, see Chapter 5). The KERNAL also does the actual input and output of data. 3) The Screen Editor controls the output to the video screen (television set) and the editing of BASIC program text. In addition, the Screen Editor intercepts keyboard input so that it can decide whether the 2 BASIC PROGRAMMING RULES Table 1-1. CBM BASICCharacter Set NAMEand DESCRIPTION CHARACTER ; = + * / t ( ) BLANK-separates keywords and variable names SEMI-COLON-used in variable lists to format output EQUAL SIGN-value assignment and relationship testing PLUSSIGN-arithmetic addition or string concatenation (concatenation: linking together in a chain) MINUS SIGN-arithmetic subtraction, unar{. minus (-1) ASTERISK-arithmetic multiplication SLASH-arithmetic division UP ARROW-arithmetic exponentiation LEFTPARENTHESIS-expression evaluatiorl and functions RIGHT PARENTHESIS-expression evaluatibn and functions % # $ If ? < > 7T PERCENT-declares variable name as ani integer NUMBER-comes before logical file num~er in input/ output statements DOLLARSIGN-declares variable name s a string COMMA-used in variable lists to forma output; also separates command para eters PERIOD-decimal point in floating point onstants QUOTATION MARK-encloses string cons ants COLON-separates multiple BASICstatem nts in a line QUESTION MARK-abbreviation for the ke ord PRINT LESSTHAN-used in relationship tests GREATERTHAN-used in relationship test PI-the numeric constant 3.141592654 characters put in should be acted upon immediatelyl to the BASIC Interpreter. The Operating or passed on System gives you two modes of BASIC loperation: 1) DIRECTMode 2) PROGRAM Mode 1) When you're using the DIRECTmode, BASIC statem~nts don't have line numbers in front of the statement. They are executed whenever the .:~IIIIII:~/. key is pressed. 2) The PROGRAM mode is the one you use for running programs. BASIC PROGRAMMING RULES 3 When using the PROGRAM mode, all of your BASIC statements must have line numbers in front of them. You can have more than one BASIC statement in a line of your program, but the number of statements by the fact only put 80 char- acters is limited on a logical screen that you can that if you are going to go over the 80 character limit you have to put the entire BASIC statement that doesn't fit on a new line with a new line number. NOTE: Always type NEW and hit line. This means DI!IiIII before starting a new program. The Commodore 64 has two complete character sets that you can use either from the keyboard or in your programs. In SET 1, the upper case alphabet and the numbers 0-9 are available without pressing the IDIIiI key. If you hold down the IDIIiI key while typing, the graphics characters on the RIGHT side of the front of the keys are used. If you hold down the [i key while typing, the graphics characters on the LEFTside of the front of the key are used. Holding down the IDIIiI key while typing any character that doesn't have graphic symbols on the front of the key gives you the symbol on the top most part of the key. In SET2, the lower case alphabet and the numbers 0-9 are available without pressing the IDIIiI key. The upper case alphabet is available when you hold down the IDIIiI key while typing. Again, the graphic symbols on the LEFTside of the front of the keys are displayed by presskey, while the symbols on the top most part of any key ing the [i without graphics characters are selected when you hold down the IDIIiI key while typing. To switch from one character set to the other press the [i and the IDIIiI keys together. PROGRAMMING NUMBERS AND VARIABLES INTEGER, FLOATING.POINT AND STRING CONSTANTS Constants are the data values that you put in your BASIC statements. BASIC uses these values to represent CBM BASIC can recognize and data during statement manipulate three types 1) INTEGER NUMBERS 2) FLOATING-POINT NUMBERS 3) STRINGS 4 BASIC PROGRAMMING RULES execution. of constants: Integer constants are whole numbers (numbers without decimal points). Integer constants must be between -32768 and +32767. Integer constants do not have decimal points or commas between digits. If the plus (+) sign is left out, the constant is assumed to be a positive number. Zeros coming before a constant are ignored and shouldn't be used since they waste memory and slow down your program. However, they won't cause an error. Integers are stored in memory as two-byte binary numbers. Some examples of integer constants are: -12 8765 -32768 +44 o -32767 NOTE: Do NOT put commas inside any number. For example, always type 32,000 as 32000. If you put a comma in the middle of a number you will get the BASIC error message ?SYNTAX ERROR. Floating-point constants are positive or negative numbers and can contain fractions. Fractional ports of a number may be shown using a decimal point. Once again remember that commas are NOT used between numbers. If the plus sign (+) is left off the front of a number, the Commodore 64 assumes that the number is positive. If you leave off the decimal point the computer will assume that it follows the last digit of the number. And as with integers, zeros that come before a constant are ignored. Floating-point constants can be used in two ways: 1) SIMPLE NUMBER 2) SCIENTIFIC NOTATION Floating-point constants will show you up to nine digits on your screen. These digits can represent values between -999999999. and +999999999. If you enter more than nine digits the number will be rounded based on the tenth digit. If the tenth digit is greater than or equal to 5 the number will be rounded upward. Less than 5 the number will be rounded downward. This could be important to the final totals of some numbers you may want to work with. Floating-point numbers are stored (using five bytes of memory) and are manipulated in calculations with ten places of accuracy. However, BASIC PROGRAMMING RULES 5 the numbers are rounded to nine digits when results are printed. Some examples of simple floating-point numbers are: 1.23 -.998877 +3.1459 .7777777 -333. .01 Numbers smaller than .01 or larger than 999999999. willbe printed in scientific notation. In scientific notation a floating-point constant is made up of three parts: 1) THE MANTISSA 2) THE LETTER E 3) THE EXPONENT The mantissa is a simple floating-point number. The letter E is used to tell you that you're seeing the number in exponential form. In other words E represents *10 (eg., 3E3=3*10j3=3000). And the exponent is what multiplication power of 10 the number is raised to. Both the mantissa and the exponent are signed (+ or -) numbers. The exponent's range is from -39 to +38 and it indicates the number of places that the actual decimal point in the mantissa would be moved to the left (-) or right (+) if the value of the constant were represented as a simple number. There is a limit to the size of floating-point numbers that BASIC can handle, even in scientific notation: the largest number is +1.70141183E+38 and calculations which would result in a larger number will display the BASIC error message ?OVERFLOWERROR. The smallest floating-point number is +2.93873588E-39 and calculations which result in a smaller value give you zero as an answer and NO error message. Some examples of floating-point numbers in scientific notation (and their decimal values) are: 235.988E-3 2359E6 -7.09E-12 -3.14159E+5 (.235988) (2359000000.) (-.00000000000709) (-314159.) String constants are groups of alphanumeric information like letters, numbers and symbols. When you enter a string from the keyboard, it can have any length up to the space available in an 80-character line 6 BASIC PROGRAMMING RULES (that is, any character spaces NOT taken up by the line number and other required parts of the statement). A string constant can contain blanks, letters, numbers, punctuation and color or cursor control characters in any combination. You can even put commas between numbers. The only character which cannot be included in a string is the double quote mark ("). This is because the double quote mark is used to define the beginning and end of the string. A string can also have a null value-which means that it can contain no character data. You can leave the ending quote mark off of a string if it's the last item on a line or if it's followed by a colon (:). Some examples of string constants are: ( a null string) "HELLO" "$25,000.00" "NUMBER OF EMPLOYEES" NOTE: Use CHR$(34) to include quotes (") in strings. INTEGER, FLOATlNG.POINT AND STRING VARIABLES Variables are names that represent data values used in your BASIC statements. The value represented by a variable can be assigned by setting it equal to a constant, or it can be the result of calculations in the program. Variable data, like constants, can be integers, floating-point numbers, or strings. If you refer to a variable name in a program before a value has been assigned, the BASIC Interpreter will automatically create the variable with a value of zero if it's an integer or floating-point number. Or it will create a variable with a null value if you're using strings. Variable names can be any length but only the first two characters are considered significant in CBM BASIC. This means that all names used for variables must NOT have the same first two characters. Variable names may NOT be the same as BASIC keywords and they may NOT contain keywords ;n the middle of variable names. Keywords include all BASIC commands, statements, function names and logical operator names. If you accidentally use a keyword in the middle of a variable name, the BASIC error message ?SYNTAXERRORwill show up on your screen. The characters used to form variable names are the alphabet and the numbers 0-9. The first character of the name must be a letter. Data BASIC PROGRAMMING RULES 7 type declaration characters (%) and ($) can be used as the last character of the name. The percent sign (%) declares the variable to be an integer and the dollar sign ($) declares a string variable. If no type declaration character is used the Interpreter will assume that the variable is a floating-point. Some examples of variable names, value assignments and data types are: A$="GROSS SALES" MTH$="JAN"+A$ K%=5 CNT% =CNT% + 1 FP=12.5 SUM=FP*CNT% (string variable) (string variable) (integer variable) (integer variable) (floating-point variable) (floating-point variable) INTEGER, FLOATING-POINT AND STRING ARRAYS An array is a table (or list) of associated data items referred to by a single variable name. In other words, an array is a sequence of related variables. A table of numbers can be seen as an array, for example. The individual numbers within the table become "elements" of the arr