How can I access system time using NASM?

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

+6
assembly nasm
source share
4 answers
%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 

It uses NASM and is here .

+5
source share

Using linux:

 mov eax, 13 push eax mov ebx, esp int 0x80 pop eax 

Print the current unix time in eax (just replace the last command if you want it to pop into a different register).

This is the sys_time system call (system call number 13), and it returns the time to the memory location in ebx, which is the top of the stack.

+6
source share

I would say, depending on which platform you are on, you will have to use the OS function.

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

+1
source share

With NASM, if you are targeting Linux x86-64, you can simply do the following:

 mov rax, 201 xor rdi, rdi syscall 

201 corresponds to the 64-bit system call number for sys_time (as indicated here ). The rdi register rdi set to 0, so the return value after making a system call is stored in rax , but you can also specify its location of your choice. The result is expressed in seconds since the Epoch.

More information about this system call can be found in the time page .

0
source share

All Articles