is it available to get time using only c code?
-
- Posts: 14
- Joined: Fri Jul 08, 2016 8:44 pm
is it available to get time using only c code?
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?
Re: is it available to get time using only c code?
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.
Re: is it available to get time using only c code?
Hi,
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
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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Re: is it available to get time using only c code?
I think that the best way of doing it would be accessing CMOS chip and getting time from there. But you need to use assembly for that as far as I know, your best bet is coding assembly from your C file.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
-
- Posts: 14
- Joined: Fri Jul 08, 2016 8:44 pm
Re: is it available to get time using only c code?
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.
-
- Posts: 14
- Joined: Fri Jul 08, 2016 8:44 pm
Re: is it available to get time using only c code?
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));
}
after doing so, still cant prints out `hour` variable or any other variable of the code
what mistake i've done?