tags:

views:

286

answers:

3

I am trying to seed my random number generator with current system time. How can I access the system time using NASM? (I am using linux)

+2  A: 
%define RTCaddress  0x70
%define RTCdata  0x71

;Get time and date from RTC

.l1:    mov al,10   ;Get RTC register A
    out RTCaddress,al
    in al,RTCdata
    test al,0x80   ;Is update in progress?
    jne .l1    ; yes, wait

    mov al,0   ;Get seconds (00 to 59)
    out RTCaddress,al
    in al,RTCdata
    mov [RTCtimeSecond],al

    mov al,0x02   ;Get minutes (00 to 59)
    out RTCaddress,al
    in al,RTCdata
    mov [RTCtimeMinute],al

    mov al,0x04   ;Get hours (see notes)
    out RTCaddress,al
    in al,RTCdata
    mov [RTCtimeHour],al

    mov al,0x07   ;Get day of month (01 to 31)
    out RTCaddress,al
    in al,RTCdata
    mov [RTCtimeDay],al

    mov al,0x08   ;Get month (01 to 12)
    out RTCaddress,al
    in al,RTCdata
    mov [RTCtimeMonth],al

    mov al,0x09   ;Get year (00 to 99)
    out RTCaddress,al
    in al,RTCdata
    mov [RTCtimeYear],al

    ret

This uses NASM, and is from here.

Kyle Rozendo
I'm a little confused. Is this working on the assumption that the poster is using windows?
BigBeagle
Should work for any OS on x86 that maintains its system date/time.
Kyle Rozendo
Just tried -- Unfortunately, "out RTCaddress,al" fails on Vista64 because of privilege level.
PhiS
Ah, I don't really know then. As I said, I this should work on x86, not x64, let alone with Vista's glorious security, hehe.
Kyle Rozendo
A: 

You could make a call to interrupt 1Ah to get the clock ticks since midnight.

here are some people talking about formating the time from ticks

Sorskoot
On which systems will or won't this work?
PhiS
I only have experience with these interrupts on DOS on a x86. They're old, but I'm not sure if they'll work on other platforms. Have a look at this, maybe it helps: http://linux.ucla.edu/LDP/LDP/khg/HyperNews/get/khg/117/1/1/1.html
Sorskoot
Just tried on Vista. Doesn't seem to work there.
PhiS
A: 

I'd say, depending on what platform you're on, you'll have to use an OS function.

On windows, try GetSystemTime. On linux, try gettimeofday - see related question here.

PhiS