如何使用NASM访问系统时间?

use*_*121 6 assembly nasm

我正在尝试使用当前系统时间为随机数生成器播种.如何使用NASM访问系统时间?(我正在使用linux)

jpo*_*ell 6

使用linux:

mov eax, 13
push eax
mov ebx, esp
int 0x80
pop eax
Run Code Online (Sandbox Code Playgroud)

将当前的unix时间弹出到eax中(如果你想将它弹出到另一个寄存器中,只需替换最后一条指令).

这是对sys_time(系统调用号13)的系统调用,它将时间返回到ebx中的内存位置,ebx是堆栈的顶部.


Kyl*_*ndo 5

%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
Run Code Online (Sandbox Code Playgroud)

这使用NASM,来自这里.


Pyv*_*ves 5

使用 NASM,如果您的目标是 Linux x86-64,则只需执行以下操作:

mov rax, 201
xor rdi, rdi        
syscall
Run Code Online (Sandbox Code Playgroud)

201sys_time对应于(如此处列出的的 64 位系统调用号。Registerrdi设置为 0,因此执行系统调用后的返回值存储在 中rax,但您也可以将其指向您选择的内存位置。结果以纪元以来的秒数表示。

有关此系统调用的更多信息可以在time 手册页中找到。