Dos Programming
Dos Programming
writing BASIC for the Altair was exhaustingPaul and I didnt sleep much and lost track of night and day. When I did fall asleep, it was usually at my desk or on the floor. Some days I didnt eatBut after five weeksworlds first microcomputer software company was born.
Bill Gates
Courtesy: The Road Ahead (ISBN 0-14-024351-8)
14
14.1 Prelude
DOS Secrets
To program well, you have to know more about your hardware and DOS internals. This book is neither a hardware book nor a beginners book. So I would slightly touch the hardware and DOS internals in this chapter. In many Institutions hardware & software are being taught as different subjects. And people dont know how both are related. For system programming you must know the relationship between the two. This chapter will help you to understand why a programmer should know hardware & DOS internals for DOS programming.
DOS (Disk Operating System) is the widely used operating system. It is a single-user operating system. DOS is designed to provide an easy way to use disks for storage. It is very efficient in controlling, accessing and managing the data from disk drives. The basic operations performed by DOS are regulate space allocation, keep track of files, save and retrieve files and manage other control functions associated with disk storage. Thus using DOS an interface is made between the user and the computer. This DOS is same for all the systems. For loading this DOS to the memory BIOS, bootstrap program, diagnostic testing programs are very essential and we will discuss it in the coming sections.
14.1.1 BIOS
It is a program that provides link between the hardware and the operating system. It is a firmware (Firmware is a program or data stored in ROM. These are not altered by software, and are not lost when the power is turned off). Since it is stored in ROM, it is usually called as ROM BIOS. It contains many low level routines. It is responsible for basic hardware operations such as interactions with disk drives and keyboards. It also has drivers and other software that manages the peripheral devices. The basic operations performed by BIOS are Keyboard routine Video routines Printer routines
This BIOS program differs from system to system. For getting good results we can use BIOS functions along with the DOS functions.
44
Operating System
RAM
!
14.1.3 Boot Sector
The boot sector on a disk is always the first sector on the first track on the first head. BIOS starts up and does the POST, when computer is powered ON. It initializes all its data and then looks for a valid boot sector. First it looks at the Floppy disk (A:), then at the Hard disk (C:). After this process, the operating system is loaded into the memory, which is explained in the figure. If it doesn't find it then interrupt 18h is called (on original IBM PCs this started the ROM BASIC). A valid boot sector (to the BIOS) is one that has 0AA55h at offset 510 in the boot sector. When the BIOS finds the boot sector, it reads that sector (512 bytes) off of the disk and into memory at 0:7C00h. Then it jumps to 0:7C00h and the boot sector code gets control. BIOS data area (40h:0) and the BIOS interrupts (10h - 1Ah) are initialized. At this point, memory is mostly unused, but not necessarily cleared to 0.
A to Z of C 45
640k
The 384K of upper memory is further divided 00000 into three equal parts of 128K each. The first 128K above the conventional memory area is reserved for the use by the video adapter and it is also called video RAM. The next 128K is reserved for use by the adapter BIOS and the last 128K is for Motherboard BIOS. In the video RAM area the information related to text and graphics display on screen is stored. E0000 DFFFF The address range of this video adapter is A0000-BFFFF. If we use Adapter BIOS 128k monochrome graphics adapter C0000 (MGA) then the information about BFFFF the display is stored between B0000 Video RAM (video adapter) 128k and B8000. If we use CGA then it A0000 occupies the address range B8000BFFFF. Graphics mode video RAM occupies A0000-AFFFF. In the 128K area of adapter BIOS, the first 32K is used by VGA compatible video adapters and the remaining area is used by network adapters and some other adapters. In the 128K of the motherboard BIOS, the first 64K is called free UMA block space and most of the systems use only the last 64K. In this area POST (Power On Self Testwhich is a set of routines that test motherboard, memory, disk drives, adapter, keyboards, other devices and components in the system), bootstrap loader (which is set of routines to start the operating system) and CMOS (Component Metal Oxide Semiconductorwhich is used to configure the system by pressing some key while booting) reside.
Motherboard BIOS 128k FFFFF
46
A to Z of C
512KB
In this method overlapping is possible. For example, we can get the same physical address in various segments and offset combinations.
B080 8008 B8888 B880 0088 B8888 B008 8808 B8888 B808 0808 B8888
256KB
0KB