home *** CD-ROM | disk | FTP | other *** search
- Subject..: Low level Routines in Microsoft BASIC
- Author...: George Spafford
- Version..: 1.1
- Date.....: May 2, 1993
-
- This small tutorial package is aimed at users who have a working knowledge
- of the Microsoft BASIC language. It is not aimed at the novice programmer.
- I hope that you find the information in this tutorial useful. If you do, please
- register this shareware. As a programmer, you know that this type of an
- endeavor requires a substantial investment of time and effort. If I am to
- continue it, I need your support.
-
- Section Outline:
-
- 1. Getting Started
- a. Binary Numbers
- b. Hexidecimal Numbers
- c. Decimal Numbers
- d. Registers
- e. Bit manipulations
- 2. Interrupts
- 3. Peeks
- 4. Pokes
- 5. Summary
- 6. Revision History
- 7. Please register this shareware!
-
- -----------------------
- NOTES ABOUT SAMPLE CODE:
- -----------------------
-
- Look at the sample .BAS files that are included with this package.
- Print them out and refer to them while you read this text. The .BAS
- files are heavily commented and should help to clarify some of your
- questions. If you are anything like me, looking at code clarifies my
- understanding of a problem immensely.
-
- If you intend to use any of the sample programs after the 30 day
- evaluation period, you must register the entire tutorial package.
-
-
- -----------------------
- 1. GETTING STARTED
- -----------------------
-
- I like BASIC! While many people make excuses for using it, I find it a
- pleasant language to develop in. By adding libraries or writing my own
- add-ins, I can perform just about any task I need to. As far as code
- reusability goes, if I used modularized programming with functions and
- subroutines, it is quite high. Note, in order for a person to see most
- of the code that I wrote for the demonstration programs, I refrained from
- functions and subroutines in order to adopt a top-down approach. The
- exception to this is MAKEWORD& - which is a function that I used in several
- demos (I called it INT2LONG& in WORD.BAS). Alright, let's get started.
-
- A. Binary Numbers
-
- The binary number system reflects that a computer circuit can only be "on"
- or "off" - nothing else. Thus, a number scheme is used to represent any
- number by means of a "1" or a "0". Any number can be computed by using
- the equation:
-
- 2^n + ... 2^3 + 2^2 + 2^1 + 2^0
-
- Numbers, bit sequences are evaluated from right to left. Let's take a look
- at some examples of this:
-
- 8 Bits Decimal
- -------- = -------
- 00000000 0
- 00000001 1
- 00000010 2
- 00000011 3
- 00000100 4
- ------
- 10000000 128
-
- 8 Bit Positions
- 00000000
- ||||||||
- |||||||| Exponent
- |||||||| --------
- |||||||\----- 0
- ||||||\------ 1
- |||||\------- 2
- ||||\-------- 3
- |||\--------- 4
- ||\---------- 5
- |\----------- 6
- \------------ 7
-
- Long Hand Examples:
-
- 01001101 = 0^7 + 2^6 + 0^5 + 0^4 + 2^3 + 2^2 + 0^1 + 2^0
- = 0 + 64 + 0 + 0 + 8 + 4 + 0 + 1
- = 77
-
- 10011100 = 2^7 + 0^6 + 0^5 + 2^4 + 2^3 + 2^2 + 0^1 + 0^0
- = 128 + 0 + 0 + 16 + 8 + 4 + 0 + 0
- = 156
-
- 00000111 = 0^7 + 0^6 + 0^5 + 0^4 + 0^3 + 2^2 + 2^1 + 2^0
- = 0 + 0 + 0 + 0 + 0 + 4 + 2 + 1
- = 7
-
- Note: If a bit position is 0 then the value is 0 - why?
- The actual notation for bit calculation can be identified
- as (1 x 2^n) if a bit is on or (0 x 2^n) if a bit is off.
- I just skipped that particular step in my examples.
-
- 16 Bit Positions:
-
- 00000000 00000000 Exponent
- |||||||| |||||||| --------
- |||||||| |||||||\------ 0
- |||||||| ||||||\------- 1
- |||||||| |||||\-------- 2
- |||||||| ||||\--------- 3
- |||||||| |||\---------- 4
- |||||||| ||\----------- 5
- |||||||| |\------------ 6
- |||||||| \------------- 7
- ||||||||
- |||||||\--------------- 8
- ||||||\---------------- 9
- |||||\----------------- 10
- ||||\------------------ 11
- |||\------------------- 12
- ||\-------------------- 13
- |\--------------------- 14
- \---------------------- 15
-
- A Long Hand Example:
-
- 01000100 10010011
-
- = 0^15 + 2^14 + 0^13 + 0^12 + 0^11 + 2^10 + 0^9 + 0^8 + 2^7 + 0^6 +
- +0^5 + 2^4 + 0^3 + 0^2 + 2^1 + 2^0
-
- = 0 + 16384+ 0 + 0 + 0 + 1024 + 0 + 0 + 128 + 0 +
- +0 + 16 + 0 + 0 + 2 + 1
-
- = 17,555
-
- B. HEXADECIMAL NUMBERS
-
- The hexadecimal numbering system is a hold over from the days
- when there were 4-bit processors. If you have every studied
- statistics, then you know if there are four available bit positions,
- there are 16 possible bit combinations (2^4). Thus, a base 16
- number system was developed.
-
- Decimal Hex Binary
- ------- --- -------
- 0 = 0 = 0000
- 1 1 0001
- 2 2 0010
- 3 3 0011
- 4 4 0100
- 5 5 0101
- 6 6 0110
- 7 7 0111
- 8 8 1000
- 9 9 1001
- 10 A 1010
- 11 B 1011
- 12 C 1100
- 13 D 1101
- 14 E 1110
- 15 F 1111
-
- Since Hex is base 16, start from the right of a value and
- work your way to the left - just as you did with binary.
-
- 03F8 = (0 * 16^3) + (3 * 16^2) + (F * 16^1) + (8 * 16^0)
- = 0 + 3 * 256 + 15* 16 + 8 * 1
- = 0 + 768 + 240 + 8
- = 1,016
-
- 2F = (2 * 16^1) + (F * 16^0)
- = 32 + 15
- = 47
-
- BASIC can process directly coded hex by prefixing the hex
- value with "&H".
-
- &H03F8 <- to BASIC, this means 3F8 Hex, 1,016 decimal
-
- C. DECIMAL NUMBERS
-
- The decimal number system is what most of us are familiar with
- so I will not go into great details about it. BASIC, however,
- does discriminate between declarations of numeric variables:
-
- Variable Memory
- Suffix Meaning Range Requirements
- -------- ---------- ------------------------------ ------------
- % integer -32,768 to 32,767 (2 bytes)
- & long -2,147,483,648 to 2,147,483,647 (4 bytes)
- ! single precision (4 bytes)
- # double precision (8 bytes)
-
- D. REGISTERS
-
- Simply put, registers are memory locations within the processor.
-
- 16 bit 8 bit Usage
- -- -- -----------------
- AX AL Accumulator
- AH
- BX BL Base
- BH
- CX CL Count
- CH
- DX DL Data
- DH
- DI Destination index
- SI Source index
- SP Stack pointer
- BP Base pointer
- DS Data segment
- CS Code segment
- ES Extra segment
- SS Stack Segment
-
- AX, BX, CX and DX are 16-bit registers that can be further broken
- down into two 8-bit registers each. The Low register contains bits
- 0 to 7 and the High register contains bits 8 to 15.
-
- Flag Registers:
-
- Bit
- Position Definition
- ---------- -------------------------------
- 0 Carry Flag
- 2 Parity Flag
- 4 Auxiliary Flag
- 6 Zero Flag
- 7 Sign Flag
- 8 Trap Flag
- 9 Interrupt Flag
- A Direction Flag
- B Overflow Flag
-
- Note: You will see the Carry Flag frequently used to indicate
- whether an error has taken place or not.
-
- E. BIT MANIPULATIONS
-
- Many of the functions at the machine level require that you operate
- at the bit level rather than at the byte level. While BASIC does
- not offer a variable at the bit level, bits can be manipulated
- through the use of the logical boolean operators. Remember when
- we discussed the bits earlier? That 00000001 = 1 and so forth?
- Consider the following, what is the fastest way to determine if
- a number is odd or even? Give up? Take a look at this code fragment:
-
- INPUT "Enter a number ",number
- IF number AND 1 THEN
- PRINT "That is an odd number"
- ELSE
- PRINT "That is an even number"
- END IF
-
- The key in this fragment is the usage of the AND operator.
- Take a look at these following odd numbers:
-
- 00000001 = 1
- 00000011 3
- 00000101 5
- 00000111 7
-
- What do they have in common? They all have the right most bit
- (bit 0) turned on. The AND operator evaluates numbers and if
- the number to the right of the and has the same bit(s) on as the
- number to the left, then the answer is true.
-
- Bits Decimal
- -------- -------
- 00000001 1
- 00000010 2
- 00000100 4
- 00001000 8
- 00010000 16
- 00100000 32
- 01000000 64
- 10000000 128
-
- Examples:
-
- 96 AND 64
- 01100000 01000000 TRUE Bit 6 matches
-
- 56 AND 8
- 00111000 00001000 TRUE Bit 3 matches
-
- 56 AND 2
- 00111000 00000010 FALSE Bits do NOT match
-
- 64 AND 8
- 01000000 00001000 FALSE Bits do NOT match
-
- Why does this come in handy? Let's say you want to test
- the FLAGS register after doing an INTERRUPT call in BASIC.
- You want to know if it has been turned on due to an error:
-
- IF OutRegs.Flags AND 1 THEN
- PRINT "Oh no, an error fried your hard drive!"
- END IF
-
- Let's say you use Crescent Software's excellent QuickPak
- Pro library for DOS (one of my favorites). Many of its
- file information subroutines return a byte field that contains
- the file's attributes. How do you test the attributes?
- DOS uses a byte to hold the information. Its 8 bits are used
- as follows:
-
- Bit Use
- --- ---
- 0 Read Only
- 1 Hidden File
- 2 System File
- 3 Volume Name
- 4 Directory
- 5 Archive Bit
-
- By now, you should know how to test for these, but let's cover
- it once more:
-
- IF attrib AND 1 THEN PRINT "Read Only File"
- IF attrib AND 2 THEN PRINT "Hidden File
- IF attrib AND 4 THEN PRINT "System File"
- IF attrib AND 8 THEN PRINT "Volume Name"
- IF attrib AND 16 THEN PRINT "Directory Name"
- IF attrib AND 32 THEN PRINT "Archive File"
-
- If you feel daring, you can get this information yourself by
- using Interrupt 21H, Function 43H, Subfunction 0.
- Input:
- AH=43H
- AL=00H
- DS=Segment
- DX=Offset
-
- DS:DX describes where to find the DOS file name.
- (File Name + Chr$(0). See CHGMOD.BAS for
- a closely related example.)
-
- OutPut:
- Carry Flag = 0 OK
- CX = File Attribute
- bit 1 = Read Only
- 2 = Hidden
- 3 = System
- 4 = Volume Name
- 5 = Archive Bit
- Carry Flag = 1 Error
- AX = 1 Unknown error
- 2 File not found
- 3 Path not found
-
- ------------------
- 2. INTERRUPTS
- ------------------
-
- The word for the day is "interrupt." An interrupt is a signal to the
- respective processor that its attention is required. The operation
- that is requesting the information returns a value that ranges from
- 0 to 255. This value is known as the "type code." The type code is
- then used to calculate a new address known as an "interrupt vector."
- This is the address of either a DOS or BIOS program that will deal with
- the call. Note, DOS stands for Disk Operating System and BIOS stands
- for Basic Input/Output System - just in case you didn't know. There
- are some minor differences that exist between DR DOS that is marketed
- by Novell and MS DOS that is by Microsoft (with version 5.00, I lump
- IBM DOS and MS DOS together in terms of functionality, but this has
- not always been the case). I bring this up, because you must be
- certain that your end-users can run your code. If you make a system
- call that only works in newer versions of DOS and not older ones, then
- you should check for the DOS version BEFORE your program executes that
- call. If you don't you will potentially have some upset customers with
- crashed systems.
- Also, if you require a TSR (Terminate Stay Resident) program to be in
- memory, such as LAN (Local Area Network) software, for an interrupt to
- work, check for it BEFORE you call it!!!!
-
-
- ------------
- 3. PEEK
- ------------
-
- PEEK is a command that resides in BASIC for the purpose of looking
- at a byte in memory. If you want to get the value of the first byte
- of the BIOS communications table, you would do the following:
-
- DEF SEG = &H0
- A = PEEK(&H400)
- DEF SEG
-
- Line 1: This sets the current segment to &H0000 - the BIOS data area.
- (The start of the BIOS data area is actually 0000:0400).
- Line 2: Reads the integer value that exists at &H400, one byte only.
- &H400 is called an "offset."
- Line 3: This returns to the segment in which your BASIC programs is
- operating in.
-
- To PEEK word values, look at WORD.BAS. BASIC's internal PEEK function
- can only handle single bytes. WORD.BAS contains sample code to PEEK
- word values.
-
- ------------
- 4. Poke
- ------------
-
-
- POKE places a value into memory at a specified address. Whereas PEEK
- reads the memory, POKE writes to the memory. For example, let's say you
- want the PC to forget that COM1 exists:
-
- DEF SEG = &H0
- POKE &H400,&H0
- POKE &H401,&H0
- DEF SEG
-
- Line 1: This sets the current segment to &H0000, the BIOS segment.
- Line 2: This writes the value &H0 to the memory location &H400.
- Remember, this location is called an offset.
- Line 3: This writes the value &H0 to the offset &H401.
- Line 4: Returns to the default BASIC segment.
-
- Voila! No more COM1 !!
-
- To POKE words, see the WORD.BAS tutor file. BASIC's POKE can only
- do one byte at a time that is annoying. WORD.BAS has sample code that
- you can use to poke words into memory at any location. POKEWORD could
- have done the above to the comm port in one call.
-
- ---------------
- 5. SUMMARY
- ---------------
-
- ::FURTHER READING::
-
- There are two books that I recommend above all others:
-
- Tischer, Michael. PC INTERN, Abacus, Grand Rapids, MI, 1992.
- ISBN: 1-55755-145-6
-
- Duncan, Ray. The MS-DOS Encyclopedia, Microsoft Press,
- Redmond, WA, 1988.
- ISBN: 1-55615-174-8
-
- The encyclopedia is getting a little dated, but it is still useful.
-
- PC Intern is excellent! Tons and tons of information and well worth
- the $59.95. It has over 1200 pages and includes sample code on a disk.
-
- ------------------------
- 6. Revision History
- ------------------------
-
- v1.1 May 2, 1993
-
- Added:
-
- CHGMOD.BAS Tutor on obtaining file directores, setting the
- DTA address, and setting file attributes.
-
- DUMPMEM.BAS Tutor on using PEEK. Runs from command line.
- (Functionally identical to READMEM.BAS)
-
- PORT.BAS Tutor on using the functions outlined in WORD.BAS
- to modify the I/O addresses in the BIOS data
- table.
-
- READMEM.BAS Tutor on using PEEK. Prompts user for answers.
- (Functionally identical to DUMPMEM.BAS)
-
- WORD.BAS Contains functions for converting integers to
- long integers and PEEKing words, and a subroutine
- to POKE words.
-
- Modified:
-
- Revised S3BASTUT.DOC to reflect the new programs and corrected
- as many of the typographical errors as I could find.
-
- v1.0 April 20, 1993
-
- Initial release.
-
-
- ----------------
- 7. Register
- ----------------
-
- If you find this information useful, please consider registering it for
- $10 US. If there is interest, I will try to improve this tutorial. At
- the least, it served as an excellent review for me just to write it.
- If you use sample code from the tutorial, please have the courtesy
- to register the tutorial. If you want the latest copy of the tutor
- to be mailed to you with your registration, please add $5 to cover the
- diskette and mailing (the total would then be ($10 x # copies)+ $5).
-
- Please send your registration to:
-
- George Spafford
- 3003 Lakeshore Drive, #216
- Saint Joseph, MI 49085
- USA
-
- I can be reached on the following BBS systems:
-
- Michigan Online 19200-8-N-1 616-429-3414
- [This board is local to me and I try to call it at least
- twice a week - usually more on weekends.
-
- EXEC-PC and Channel-1: These are pay BBS systems, so I am
- not going to list their phone numbers.
-
- ALL SAMPLE PROGRAMS ARE PART OF THE S3 SOFTWARE BASIC TUTORIAL. THEY ARE NOT
- TO BE CONSIDERED SEPARATE. IF YOU CHOOSE TO USE ANY OF THE PROGRAMS IN THIS
- PACKAGE AFTER A 30 DAY TRIAL PERIOD, YOU MUST REGISTER THE ENTIRE PACKAGE ON
- THE BASIS OF CONCURRENT USE.
-
- NOTE: The SDV file viewer is included with the tutorial for the use of viewing
- this .DOC file and other package files. If you register this tutorial, you may
- continue to use it on a single system concurrently. If you do not register
- this tutorial, but choose to continue using SDV, you must register it separately
- and details can be found in the SDV12.DOC file.
-
- "Concurrent Use" defined:
-
- I consider concurrent use to mean more than one PC running, or looking, at the
- tutorial and/or its programs at the same time. If you plan on having 5 PCs
- using the tutor, or any of the programs that comprise it, at the same time, then
- you must register 5 copies of the program.
-
- ============
- LEGAL
- ============
-
-
- THE S3 BASIC TUTORIAL AND ALL OF ITS ACCOMPANYING SAMPLE CODE AND PROGRAMS ARE
- DISTRIBUTED AS IS. THE AUTHOR (GEORGE SPAFFORD), OR ANY CONTRIBUTOR, MAKES NO
- WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
- WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, WITH
- RESPECT TO THIS SOFTWARE AND DOCUMENTATION. IN NO EVENT SHALL THE AUTHOR OR
- CONTRIBUTORS BE LIABLE FOR ANY DAMAGES, INCLUDING LOST PROFITS, LOST SAVINGS,
- OR ANY OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF OR
- THE INABILITY TO USE THIS PROGRAM.
-
- All products mentioned are the property of their respective companies.
-
- SDV and the S3 BASIC Tutorial are the copyright of George Spafford. All
- Rights Reserved.
-