Page 1 of 1

is it available to get time using only c code?

Posted: Fri Jul 08, 2016 8:54 pm
by Mohamed007
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?

Posted: Fri Jul 08, 2016 9:45 pm
by alexfru
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?

Posted: Fri Jul 08, 2016 11:53 pm
by Brendan
Hi,
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?
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).

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

Re: is it available to get time using only c code?

Posted: Sat Jul 09, 2016 2:55 am
by Octacone
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.

Re: is it available to get time using only c code?

Posted: Sat Jul 09, 2016 2:56 am
by Mohamed007
alexfru 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.
im using gcc and there is no <time.h> library available

Re: is it available to get time using only c code?

Posted: Sat Jul 09, 2016 3:01 am
by Mohamed007
Brendan wrote:Hi,
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?
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).

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
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)

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));
}
*uint8 and uint16 is a defined types by me*
after doing so, still cant prints out `hour` variable or any other variable of the code

what mistake i've done?