Search code examples
linuxassemblytimex86gettimeofday

How to get time in milliseconds in assembly?


I was able to get it in seconds, but how to get the value of milliseconds? I am trying with the gettimeofday system call:

sys_gettimeofday_ms:
enter   $0, $0
pushq   $0
pushq   $2
leaq    -16(%rbp), %rdi
mov     $96, %rax
syscall

movq    -16(%rbp), %rax

leave
ret

Solution

  • This might work (untested), targeting x86_64 Linux.

    sys_gettimeofday_ms:
        mov rax, 96
        lea rdi, [rsp - 16]
        xor esi, esi
        syscall
        mov ecx, 1000
        mov rax, [rdi + 8]
        xor edx, edx
        div rcx
        mov rdx, [rdi]
        imul rdx, rcx
        add rax, rdx
        ret