Time and date syscalls
clock_gettime
Syscall 228 (clock_gettime) fills a struct timespec with seconds and nanoseconds. Pass the clock ID in RDI (0 for realtime) and a pointer to the struct in RSI:
%define SYS_CLOCK_GETTIME 228 section .bss ts resq 2 ; timespec: sec, nsec section .text mov rax, SYS_CLOCK_GETTIME mov rdi, 0 mov rsi, ts syscall
The timespec struct
The timespec is two 64-bit fields: seconds, then nanoseconds. Read them as two quadwords, and remember the nanosecond field wraps at one billion, so subtracting times needs a borrow check:
mov rax, [ts] ; seconds mov rdx, [ts+8] ; nanoseconds
Measuring elapsed time
Take two readings and subtract. If the end nanoseconds are smaller than the start, borrow one second: add 1,000,000,000 to the end nanoseconds and subtract one from the seconds. A complete program proves that the call succeeds and prints a marker:
default rel %define SYS_CLOCK_GETTIME 228 %define CLOCK_REALTIME 0 %define SYS_WRITE 1 %define SYS_EXIT 60 section .bss ts resq 2 section .data ok db "clock", 10 section .text global _start _start: mov rax, SYS_CLOCK_GETTIME mov rdi, CLOCK_REALTIME mov rsi, ts syscall test rax, rax js failed mov rax, SYS_WRITE mov rdi, 1 mov rsi, ok mov rdx, 6 syscall mov rax, SYS_EXIT xor rdi, rdi syscall failed: mov rax, SYS_EXIT mov rdi, 1 syscall
nasm -f elf64 -o clock.o clock.asm
ld -o clock clock.o
./clock
echo $?
clock
0
The short snippets on this page are fragments: they show the call and the struct fields. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which syscall number does clock_gettime use on x86-64 Linux?
Answer
228
The time syscalls are in the 200s, and clock_gettime is 228.
-
Which clock ID is CLOCK_REALTIME?
Answer
0
CLOCK_REALTIME is 0; CLOCK_MONOTONIC is 1, which is the better choice for elapsed time.
-
What two fields does a timespec hold?
Answer
Seconds and nanoseconds
Each field is a 64-bit integer: seconds first, then nanoseconds.
-
Which register points at the timespec for clock_gettime?
Answer
RSI
RDI is the clock ID and RSI the output buffer.
-
What does clock_gettime return in RAX on success?
Answer
0
The call returns 0 and writes the time into the struct; a negative value is an error.
-
Why borrow a second when subtracting times?
Answer
The nanosecond field wraps at one billion
If the end nanoseconds are smaller, one second must be moved into the nanosecond field.
-
What does this program print?
default rel %define SYS_CLOCK_GETTIME 228 %define CLOCK_REALTIME 0 %define SYS_WRITE 1 %define SYS_EXIT 60 section .bss ts resq 2 section .data ok db "clock", 10 section .text global _start _start: mov rax, SYS_CLOCK_GETTIME mov rdi, CLOCK_REALTIME mov rsi, ts syscall test rax, rax js failed mov rax, SYS_WRITE mov rdi, 1 mov rsi, ok mov rdx, 6 syscall mov rax, SYS_EXIT xor rdi, rdi syscall failed: mov rax, SYS_EXIT mov rdi, 1 syscallAnswer
clock
The call succeeds, so the program skips the failure branch and prints the marker.
-
Which clock should measure elapsed time, since it never jumps backwards?
Answer
CLOCK_MONOTONIC
CLOCK_MONOTONIC counts from an arbitrary start and is not changed by the system clock.
-
What exit status does the clock program return when the call succeeds?
Answer
0
The success path clears rdi before sys_exit.
-
How many 64-bit fields does the program reserve for the timespec?
Answer
2
resq 2reserves two quadwords, one for seconds and one for nanoseconds.
Article author: Arthur Isaev