2.4.1 MS-DOS Programs

MS-DOS recognizes two program types: .COM and .EXE. A .COM program, sometimes called a “tiny model” program, consists of code, data, and a stack, in a single segment. Such programs typically have a single purpose: carrying out a task and terminating. On the other hand, an .EXE program is usually large and has code and data in separate segments. In fact, an .EXE program can have any number of segments, the combined size of which is limited only by system memory. An .EXE program can be loaded anywhere in memory. MS-DOS adjusts any segment addresses in code and data when it loads the program.

2.4.1.1 A Simple MS-DOS Program

MS-DOS programs can use system functions to carry out their work. Programs call the system functions by using the int instruction and specifying Interrupt 21h. For this reason, many MS-DOS programs are written in assembly language or in a mixture of assembly language and a high-level language such as C.

When a program issues an interrupt, execution control transfers to the MS-DOS routine that handles system-function requests. MS-DOS installs this routine at system startup.

The following sample program shows how system functions are called. The program writes the message “Hello, MS-DOS!” to the screen and then terminates immediately.

title 'Sample Program'

.model small

.data

String db 'Hello, MS-DOS!', 13, 10

StringLen equ $ - String

.code

Start:

mov bx, 1 ;handle of file or device

mov cx, StringLen ;maximum number of bytes to write

mov ax, seg String

mov ds, ax

mov dx, offset String ;ds:dx points to buffer containing data

mov ah, 40h ;Write File or Device

int 21h

mov al, 0 ;program-defined return value

mov ah, 4Ch ;End Program

int 21h

.stack 256

end Start

This program calls two system functions: Write File or Device (Interrupt 21h Function 40h) and End Program (Interrupt 21h Function 4Ch).

Write File or Device writes the message. It requires a file or device handle in the BX register; the length of the string, in bytes, in the CX register; the address of the string in the DS:DX registers; and the function number, 40h, in the AH register. In this example, the program uses the standard-output device handle (1), which is supplied by COMMAND.COM when it starts the program. Unless the user redirects output, the program can use the standard-output device handle to write to the screen.

End Program terminates the program and returns control to COMMAND.COM. Every MS-DOS program must terminate by using a system function such as End Program.

2.4.1.2 Terminate-and-Stay-Resident Programs

Although most programs offer their services to users only while the programs are running, MS-DOS allows programs to offer their services even after they terminate. Such programs are called terminate-and-stay-resident programs (TSRs). These programs receive execution control through hardware or software interrupts, such as the interrupt generated by pressing the SHIFT+PRINT SCREEN key combination. The interrupt temporarily suspends the program that is currently running and lets the TSR carry out work. When the TSR has completed its task, it reactivates the suspended program by returning control to it.

Many MS-DOS programs are TSRs—for example, Nlsfunc, Keyb, Share, and Doskey. MS-DOS uses these programs to provide extended capabilities in areas such as national language support and file sharing.