Sunday 20 November 2011

How do you create a delay timer in a DOS program? in C programming

How do you create a delay timer in a DOS program?

Fortunately for us programmers, the folks at Microsoft thought it would be a good idea to create a hardwareindependent delay timer. The purpose, of course, is to allow a delay of a fixed amount of time, regardless of  the speed of the computer on which the program is being run. The following example code demonstrates how to create a delay timer in DOS:

#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
void main(int argc, char ** argv)
{
union REGS regs;
unsigned long delay;
delay = atol(argv[1]); /* assume that there is an argument */
/* multiply by 1 for microsecond-granularity delay */
/* multiply by 1000 for millisecond-granularity delay */
/* multiply by 1000000 for second-granularity delay */
delay *= 1000000;
regs.x.ax = 0x8600;
regs.x.cx = (unsigned int)((delay & 0xFFFF0000L) >> 16);
regs.x.dx = (unsigned int)(delay & 0xFFFF);
int86(0x15, &regs, &regs);
}

The example uses DOS interrupt 0x15, function 0x86, to perform the delay. The amount of delay is in microseconds. Due to this, the delay function assumes that you might want a really big number, so it expects the high-order 16 bits of the delay value in CX, and the low-order 16 bits of the delay value in DX. At its maximum, the delay function can stall for more than 4 billion microseconds, or about 1.2 hours. The example assumes that the delay value will be in microseconds. This version of the example multiplies the delay by one million so that the delay entered on the command line will be turned into seconds. Therefore,a “delay 10” command will delay for 10 seconds.

Cross Reference:

XXI.2: How do you create a delay timer in a Windows program?

No comments:

Post a Comment