I need to get system uptime since last boot using C.
Target OS is MS DOS.
I tried time()
, ftime()
but they are relative to Jan 1,1970 00:00:00.
Can anybody suggest some solution?
Thanks in advance.
To read the system time on MS-DOS, you can call INT 1a,0
, e.g. like this (GNU syntax):
unsigned short cx, dx;
__asm__ (
"mov $0, %%ah \n\t"
"int $0x1a \n\t"
: "=c" (cx), "=d" (dx)
:
: "ax"
);
time_t time = cx;
time <<= 16;
time |= dx;
time = (time_t) ((double)time / 18.2065);
This is probably your best bet. If nobody has set this time using INT 1a,1
, you will get the seconds since boot.
Note this only counts up to 24 hours, if you need longer periods of time, you have to call this regularly and pay attention to the "midnight flag" in al
.
Add another output variable in this case, increment a day counter whenever you see al
non-zero and just add days * 86400
to the final result, rough outline:
unsigned short ax, cx, dx;
static unsigned days = 0;
__asm__ (
"mov $0, %%ah \n\t"
"int $0x1a \n\t"
: "=a" (ax), "=c" (cx), "=d" (dx)
);
if (ax & 0xff) ++days;
time_t time = cx;
time <<= 16;
time |= dx;
time = (time_t) ((double)time / 18.2065);
time += days * 86400;
I found some additional info that might be useful here:
The second problem comes in because of how BIOS int 0x1A operates. Whenever you call this function to retrieve the system time (the current timer tick value) it also returns the current MIDNIGHT flag and RESETS THE FLAG. But since the BIOS function doesn't update the DOS date, the next time you ask for the date, it will not be updated correctly. DOS is aware of the behavior, so when you call any DOS function, the MIDNIGHT flag is maintained correctly. If you call BIOS int 0x1A yourself, you MUST check the MIDNIGHT flag value and turn it back on if it was set.
So, in short, if you need MS-DOS to maintain a correct date while your program is running, you have to do additional work (like restoring the flag manually and calling INT 21,2A
each time it was found)
0040:006C
which is the number of ticks since power up. When the count reaches 24 hours, it is cleared and the byte value at0040:0070
is incremented.int 1Ah
function00h
Get Clock Counter, which I think has the side effect of clearing the midnight flag.difftime()
to get the uptime value. cplusplus.com/reference/ctime/difftime*nix
take a look atuptime.c
fromprocps