Sunday 13 November 2011

How can I call DOS functions from my program? in C programming

How can I call DOS functions from my program?

To be honest, you are calling DOS functions whenever you call printf(), fopen(), fclose(), any function
whose name starts with _dos, or dozens of other C functions. However, Microsoft and Borland C provide a pair of functions called int86() and int86x() that enable you to call not only DOS, but other low-level functions as well. Using these functions, you often can save time by bypassing the standard C functions and calling the DOS functions directly. The following example illustrates how to call DOS to get a character from the keyboard and print a character string, instead of calling getch() and printf() to perform the task. (This code needs to be compiled with the Large memory model.)

#include <stdlib.h>
#include <dos.h>
char GetAKey(void);
void OutputString(char *);
main(int argc, char ** argv)
{
char str[128];
union REGS regs;
int ch;
/* copy argument string; if none, use “Hello World” */
strcpy(str, (argv[1] == NULL ? “Hello World” : argv[1]));
while((ch = GetAKey()) != 27){
OutputString(str);
}
}
char
GetAKey()
{
union REGS regs;
regs.h.ah = 1; /* function 1 is “get keyboard character” */
int86(0x21, &regs, &regs);
return((char)regs.h.al);
}
void
OutputString(char * string)
{
union REGS regs;
struct SREGS segregs;
/* terminate string for DOS function */
*(string+strlen(string)) = ‘$’;
regs.h.ah = 9; /* function 9 is “print a string” */
regs.x.dx = FP_OFF(string);
segregs.ds = FP_SEG(string);
int86x(0x21, &regs, &regs, &segregs);
}

The preceding example code created two functions to replace getch() and printf(). They are GetAKey()
and OutputString(). In truth, the GetAKey() function is actually more analogous to the standard C getche() function because it, like getche(), prints the key character on-screen. In both cases, DOS itself was called using the int86() function (in GetAKey()) and the int86x() function (in OutputString()) to perform the desired tasks. 

DOS contains a veritable plethora of callable routines just like these two functions. Although you’ll find that many of them are well covered by standard C routines, you’ll also find that many are not. DOS also contains many undocumented functions that are quite interesting, and useful as well. An excellent example of this is the DOS Busy Flag, also called the InDos Flag. DOS function 34 hex returns a pointer to a system memory location that contains the DOS Busy Flag. This flag is set to 1 whenever DOS is busy doing something critical and doesn’t want to be called (not even by itself). The flag is cleared (set to zero) when DOS is not busy. The purpose of this flag is to inform DOS when it is executing critical code. However, programmers find this flag useful as well so that they can also know when DOS is busy. Because Microsoft has recently documented this function, it is technically no longer an undocumented routine, but it has been in DOS since version 2.0.Several excellent books on documented and undocumented DOS functions are available for those who are more interested in this topic.

Cross Reference:

XIV.3: How can I call BIOS functions from my program?

No comments:

Post a Comment