123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- [bits 64]
-
- section .text
- global _start
-
- _start:
-
- ; set stdin non blocking
- mov rax, 72 ; fcntl
- mov rdi, 0 ; stdin
- mov rsi, 3 ; F_GETFL
- mov rdx, 0
- syscall
- mov rdx, rax
- or rdx, 4 ; O_NONBLOCK
- mov rax, 72 ; fcntl
- mov rdi, 0 ; stdin
- mov rsi, 4 ; F_SETFL
- syscall
-
- mov rax, 228 ; clock_gettime
- mov rdi, 0 ; CLOCK_REALTIME
- mov rsi, ts_start
- syscall
-
-
- print_time:
-
- ; updating ts_cur time
- mov rax, 228 ; clock_gettime
- mov rdi, 0 ; CLOCK_REALTIME
- mov rsi, ts_cur
- syscall
-
- mov rax, [tv_cur_us]
- mov rbx, [tv_start_us]
- sub rax, rbx
- cmp rax, 0
- jge print_time_us_cont
- ; negativ result
- add rax, 1000000000
- mov rbx, [tv_cur_s]
- sub rbx, 1
- mov [tv_cur_s], rbx
- print_time_us_cont:
- xor rdx, rdx
- mov rcx, 100000
- div rcx
- ; set the us char in timestr
- mov r8, timestr
- add r8, 10 ; r8 points on last char before \r
- mov r9, 4 ; r9 count the number of digits
- print_time_us_loop:
- xor rdx, rdx
- mov rcx, 10
- div rcx
- add dl, 0x30
- mov [r8], dl
- sub r8, 1
- sub r9, 1
- cmp r9, 0
- jg print_time_us_loop
-
- ; handling seconds, minutes & hours
- mov rax, [tv_cur_s]
- mov rbx, [tv_start_s]
- sub rax, rbx
- ; rax now contain elapsed seconds
- ; filling timestr with seconds & minutes
- add rax, 3559
-
- xor rdx, rdx
- mov rcx, 10
- div rcx
- push rax
- mov rax, rdx
- add al, 0x30
- mov byte [timestr + 5], al
-
- pop rax
- xor rdx, rdx
- mov rcx, 6
- div rcx
- push rax
- mov rax, rdx
- add al, 0x30
- mov byte [timestr + 4], al
-
- pop rax
- xor rdx, rdx
- mov rcx, 10
- div rcx
- push rax
- mov rax, rdx
- add al, 0x30
- mov byte [timestr + 2], al
-
- pop rax
- xor rdx, rdx
- mov rcx, 6
- div rcx
- push rax
- mov rax, rdx
- add al, 0x30
- mov byte[timestr + 1], al
-
- ; filling the hours buffer
- ; r8 will contain our digits counter : max is 8
- mov r8, 8
- pop rax
- print_time_hours_loop:
- mov rcx, 10
- xor rdx, rdx
- div rcx
- add dl, 0x30
- cmp rax, 0
- jne print_time_hours_print_mod
- cmp rdx, 0
- je print_time_hours_loop_end
-
- print_time_hours_print_mod:
- mov r9, hours
- add r9, r8
- mov byte [r9], dl
- cmp r8, 0
- je fault
- sub r8, 1
- cmp rax, 0
- jne print_time_hours_loop
- print_time_hours_loop_end:
-
- ; print hours + timestr
- add r8, 1
- cmp r8, 7
- jle print_time_hours_cont
- mov r8, 7
- print_time_hours_cont:
- mov r9, hours
- add r9, r8
- mov rcx, 9
- sub rcx, r8
- add rcx, timestrlen
-
- mov rax, 1 ; write
- mov rdi, 1 ; stdout
- mov rsi, r9
- mov rdx, rcx
- syscall
-
- ; Attempt to read from stdin
- ; if something read, enter has been pressed
- mov rax, 0
- mov rdi, 0
- mov rsi, buf
- mov rdx, 1
- syscall
- cmp rax, 0
- jge flush_stdin ; flush stdin and exit
-
- sleep:
- mov rax, 35
- mov rdi, ts_sleep
- mov rsi, 0
- syscall
-
- jmp print_time ; main loop
-
- flush_stdin:
- mov rax, 0
- mov rdi, 0
- mov rsi, buf
- mov rdx, 1
- syscall
- cmp rax, 0
- je newline
- jg flush_stdin
-
- exit:
- mov rax, 60 ; sys_exit
- mov rdi, 0 ; OK
- syscall
- fault:
- mov rax, 60 ; sys_exit
- mov rdi, 1 ; failure
- syscall
-
- newline:
- mov rax, 1
- mov rdi, 1
- mov rsi, nl
- mov rdx, 1
- syscall
- jmp exit
-
-
- section .data
-
- ts_start:
- tv_start_s dq 0
- tv_start_us dq 0
-
-
- msg: db "0 Hello, world!", 10
- msglen: equ $ - msg
-
- hours: db "000000000"
- timestr: db ":00:00.0000 ", 0x0D
- timestrlen: equ $ - timestr
-
- nl: db 10
- buf: db 0
-
- ts_cur:
- tv_cur_s dq 0
- tv_cur_us dq 0
-
- ts_sleep:
- tv_sleep_s dd 0,0
- tv_sleep_us dd 10000000,0
-
-
- ;; 1/2s sleep
- ;ts_sleep:
- ; tv_sleep_s dq 0
- ; tv_sleep_us dq 500000000
-
-
- .end:
|