Friday, November 21, 2008

Qbasic - FAQs

Qbasic - FAQs

1.0.0 What is BASIC?
1.1.0 What is Qbasic?
1.1.1 Why QBasic?
1.1.2 Where can I find it?
1.1.3 Qbasic's limits and capabilities.
1.2.0 How does Qbasic differ from Quick Basic?
1.2.1 From GW-Basic?
1.2.2 From Basica?
1.3.0 What is an "interpreter"?
1.4.0 What is a "compiler"?
1.5.0 Compiling your basic programs.
1.6.0 Where do I find QuickBasic?
1.7.0 Using other compilers.
2.0.0 Loading programs.
2.1.0 Why won't my program load in Qbasic?
2.1.1 "Bad file mode"?
2.2.2 "Syntax error"?
3.0.0 Program structure.
3.1.0 What is modular programming?
3.2.0 What's the difference between SUB module and a GOSUB?
3.2.1 When should I use them?
3.3.0 What is a function?
3.3.1 Why should I use them?
3.4.0 What is event programming?
3.4.1 Is QBasic an event driven language?
4.0.0 Enhancing programs
4.1.0 How do I use the mouse in my QBasic programs?
4.2.0 How do I get more colors in my QBasic programs?
4.2.1 What is a color attribute/value?
4.2.2 How do I change them?

--------------------------------------------------------------------------------

1.0.0 What is BASIC?

"BASIC" is an acronym for the name of just one of the earlier high-level languages built for use by early programmers. The letters stand for: Beginner's All-purpose Symbolic Instruction Code. It's main purpose was to be a language that allowed the user to develop code quickly and easily, without a lot of knowledge concerning low level, or assembly language.

Although not actually written by Microsoft™, BASIC was ported by the then-small company for use on the personal computer. Bill Gates helped construct the code for what was considered the "first" personal computer... the Altair. Prior to BASIC early code was "toggled" in on the front panel one line at a time using switches. It was probably one of the first true "killer aps". With BASIC, a person that owned one of these early computers could write programs that were simpler to construct than machine language. It wasn't long before the personal computer grew to be more than a curiosity; probably due in part to the popularity of BASIC.

For more on the history B.A.S.I.C. try here: www.truebasic.com

--------------------------------------------------------------------------------

1.1.0 What is Qbasic?

Qbasic is a truncated version of QuickBasic(tm). It was developed by Microsoft from QuickBasic for use and inclusion with DOS 5.0 and later. QBasic is a run-time interpreted language that comes with a full featured editor and debugger. It supports more features than previous products, such as MS Basic, BasicA and GWBasic.

--------------------------------------------------------------------------------

1.1.1 Why QBasic?

Of course the appropriate response here would be... Why not?
It's FREE, easy to use, and compact. Beginning programmers now have an easy medium in which to "stretch their legs". QBasic will allow the programmer to develop code that has modular structure, in a much friendlier environment. QBasic is much more powerful than other "beginner" languages that previously were free with an operating system.

--------------------------------------------------------------------------------

1.1.2 Where can I find it?

Try Googling around for qbasic.

Following files are needed
QBASIC.EXE - The actual editor/interpreter program.
QBASIC.HLP - The help files for QBasic.
QBASIC.INI - Not necessary, since QB will create a new one.

--------------------------------------------------------------------------------

1.1.3 Qbasic's limits and capabilities.

Variable-name Length: 40 characters
Strings: 32,767 characters

Double-precision numbers:
1.79769313486231D+308 to -4.940656458412465D-324

Video:
CGA,MCGA,EGA,VGA,Olivetti and Hercules support.
Up to 262,144 colors with from 4 to 256 displayable.

Structure:
64K Main module level.
With included modules up to 125K

