Page 1 of 1

Getting Real-Clock Time

Posted: Mon Mar 24, 2008 3:44 am
by Marine
Hey,

Love your tutorials, definately useful in gaining knowledge in ASM and how the internal cogs of modern PC's work (Computing class in college only goes so far)

However, i want to be able to make a timer, and my kernel to sleep for a few seconds when loading (So you see my load screen rather than it flash off :P)

I have tried the following in my kernel Main() function (just to test it), and it resets bochs.

Code: Select all

__asm
{
   mov ah,00h
   int 1Ah
}

Is there a better way to make a timer/sleep method, or is this the right way just not working properly? :D

Posted: Mon Mar 24, 2008 3:11 pm
by Andyhhp
Hi,

That piece of code wont work because in the kernel, you are in protected mode so all interrupts cause triple fault (sounds like what you are getting with bochs) until you write your own interrupt table and handlers.

Also, it seems that you are trying to use a DOS interrupt not a BIOS one (cant remember where the split is so this might not be accurate). This means that it will not work unless you are using DOS as an OS (which you arnt).

My suggestions are either:

1) write interrupt handlers and learn how to use the Programmable Interrupt Timer to signal when a certain time has elapsed.

2) If you have a Pentium Processor or later, you can use the RDTSC (ReaD TimeStamp Counter) instruction. There is information about its complete use in the Intel Reference manuals (253666 and 253667). Basically, it is a counter that is incremented on every clock tick.

e.g.

Code: Select all

wait_time: dq 0;reserve 64bits for value
...
rdtsc ;loads edx:eax with timestamp
add eax,0x3b9aca00 ;add 1billion to tsc value (1 second on a 1Ghz processor - you will have to alter this value to get a decent time)
adc edx,0 ;make sure any carry is accounted for

mov dword [wait_time],edx
mov dword [wait_time+4],eax ;store values

wait_loop:
rdtsc
cmp edx,dword [wait_time]
jl wait_loop
cmp eax,dword [wait_time+4]
jl wait_loop

;Your specified time has now elapsed
Note: This is untested code but its meaning should be clear.

Hope this helps,

Andrew

Posted: Mon Mar 24, 2008 4:57 pm
by Marine
Thanks, i thought it would be something like that stopping me :D

I'll have a look at interrupt tables right now.