Gw basic pdf manual
The first characters are rigidly defined. Some computers such as the IBM PC define an additional set for the numbers but these are not standardized. In the IBM system these include the symbols for creating boxes and forms.
Thus the upper case 'A' is Most confusion comes with numbers. ASCII 49 is the character '1'. Later we'll see ways of using this numeric feature in sorting and alphabetizing. Another option would be to write a short Basic program that prints all values. Try writing a program that will format this into 8 columns. Thus the first line would have the 0, 32, 64, 96, You can do this with either one loop and a long print statement or nested loops and a single print statement.
Remember that a semicolon ; after a print line will prevent a skip to the next line. If you use this method, you'll need to have a separate print statement to shift to the next line. Now we're ready to draw a box: 1 ' drawbox1. These are ascii values and We next ask for row and column coordinates.
The program draws the upper left corner, a row of horizontal characters and the upper right corner. For each intermediate line, we draw a vertical section, spaces and another vertical. We finish with the 2 lower corners connected by another horizontal line. Allow user to draw several boxes without erasing them You'll need to keep the input on several lines, eg, lines 21 and Clear each line before prompting for new data.
Find the characters that can be used to make a single lined box and add them to the program. Use a 2 dimensional array. More involved: This box is drawn from the top down. An alternative would be to draw it as if a pen were sketching it. Rewrite the program to draw the first line, then drop down the right hand side, come back along the bottom right to left, then draw up to the original corner. For the sides you'll need to recalculate the x,y position each time.
We've also managed to review many of the constructs that we covered in previous sessions. In the next installments we'll refine these ideas as we start considering how to build larger programs in a structured manner. Calculate the number of days elapsed since the start of the year and the number of days till the end of the year.
Up to now, when we've wanted to repeat a section of code, we've had 2 choices. We could just copy the code over, or we could set up a loop. Sometimes that still leaves us with a messy solution.
But what if we want to input 2 sets of numbers? One way would be to repeat the code. This is a special command that tells the program to jump to a particular line. This lets a whole section of code be used in several places. Note the use of the END statement. Take it out and see what happens. When working with dates for comparison you'll usually want to deal with the number of days past the beginning of the year, so this conversion routine is quite handy.
This notation for dates is called Julian as in Caesar. This is more of a programmer's problem though, not the language's. You can write structured programs in Basic just as you can write spaghetti code in Pascal or unmaintainable trash in C. Structured methods will produce good code in any of the languages.
If you follow these guidelines you'll be able to keep your programs readable and easy to maintain. In older books or articles you may see the suggestion to jump to the end of the program for initializing or other reasons. This made a small amount of sense in the old days, but modern Basic interpreters are much more efficient and you won't notice any difference. What you will get is a program that's harder to work on. Functions are even more transportable. By applying these two rules you abide by one of the central dogmas of structured programming -- single input, single output.
This ensures that every time the subroutine is used it's used the same. Thus you won't get unpredictable results. Sometimes you'll have gosubs that finish in the middle of a section. This is an insignificant increase in the amount of code, but results in an immense increase in readability and ease of maintainence.
It also makes it easier to trace and isolate problems. If you know there's only one entry and one exit, you can concentrate on the routine that's causing the problem. You won't have to worry what conditions are where the gosub is called.
If there were multiple entries or exits this would be an additional problem. GOSUB END ' subroutine to do stuff It's much to be preferred to flowcharts. Here we've outline the structure of our entire program without getting bogged down in petty details. Another good feature of this design is that we can prototype. We could write the initializing section and the final processing section without worrying for the moment about the 2 middle sections.
We could just put some print statements in there to alert us to the fact they need to come later. This method, called Top-Down Design starts with the most important elements of the program, the overall structure and works in ever more detail. In this way, the interactions among the program parts is being tested from the start.
Try this method with simple programs and you'll find that soon you can tackle more complicated projects. If you're interested in more on structured programming, look for books by Constantine or Yourdon. Most of the ideas in these books can be applied to any language. This feature alone might be enough to convince you to acquire a compiler.
It's especially useful since it takes care to maintain any references you've set up. The numbers may be different. This is the preferred way of changing numbers. You can do it by hand, but if you miss one, it'll be hard to find. If you have a large file and want to reorder just part you can use RENUM [newnum], [oldnum] This will start at line [oldnum] and change it to [newnum] and continue renumbering from there.
Try this on a practise file until you feel comfortable with the power of this command. There are 2 philosophies about renumbering. As long as you keep current listings, there's no problem.
Others design a program with large separations between gosubs. Thus you might define them as , , , This method keeps the subroutines in the same place, but is more tedious to renumber. The first method is certainly the easiest, and if you're careful to follow the guidelines above your program will still be readable. Design a program that will take 2 dates, prompting for year, month and day.
Then calculate the number of days that separate these two dates. Hint: in our first treatment we ignored leap years. But when you know the year, and are looking over several years you don't have that luxury. You'll need to include a check for leap year in the new elapsed days gosub. The difference is that functions are defined at the start of a program, and, in interpreted Basic, are limited to single lines.
The other difference is that there can only be one value returned from a function. You can't use a function on the left hand side though. Actually we've already used several functions. These are system functions since they come as part of the language. Why don't we print characters 10 to 12? Try taking these out and see why. These allow us to process parts of strings. The length to be used is given as the second parameter.
A final function for now is LEN. This returns the length of a string. Write a gosub that strips multiple blanks from a string, reducing them to single blanks. Strip all trailing blanks. Writing your own functions is straightforward.
Any other variables used in the function take the value of the current state of that variable. The values X and are sent to replace a and b. This can cause strange happenings in your programs. It's legal, but not recommended as good technique. The function first takes the maximum of X or min, then takes the minimum of x or max. Use this in programs like the checkbook program where you can define the expected maximum and minimum values.
Function writing is some of the most fun in Basic. You can be quite creative. While normally obscure tricks are frowned upon, in functions, they're almost okay, as long as you comment them. Once they work, you can use them in many different places. Another advantage, is that, with proper naming conventions your mainline code will be self-documenting.
Which of the following is easier to understand? Comment it. Then put it away and call it when needed. Put a space before and after the name. Even if you don't do the actual programming, it would be worthwhile to design a prototype on paper so that you understand the methods involved. Start with the modified totals program from last time which shows 6 products over 12 months. Write a 2 functions that take as input the I,J coordinates of the array X and return the screen row and column where that element should be printed.
Use a gosub to prompt on lines 23 and 24 for the row and col to update. Check that the row and columns are valid. Give an error message if they're not. Don't allow entry of 0's as we're going to calculate them. Once you have the new row and column, prompt and get a new value, then recalculate the totals and use the functions to redisplay only the fields that have changed. At this point, you've learned enough about Basic to write useful programs.
However, we still have no way of preserving the results of a program. What if we want to keep results from one run to another. The solution is the use of files. We've already used files to save our Basic programs. When you do a normal save from within Basic, only the Basic interpreter can use it. Then exit Basic and use DIR to look at the sizes of the programs. Notice that the ascii cersion takes up more room. If you use an editor, though, you'll be responsible for putting in your own line numbers.
Other common uses of files are to hold data that must remain when the computer is turned off, or to handle large quantities of data. Basic provides 2 ways to handle files -- sequential and random access. Don't worry about the details. The important thing is to understand the distinctions between the 2 types of files and when each is appropriate.
All files contain records. Records are the repeating elements within a file. They in turn are usually broken down into fields. For example, a file record for a mailing list program might have fields with the name, address and phone number of each person. Sequential and random files handle records and fields quite differently.
Each has definite advantages and drawbacks. Sequential files read or write from the start of the file and proceed in an orderly fashion to the end of the file. Think of them as a cassette tapes. In order to find out what's in the tenth record, we need to read the preceding 9 records. We may not do anything with the information we read, but we have to read it.
Random files give you immediate access to any record in the file. A jukebox with its dozens of records is a good example.
When you request a record, the mechanical arm goes directly to the record you requested and plays it.
In computer programs, sequential files must always be accessed from the beginning, while random files can select any record at any time. Any additional structure is our logical description for convenience and understanding. We'll set up a simple data record and see how the two types of files deal with it.
Our data consists of the following fields: name city age phone The following program asks for the information to make 3 records, then stores them to a file. It then reads that file and displays the results on the screen. Ignore the actual commands for the moment, and concentrate on what the program is trying to do. First we'll do it sequentially. BAS We read the name, city, age and phone number lines , then write them to the file lines When we're done, the file physically looks like this: Note that each record takes up a variable amount of space in the file.
Thus we have no way of predicting where a particular record begins. If we start at the beginning and just read one record, field by field that never concerns us. Sequential files are thus most useful when we have either very short files, or when we know we'll always want to read the entire file into memory. Their main advantage is that they're easy to program and maintain.
We'll look at the organization of a random file, then return to see how to program them. BAS Again, we read the name, city, age and phone number, and write them to the file. This time, the file will physically look like this: name Each record consists of 48 bytes or characters.
The information is stored exactly the same for each record. Thus if we want to find the third record, we know that it begins on the 97th byte. There's no need to look at the intervening information.
We can point directly to the start of the record and read it. Random access files are a bit more complex than sequential, and take more programming effort to maintain, but they are much more flexible. They're best suited to cases where data will be required in no particular order. Random files are also preferred for large files that are frequently updated. Consider, if you have records and change 10 of them, a sequential file makes you read all , make the changes, then write all again.
That's disk reads and writes. With a random file, you only need 10 reads and 10 writes. The first is the control of files. You can have several files open at one time. For example, you might have a customer file, an invoice file and a pricing file. Basic distinguishes among them by assigning a number to each one. The sign in front of the file number is optional, but recommended to distinguish it as an identifier rather than a numeric value. OPEN is used by both types of files, but has a different syntax for each.
CLOSE is used when you're finished with a file. When you leave a Basic program, all files will be automatically closed, but it's good practise to have your program do it. In either case, an invisible pointer is maintained that indicates where the next record is coming from. Usually existing files are read first, new information added, then the entire file is written out.
Any existing file is kept and new information is added to the end of the file. An example might be a file that keeps a list of errors encountered during the program. There's no need to read in previous errors, but you also don't want to destroy them. Since the filename can be a variable, you can make your programs more user- friendly by showing the user what files are available.
For example, if you have a series of files that are called MAR. INPUT reads the requested variables from the given file. You have to be careful that the variable types match.
Ie, you can't read a string into an integer variable. PRINT has several problems for beginners, mostly related to how it formats the fields before writing. The Basic manual describes the problems and their solutions if you're in a masochistic mood. It encloses each string in quotation marks and separates fields on a line by commas.
In this example, we've made it even simpler by writing each field separately. DAT each field will be on a separate line. This uses an extra byte per line, but makes data files easier to display and programs easier to debug. After assigning a number, they also require a length for each record.
This command defines the length of each field in a record. You should be careful to use different variable names for the field elements and for your actual data. There are some places where Basic lets you use the same name, but you're taking an unncessary risk of causing bugs. Since the field statement defines the length of each field, you have to do a little more planning with random files.
In the sequential file, we never had to consider the length of a name, or whether a phone number had an area code attached. Here, we make the conscious decision that names will be only 20 characters, and that phone numbers will not have area codes. A special case is the storage of numbers. All items in random files are stored in a special format.
For numbers, this means all integers are stored as 2 bytes. Even if the actual number is 4 or 5 digits, it's compressed before storage using the functions defined below. Thus random files are often much smaller than sequential files. The next difference is seen in the method used to write a record. First all fields are determined. Then, each element of the field statement is set up using the LSET statement. For strings, this is just an assignment: LSET field. Then we write this record with the statement PUT 1, rec.
To read a record, we need only it's position. That's it! Now we have 2 methods for saving data and several ways to manipulate the files. For the following, write down what you think will happen before trying it with the programs.
Eg, what occurs if you enter a 3 or 4 digit age? When you finish these you'll have a good working understanding of Basic files. Sequential files Use the earlier checkbook programs to create a checkbook file. This should use the following records: check number description amount At the start of the program, you'll need to read in the starting balance. At the end, you'll have the ending balance. This would just read the file and print a report. You should have the deposits and debits in two different columns.
Random files Change the earlier name and address example to allow updates and additions. In outline form: open "test2. What happens if you read a record beyond the end of the file?
What if you write past the end? If you don't have the time to do the entire program, implement only one part of it. Put in the prompting for the other sections anyway. If those sections are selected, then print a short message saying that the selection chosen isn't available yet. This is a method that's often used in actual software development.
You block out the main segments of the program, then use stubs to indicate where later functions will be. This way a partial program can be tested early rather than trying to debug an entire program at once.
These are graphics adapters that allow you to go beyond simple text. Thus we've accepted Basic default modes of text screens with 80 columns. There are several ways we can change these defaults. We can set the width of a text screen to be either 40 or In 40 column, the letters are larger, so sometimes easier to read.
Both modes have their applications. All that happens is that display is limited to the left hand side of the screen. If a machine fails the compatibility test it's often related to how its video display is set up. You can use the example programs to test their claims when shopping! The following few paragraphs may tell you more than you want to know about the internals of video displays. You can skim them if you wish, then catch up with us below just press F2 The first thing we'll need to understand is what an attribute is.
Attributes describe how a character is displayed on the screen. We've actually been using attributes without worrying about them up to now.
When you enter, COLOR 15,1 you're telling Basic to set the attribute to 31 which is displayed as intense white on a dark blue background. But why 31? The attribute byte, like all bytes can take values from 0 to We can look at it as a series of 8 bits, each of which can be 0 or 1. It's an extremely efficient way to code the foreground color, its intensity, the background color, and whether or not the character is blinking, all in one number.
Next we code the intensity by setting the 4'th or 8's bit on if we want intense color, leaving it at 0 otherwise. So to code for intense white, we'd use The initial 1 says intense, the next 3 give 7, white's code. Binary is the same as decimal 15, the number we've been using for intense white all along.
We can also code 8 possible background colors using another 3 bits. That brings our total to 7 bits. We use the last, the 's column bit to show whether or not to blink. We've wasted nothing and stored 4 pieces of information in one tiny byte.
There's no need to remember all the details. In order to store information on the screen, we need to know what to put there, and how to display it. The IBM method stores the information as repeating pairs of bytes. Even numbered bytes tell which ascii character, odd bytes give their attribute. If each character needs 2 bytes, then a 25 line screen of width 80 requires bytes.
A 40 column screen needs only However, the IBM video display has room to store 16K! Early applications failed to capitalize on this extra memory, since the tricks we'll look at now work only with color graphics adapters and their successors. It turns out that we can use that extra memory to display multiple screens at once. We can think of the IBM memory as being a chunk of 16K broken down as follows: 80 col 40 col 0 0 2K 1 4K K 1 2 6K 3 8K 8K 2 4 10K 5 12K 12K 3 6 14K 7 16K 16K Normally, we'd say that Basic limits us to 64K of code and data in our programs.
However, the IBM video display can be thought of as an additional 16K of memory. We'll look at some ways that extra memory can be used. The first and simplest way is just to display information. In the default screen of 80 columns and 25 rows, we have bytes of information. Looking at the map, we see that in fact there's room for 4 pages of information, numbered 0 to 3. Similarly, the 40 column width gives us 8 pages, from 0 to 7.
The burst is 0 for RGB screens, 1 for composite. This is an archaic leftover from early video monitors. Set the burst to 1 for any modern system.
The two interesting guys are apage and vpage, standing for active page and visual page. These can range from 0 to 3 or 7 depending on width. The visual page is the screen you're currently showing on your monitor. The active page is the page to which your program reads or writes. Normally these are the same, but some interesting effects are possible if you vary them. Screens are easier to show than explain.
See program SCR. This program shows how screens work. First we write a line on each of the 4 screens, switching both active and visual. Then we write to each of the screens, while keeping 0 as the active screen.
Finally we switch from screen to screen without writing anything more, to prove that in fact we have done something. Why bother? What happened while the program was writing to the other pages? Did you notice a delay? What if you had several pages of information, such as instructions that you wanted to store for easy reference, and didn't want to rewrite each time?
If you stored them to an alternate page, you'd only have to write them once, then by just shifting screens you could get that information instantly. Write the error first, then shift to the page. Keep track of errors and write each one on a new line. Be careful that you don't try to write past line 25! After each error, shift to the page showing accumulated errors, then wait for a keypress before coming back to the main program. BAS shows some of the straighter applications First we'll look at circles and colors: 10 'palette.
It gives players the ability to configure the colors to their taste or lack thereof. This changes the orientation of the screen from 80 by 25 text characters to by pixels or dots. This lets us create graphics images, lines and patterns. However, the second argument can only be 0 or 1. The first argument still sets the background color. If you issue a palette change command, the screen stays the same and the color switch. This is illustrated in the program.
The radius is in pixels, and the color is 0 to 3. PAINT -- fills an area with a color, starting at the indicated point. For circles or boxes, the center works well, but it's not required. The paint starts at that point and continues in all directions until a line of the indicated color is reached.
The next program builds on what we've learned with screen and color and adds straight lines and patterns. BAS, included in the shareware package. In addition to the commands we met earlier, this program introduces 2 new ones: PSET X,Y , color -- places a dot of color at the indicated pixel.
LINE -- This command has two forms, and several options. It's simplest, but longest form is LINE x1,y1 - x2,y2 ,color This draws a line from x1,y1 to x2,y2 in this color. From this point, we could write LINE x2,y2 - x3,y3 ,color to add a new segment starting at x2,y2, or we could just write LINE - x3,y3 ,color which says to draw the line from the last cursor location to x3,y3. There's a simple way of drawing boxes and optionally filling them. The 4 lines represented by these corners are drawn automatically.
The exercises this time are a little more involved than previously. This is because Basic graphics commands are fun to play with, AND because there are several points that can best be made after you've had some time to experiment.
Even if you don't do any of the exercises, you might find it interesting to read through this exercise section. Create a program that randomly draws boxes and circles on the screen, filling them with color. Experiment with different colored borders and painting. Watch what happens when borders overlap. At this point in the series, if you've been doing your homework, you should find the previous exercise straightforward. The next project should be more challenging! Hexadecimal Equivalents H. Key Scan Codes I.
The software described in this document is furnished under a license agreement or nondisclosure agreement. It is against the law to copy this software on magnetic tape, disk, or any other medium for any purpose other than the purchaser's personal use.
Copyright Microsoft Corporation, , All rights reserved. The software may be used or copied only in accordance with the terms of the agreement.
Section 1. Be sure to make a working copy of the diskette before you proceed. Brackets surround optional command-line elements. It also explains the unique properties of the ten re- definable function keys and of other keys and keystroke combinations.
Home Education Gw basic manual guide. See Full Reader. Post on Jan views.
0コメント