No line numbers (Optional)
(More specific information is available inside QBasic's HELP documentation.)

--------------------------------------------------------------------------------

1.2.0 How does QBasic differ from Quick Basic?

QBasic is very similar to QuickBasic. QBasic is actually a truncated version of QuickBasic, a compilable version. QuickBasic supports two(2) ways of program compilation:

Requires a run-time module, to interpret the QuickBasic Code.

Will create stand-alone compiled *.EXE files.

QuickBasic also supports libraries that can be compiled at runtime, but have been previously created. These libraries are interchangeable with other "Quick" products like Quick C, etc.

QBasic is not entirely the same as QuickBasic, in that some code modification may be necessary to get the QB code to run on QuickBasic, but more importantly, QuickBasic code needs to first be saved in ASCII text format and edited to remove unsupported commands for it to run in QBasic.

The easiest way to tell which one you have, is by looking at the version number... QB is usually ver 1.1, while the most common versions of QuickBasic are 4.0 and 4.5.

Note: version 7.0 is commonly called PDS. (Professional Development System.)

-------------------------------------------------------------------------------

1.2.1 From GW-Basic?

GW-Basic supported many of the features that QBasic does, yet others have been dropped. GW also could be compiled if the compiler was purchased separately. QBasic can run old GW files provided they are edited for unsupported commands, are syntactically correct, and in ASCII text file format.

GW Basic command structure and syntax was mostly comparable. QB changed the syntax on several commands, (Some support two syntax structures.) GW required line numbers; QB does not.

(Trivia note: The `GW' in GWBasic actually stands for Gee Whiz.)

--------------------------------------------------------------------------------

1.2.2 From BasicA?

BasicA (Microsoft Advanced Basic), supported many of the features that QBasic does, yet others have been dropped. (Namely cassette drive support) BasicA programs also could be renamed and accompanied by a run-time interpreter for use as stand-alone programs. QBasic can run old BasicA files, as long as line continuance characters are removed, (the hyphen), and provided they are edited for unsupported commands and are syntactically correct, and of course, in ASCII text file format.

BasicA command structure and syntax was mostly comparable. QB changed the syntax on several commands, (Some support two syntax structures.) BA required line numbers; QB does not.

--------------------------------------------------------------------------------

1.3.0 What is an "interpreter"?

An interpreter is a program that reads the source code and translates it's output as code that runs as a program. The "translation" is a run time compilation to memory only. It is full compilation, (Unlike the line-item compilation of past interpreters.) This is done every time the program is invoked, and can be much slower than a compiled program. (This form of compilation makes QBasic faster than previous languages, but still not as fast as a fully compiled EXE.)

This differs from a compiled program where code is actually ready for execution when the program is invoked. Compilation needs only be done once to create the program. Compiled programs cannot be edited, the source code must be edited, then recompiled.

--------------------------------------------------------------------------------

1.4.0 What is a "compiler"?

A compiler, whether stand-alone or part of the editor environment, is a program that takes source code and generates an executable program that can stand alone.

Compiled programs are desirable for several reasons:

They can be run without a copy of the interpreter (QBasic).

They execute much faster, than interpreted code.

The source cannot be edited, accidentally or otherwise.

Compiled programs are *.EXE files but can also be named *.COM files.

QuickBasic supports two methods of compilation; one that requires a run-time module, and one that creates a stand alone EXE file. Most programmers choose to compile to EXE, to reduce the chances of the user losing the runtime file.

--------------------------------------------------------------------------------

1.5.0 Compiling your Basic programs.

In order compile your Basic programs, you will need a compiler.

One IS NOT included with QBasic.

You can get a compiler for another version of Basic though. You can then compile programs using it, provided that your QBasic code has been edited to be syntactically correct with the compiler. Many compilers available today are closely compatible with QBasic, with QuickBasic being the most compatible. (Generally speaking, if the source code RUNS in QBasic, it will run IN QuickBasic, but compilation may require that the programmer, use proper programming techniques, conventions and structure to facilitate a correct compilation to *.EXE).

Many professional and shareware compilers are currently available, some are quite good. Each will of course, have it's own supported syntax, but with a little modification, your QB code can be complied.

--------------------------------------------------------------------------------

1.6.0 Where do I find QuickBasic?

Unfortunately, Microsoft stopped marketing and sales of QuickBasic many moons ago. MS' attention has turned from DOS-based programming languages to Windows, event-driven languages. (Visual Basic, Visual C++, etc). Some Internet software marketers still carry QuickBasic as it was last released, but finding a place that actually sells it, or has a copy available for registration is difficult. If found, the price of QuickBasic hasn't altered much since it's release. At last check, the best price found was for $89 American dollars sold through a marketer in the U.K. Other places were selling it for as much as $145. If you search the Internet and WWW you should be able to find several places that still offer not only QuickBasic, but other compilable Basic languages too.

You may also discover some less-than-reputable types that offer QB4.5 free for download. Downloading such a copy is not recommended, as QuickBasic is still not released for such use.

* * PLEASE NOTE! - Copying QuickBasic down from another computer will not get you a registered copy. Registration for older software is keep at the developer. Most software developers don't usually transfer registrations either. Pirating a copy is considered illegal. UWLabs does not encourage, nor does it support software piracy. UWL's own products had to be compiled using a borrowed computer with a registered version of QuickBasic. Other programs had been compiled using demo's of other compilers available as shareware. It is recommended that if you wish to compile your programs, that you try some of the other products available today, first. Many of these products will allow you to compile your programs, even before you need to register.

--------------------------------------------------------------------------------

1.7.0 Using other compilers.

Other compilers may seem foreign at first, but all program editors work on the same basic principles. You made need to adapt to the different programming environment, and also to the new compiler's command syntax. Good compilers/editors will include documentation to help with the transition. A tangible side effect of using other compilers, is that you will learn to create code based on structure rather than language.

--------------------------------------------------------------------------------

2.0.0 Loading programs

Simple program loading is available in QBasic. Almost any TEXT file can be loaded into the editor. (ASCII formatted text is of course, expected.) QBasic's editor also supports "edit" mode where the editor can be used to create text files.

To launch just the plain text editor, type either one of the following at the C:\> prompt:

C:\> edit
C:\> qbasic /e

Note: on Win9x machines, EDIT will actually invoke MS' newer DOS-like editor EDIT.EXE, Where the "/e" will force QB to be the editor.

--------------------------------------------------------------------------------

2.1.0 Why won't my program load in Qbasic?

It is possible that the program you acquired was not written in QBasic. Or if it was written in QuickBasic, the author did not save the file in ASCII format. Often times program authors will post their wares, then in an effort to save time, will refer to QuickBasic as QBasic. As previously stated, QuickBasic *.BAS files may not necessarily load into QBasic. (You can thank these authors for further confusing new QBasic users, by referring to QuickBasic as QBasic).

It is also possible that file was created by any one of the following BASIC programming languages; QuickBasic4.5, QuickBasic PDS, TurboBASIC, Visual Basic, BasicA, GW Basic, Apple Basic, Tandy Basic, Microsoft Basic, and many, many more.

--------------------------------------------------------------------------------

2.1.1 "Bad file mode"?

GW Basic, QuickBasic, and BasicA, all used a binary save format. This format was created to save disk space on early computers. QBasic *.BAS files are saved ONLY in ASCII text format, so any file that displays this error cannot be loaded into QBasic, unless it first is loaded into another Basic editor and saved as ASCII.

NOTE: Just because a file has a *.BAS extension, does not necessarily mean that it is a QBasic file. It could be one of any number of Basic program files... including Visual Basic.

-------------------------------------------------------------------------------

2.2.2 "Syntax error"?

The word that is highlighted in your editor, is not recognized by QB as an accepted command, variable name, line identifier, or other supported programming convention. Either it was keyed-in incorrectly, or...

Again the file may have been created in another editor, with different language syntax. You will need to find out the QBasic method of performing the specified task, and modify your code accordingly.

--------------------------------------------------------------------------------

3.0.0 Program structure

QBasic provides the beginning programmer with the tools to learn to program. HOW the new user chooses to create his/her program is their choice. Program structure should be of accepted standard so that when the source code is shared, (as you more than likely will do), it is comprehensible. Good structure will include clearly defined TYPES, SUBS, MODULES, and LOOPs. Good QBasic structure will also include clearly named variables, line identifiers, arrays and files. Loops should have a minimum of exit points, and subs should not branch unpredictably.

It is also considered good practice to include comments for your code, and use line indents, for each loop level.

The old adage of "GOTO is poor programming practice" rings true.

--------------------------------------------------------------------------------

3.1.0 What is modular programming?

Modular programming involves building a program in such a way as to create it in specific sections; modules. This can be done using SUB declarations, GOSUBS, and FUNCTIONS. Using modular structure is preferred because, elements that need to be repeated can be created once, than invoked repeatedly. Good programming practice will also allow you to "transport" SUB modules for inclusion in other programs you create.

A good example:

DECLARE SUB Center(RowPosition, Text$)
'
Center 12, "This text is centered."
Center 13, "So is this text example."

END
'------------------------------------
SUB Center (RowPosition,Text$)
Column = 40 - INT(LEN(Text$)/2)
LOCATE RowPosition, Column
PRINT Text$;
END SUB

The above example allows the programmer to call the SUB Center() any time he wishes to print centered text to the screen.

Now in the QB environment, the SUB would not be shown in the main edit window until it was chosen from the menu. Keeping the SUBs separated like this makes it easier to concentrate on the task at hand, and moves the coding to it's own separate "page" so to speak.

--------------------------------------------------------------------------------

3.2.0 What's the difference between a SUB module and a GOSUB?

QBasic allows the programmer to create SUB modules that are accessed separately from the main module, where GOSUBS are program directions that are invoked at any level.

Similar to a GOTO, GOSUB routes the program instructions to a new line to execute that code. The last line will need to be RETURN, so that program execution returns to the line of below where it left out. QB allows for a RETURN Line Identifier, but that also allows for poor programming practice. You should just RETURN where you left from to avoid unexpected results.

SUBs are separate modules that are created with the DECLARE SUB statement. Module level SUBs are great for redundant and recursive operations, and they accept parameters. The parameters are passed at the time of CALL, and usually are what the SUB uses to complete it's work. (See the example in Modular programming.) The RowPosition and Text$ are parameters.

Declared SUB routines are unaware of variables in other modules unless they are passed as parameters, or defined as SHARED. (Hint: You can use COMMON SHARED, but it uses more resources than if you DIMension them shared.)

A secondary benefit to creating SUB modules is that the amount of memory used for program loading is not as affected by the module size. Main Module level is restricted to 64K, but module code can be included making it much larger.

--------------------------------------------------------------------------------

3.2.1 When should I use them?

Use SUBs any time you need to do some work repeatedly, or any time it can be used to save coding steps, or if you would like to separate a section of code from the main module. USE GOSUBs when you want to keep the work locally, and there isn't a need for passing parameters, or if the work needs access to variables that aren't shared.

The nice thing about GOSUBs is that you can use them inside SUB modules. Effectively skipping to that module as if it where another program in itself.

--------------------------------------------------------------------------------

3.3.0 What is a function?

A FUNCTION is similar to a Declared SUB. It too is DECLAREd, and uses parameters that can be past when it is invoked. The main difference is that the FUNCTION is used more as a way to add to QB's already defined functions/commands. A function (typically), is used to do some mathematical operation. Be it a Boolean choice, or an algebraic function.

Once DECLAREd the FUNCTION can be used much like a command already in QB.

Example:

DECLARE FUNCTION Area (Length, Width)
'
INPUT "Enter the width"; W
INPUT "Enter the length"; L
Total = Area(L,W)
PRINT Total

END
'------------------------------------
FUNCTION Area (Length, Width)
Area = Length * Width
END SUB

Not the best example, after all, you could probably calculate the area, right there inline much faster, but it shows a simple example.

FUNCTIONs need not necessarily be mathematical in nature, the "SUB Center" example could easily have been a function instead. A FUNCTION needs to have at least on value to return when called.

--------------------------------------------------------------------------------

3.3.1 Why should I use them?

Good programming practice dictates that whenever you can create a module of any type to support your coding, you should. If it can be done easier, and repeatedly though a FUNCTION, then by all means, you should create a FUNCTION to do it. A well written function can be called time and time again with just a command, and save so much coding. The benefit is that FUNCTIONs can be recursive. Meaning that it can call itself! (This applies to SUBs too.) That way a simple task can be repeated until a condition is met, and it then becomes a complex task in very few lines of code.

--------------------------------------------------------------------------------

3.4.0 What is event programming?

Before the advent of Windows, most software was not programmed in event driven languages, but rather polled languages. Event programming is somewhat easier because, cause and action inside the program are based on the "event" taking place, rather than polling. (Continually checking a device, a variable, etc.)

The best way to explain this is with the way the two different methods handle mouse routines. In polled language, you must create a conditional loop while using the mouse, and once the condition is met, and action is taken. (Say using IF/ THEN or possibly SELECT CASE.) Then you must continually check to see if the mouse has performed the action.

In an event driven language. You construct the program to handle an event when it occurs. Example: OnMouseClick PressButton1% The operating system reports the event through the kernel, and you then can handle it.

--------------------------------------------------------------------------------

3.4.1 Is QBasic an event driven language?

Absolutely not. BUT, you can still make use of some simple QB commands that will help your program BEHAVE like it is event driven. Commands like WAIT, ON ERROR. ON PLAY, INP(), etc.

--------------------------------------------------------------------------------

4.0.0 Enhancing programs

QBasic allows for more commands and structured code to enhance your programs over previous Basic languages. VGA support, SUB and FUNCTION modules, and a way to include ASM (Assembly) language through CALL ABSOLUTE.

With the addition of these "upgrades," programs can be more colorful, and can include newer devices that may have not been planned for at the time of Basic's creation. Devices like the mouse as opposed to the light pen or joy stick.

--------------------------------------------------------------------------------

4.1.0 How do I use the mouse in my QBasic programs?

Suffice to say, that there are several good ways to get the mouse to work in your QB program. But there isn't enough space here to fully explain. Download mouse-it.bas from the files section, and it should get you well on your way to using the mouse in your programs.

--------------------------------------------------------------------------------

4.2.0 How do I get more colors in my QBasic programs?

QBasic is capable of the following amount of colors in their corresponding screen modes:

Screen Mode Resolution Foreground Background
0 40 to 80 by
25 to 50 characters 32* 16
1 320 x 200 pixels 4* 1
2 640 x 200 2* 1
7 320 x 200 16 1
8 640 x 200 16 1
9 640 x 350 16* 1
10 640 x 350 8* (b&w) 1
11 640 x 480 8* (b&w) 1
12 640 x 480 16* 1
13 320 x 200 256* 1
* 16 Blinking
* Pseudo colors
* Out of 4 palettes of 4 colors
* Out of 64 in the palette
* Out of 262,144 in the VGA

Depending on the current screen mode, you can use the COLOR, command to change the current color, or change a background color. Other colors available to that particular screen mode can also be replaced using PALETTE or PALETTE USING.

--------------------------------------------------------------------------------

4.2.1 What is a color attribute/value?

A color ATTRIBUTE is a number that describes the color CHOICE directly from a list of colors available, where a color VALUE is a 4-byte color DESCRIPTION of a blend of the three colors that compose a monitor. Those three colors are: RED, GREEN, and BLUE. Hence the abbreviation RGB.

Here's an example:

Color Name Attribute EGA Value VGA/RGB Value
Black 0 0 0
Bright Red 12 12 63
Bright Green 10 10 16,128
Bright Blue 9 9 4,128,768
Bright Yellow 14 62 16,191
Bright White 15 63 4,144,159

Wow! Would you look at the size of those numbers! Why so high? Simple. RGB Values are calculated using a VERY SIMPLE formula, and here it is:

Value = (Red * 1) + (Green * 256) + (Blue * 65536)

Where "Value" is the 4-byte color descriptor and
"Red", "Green", and "Blue" are numbers from 0 to 63.

In the above example, (Bright White), the Value is 4,144,959.
Applying the formula:

4144959 = (63 * 1) + (63 * 256) + (63 * 65536)
Br.White = Max.Red + Max.Blue + Max.Green

So, by adjusting any of the RGB values, we can create any color within the 262,144 available!

--------------------------------------------------------------------------------

4.2.2 How do I change them?

To use one of your new-found colors is very simple. Simply choose a color ATTRIBUTE to replace with your new color VALUE.

Like this... PALETTE Color_To_Replace%, RGB_Value!

Where, Color_To_Replace% is the Attribute and, RGB_Value! is the Value.

Example, (Replace color attribute 5, magenta, with a value of 1572894, a color that looks like a red-violet on my monitor...

PALETTE 5, 1571894

There is another faster but yet more complicated way to change the RGB values right in the VGA itself. To see how that is done, download my Palette Fader routines from the files section.

--------------------------------------------------------------------------------

MORE ... Later

-------------------------------------------------------------------------------

0 comments :

Post a Comment