is it available to get time using only c code?
Posted: Fri Jul 08, 2016 8:54 pm
Hi, i was asking if there is a way to get time with just c code, with out assembly..if not, how to do it with assembly?
The Place to Start for Operating System Developers
http://f.osdev.org/
It's impossible to get the time with just C code without any assembly being used directly or indirectly (e.g. in a library somewhere).Mohamed007 wrote:Hi, i was asking if there is a way to get time with just c code, with out assembly..if not, how to do it with assembly?
im using gcc and there is no <time.h> library availablealexfru wrote:Your question should be a bit more specific. If you're using a standard C compiler (which implies a standard C library), then there are time functions declared in <time.h>, look them up. If not, it depends on your hardware. If time can be obtained by reading memory at a specific address, then you may be able to do just that. If, however, communication with the timer/clock cannot be done via memory alone (say, you need to access x86 I/O ports), then you're probably out of luck.
i followed the code and coded out_byte and in_byte functions like that (the same way i used while dealing with keyboard ports and its working for the it)Brendan wrote:Hi,
It's impossible to get the time with just C code without any assembly being used directly or indirectly (e.g. in a library somewhere).Mohamed007 wrote:Hi, i was asking if there is a way to get time with just c code, with out assembly..if not, how to do it with assembly?
To get the time you'll need to determine where you're getting the time from - getting time from (e.g.) the RTC chip on 80x86 is very different from getting time from an NTP server.
There is example code to get time and date from the RTC chip on 80x86 (which is almost entirely in C, but uses assembly for IO port access) on the wiki. Of course you don't really know which time and date your getting (local time or UTC or something else) and this should only need to be done once (e.g. the OS should do "time_now = time_at_boot + time_since_boot" where you only determine "time_at_boot" once and "time_size_boot" can be maintained by a more precise timer like HPET).
Cheers,
Brendan
Code: Select all
uint8 in_byte (uint16 _port)
{
uint8 rv;
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}
Code: Select all
void out_byte (uint16 _port, uint8 _data)
{
__asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}