Sunday 13 November 2011

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

How can I call BIOS functions from my program?

As in the preceding example, you are quite frequently calling BIOS functions when you use standard C library routines such as _setvideomode(). In addition, even the DOS functions used previously (getch() and printf()) ultimately made calls into the BIOS to actually perform the tasks. In such cases, DOS is simply passing on your DOS request to the proper lower-level BIOS function. In fact, the following example code illustrates this fact perfectly. This code performs the same tasks that the preceding example does, except that DOS is bypassed altogether and the BIOS is called directly.

#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 = 0; /* get character */
int86(0x16, &regs, &regs);
return((char)regs.h.al);
}
void
OutputString(char * string)
{
union REGS regs;
regs.h.ah = 0x0E; /* print character */
regs.h.bh = 0;
/* loop, printing all characters */
for(;*string != ‘\0’; string++){
regs.h.al = *string;
int86(0x10, &regs, &regs);
}
}

As you can see, the only changes were in the GetAKey() and OutputString() functions themselves. The
GetAKey() function bypasses DOS and calls the keyboard BIOS directly to get the character (note that in this particular call, the key is not echoed to the screen, unlike in the previous example). The OutputString() function bypasses DOS and calls the Video BIOS directly to print the string. Note the inefficiency of this particular example—the C code must sit in a loop, printing one character at a time. The Video BIOS does support a print-string function, but because C cannot access all the registers needed to set up the call to print the string, I had to default to printing one character at a time. Sigh. Regardless, you can run this example to produce the same output as is produced in the example beforeit.

Cross Reference:

XIV.2: How can I call DOS functions from my program?

No comments:

Post a Comment