123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- ; WTFStopW : a simple stopwatch
- ; Copyright (C) 2018 Weber Yann
- ;
- ; This program is free software; you can redistribute it and/or modify
- ; it under the terms of the GNU General Public License as published by
- ; the Free Software Foundation; either version 3 of the License, or
- ; any later version.
- ;
- ; This program is distributed in the hope that it will be useful,
- ; but WITHOUT ANY WARRANTY; without even the implied warranty of
- ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ; GNU General Public License for more details.
- ;
- ; You should have received a copy of the GNU General Public License
- ; along with this program. If not, see <http://www.gnu.org/licenses/>.
-
- ;
- ; A simple precise stopwatch
- ; Build : nasm -felf64 wtfstopw.asm && ld -s -melf_x86_64 wtfstopw.o -o wtfstopw
- ; Build Debug : nasm -g -F dwarf -felf64 -l wtfstopw.lst wtfstopw.asm && ld -melf_x86_64 wtfstopw.o -o wtfstopw
- ;
- ; ./wtfstopw [-h] [-r NDIGITS]
- ; Options :
- ; -h print this help and exit
- ; -r number of digits bellow seconds to display, default is 2
- ; Interact :
- ; press enter to exit
- ; send SIGINT (with kill -2 ir ctrl + c) for a new lap
- [bits 64]
-
- %define O_NONBLOCK 0x800
-
- STRUC TIMESPEC_STRUC
- .tv_sec: resq 1
- .tv_nsec: resq 1
- ENDSTRUC
-
-
- %macro TIMESPEC 1
- %1: ISTRUC TIMESPEC_STRUC
- at TIMESPEC_STRUC.tv_sec, resq 1
- at TIMESPEC_STRUC.tv_nsec, resq 1
- IEND
- %define %1.tv_sec %1+TIMESPEC_STRUC.tv_sec
- %define %1.tv_nsec %1+TIMESPEC_STRUC.tv_nsec
- %endmacro
-
- STRUC SIGACTION_STRUC
- .sa_handler: resq 1
- .sa_flags: resq 1
- .sa_restorer: resq 1
- .sa_mask: resb 128
- ENDSTRUC
-
- ; from https://www.linuxnasm.be/home/library/lib-includes/signals-inc
- %macro SIGACTION 1
- %1: ISTRUC SIGACTION_STRUC
- at SIGACTION_STRUC.sa_handler, dq 0
- at SIGACTION_STRUC.sa_flags, dq 0
- at SIGACTION_STRUC.sa_restorer, dq 0
- at SIGACTION_STRUC.sa_mask, times 128 db 0
- IEND
- %define sigaction.sa_handler sigaction+SIGACTION_STRUC.sa_handler
- %define sigaction.sa_flags sigaction+SIGACTION_STRUC.sa_flags
- %define sigaction.sa_restorer sigaction+SIGACTION_STRUC.sa_restorer
- %define sigaction.sa_mask sigaction+SIGACTION_STRUC.sa_mask
- %endmacro
-
- section .bss
- align 8
- TIMESPEC ts_start
- TIMESPEC ts_cur
- TIMESPEC ts_sleep
- buf: resb 1
-
- section .data
-
- time_res: dq 2 ; 2 digits bellow seconds can grow to 8
- fcntl_flag: dq 0
- SIGACTION sigaction
-
- align 8
- hours: times 8 db '0' ; allows storing max hours in 1<<64 secs
- timestr: db ":00:00.0 ", 0
- align 8
- timestrlen: equ $ - timestr
- hourslen: equ timestr - hours ; hours len -> the max number of digits
-
- lapsmsg: db "Lap : "
- lapsmsglen: equ $ - lapsmsg
-
- lapcount: times 20 db '0' ; allows storing decimal repr of 1<<64
- lapcountlen: equ $ - lapcount
- laplen: dq 2
-
- startmsg: db "Press Enter or ctrl+d to exit and ctrl+c for new lap."
- db 0xA
- startmsglen: equ $ - startmsg
-
- usage_pre: db "Usage : "
- usage_prelen: equ $ - usage_pre
-
- usage_post: db " [-h] [-r NDIGITS]", 0xA
- db "Options :", 0xA
- db 0x9, "-h print this help and exit", 0xA
- db 0x9, "-r Number of digits bellow seconds, between [1..8]"
- db 0xA
- db "Interactions :", 0xA
- db 0x9, "Press enter or close stdin to exit", 0xA
- db 0x9, "Send SIGINT (with kill -2 or ctrl + c) for a new lap"
- db 0xA
- db 0x9, "Elapsed time is sent on stderr and laps infos on stdout"
- db 0xA
- usage_postlen: equ $ - usage_post
-
- badarg_msg: db "Unexpected argument : "
- badarg_msglen: equ $ - badarg_msg
-
- badval_msg: db "Value for -r should be in [1..8] but got "
- badval_msglen: equ $ - badval_msg
-
- faultmsg: db "Fault !", 0xA
- faultmsglen: equ $ - faultmsg
-
- nl: db 0x0A
- cr: db 0xd
-
-
- section .text
- global _start
- _start:
-
- ; parse arguments and set time_res value
- jmp arg_parse
- arg_ok:
-
- ; set stdin reads non blocking
- xor rdx, rdx
- xor rdi, rdi
- mov rax, 72 ; fcntl
- mov rsi, 3 ; F_GETFL
- syscall
- mov [fcntl_flag], rax
- mov rdx, rax
- or rdx, O_NONBLOCK
- mov rax, 72 ; fcntl
- mov rsi, 4 ; F_SETFL
- syscall
- cmp rax, 0
- jne fault
-
- ; preparing SIGINT catch
- mov rax, proc_lap_handler
- mov qword [sigaction.sa_handler], rax
- mov eax, 0x14000000 ; SA_RESTART | SA_RESTORER
- mov dword [sigaction.sa_flags], eax
- mov rax, sig_restorer
- mov qword [sigaction.sa_restorer], rax
-
- mov rax, 13 ; sys_rt_sigaction
- mov rdi, 2 ; SIGINT
- mov rsi, sigaction
- mov rdx, 0 ; NULL
- mov r10, 8 ; sig_size
- syscall
- cmp rax, 0
- jne fault
-
- mov rax, 228 ; clock_gettime
- mov rdi, 0 ; CLOCK_REALTIME
- mov rsi, ts_start
- syscall
-
- mov rax, 1 ; write
- mov rdi, 2 ; stderr
- mov rsi, startmsg
- mov rdx, startmsglen
- syscall
-
- ; set value for ts_sleep.tv_nsec given time_res
- ; div sleep time by 10 for each digits added bellow seconds
- mov qword [ts_sleep.tv_sec], 0
- mov rax, 100000000
- mov r8, [time_res]
- mov r9, 10
- xor rdx, rdx
- setsleep_loop:
- cmp r8, 1
- jle setsleep_endloop
- div r9
- sub r8, 1
- jmp setsleep_loop
- setsleep_endloop:
- mov [ts_sleep.tv_nsec], rax
-
- std ; set DF for string operations
- main_loop:
- push 2 ; stderr
- push 0xD ; \r
- call proc_print_time
-
- ; 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
-
- mov rax, 35 ; nanosleep
- mov rdi, ts_sleep
- mov rsi, 0
- syscall
-
- jmp main_loop ; main_loop
-
- flush_stdin:
- mov rax, 0
- mov rdi, 0
- mov rsi, buf
- mov rdx, 1
- syscall
- cmp rax, 0
- je newline_exit
- jg flush_stdin
-
- mov rdi, 0 ; EXIT OK
- ;
- ; Expect rdi to be the return code
- ;
- exit:
- push rdi
- ; restoring stdin state
- mov rax, 72 ; fcntl
- xor rdi, rdi
- mov rsi, 4 ; F_SETFL
- mov rdx, [fcntl_flag]
- syscall
- cmp rax, 0
- je exit_end
- pop rdi ; failed to restore
- push 1 ; exit FAIL
-
- exit_end:
- mov rax, 60 ; sys_exit
- pop rdi ; return code
- syscall
- fault:
- mov rdi, 2 ; stderr
- call proc_nl
- mov rax, 1
- mov rsi, faultmsg
- mov rdx, faultmsglen
- syscall
-
- mov rdi, 1 ; failure
- jmp exit
-
- newline_exit:
- mov rdi, 1
- call proc_nl
-
- mov rdi, 0 ; exit OK
- jmp exit
-
- ;
- ; Print current time on FD and add a leading char CHR
- ; push FD & push CHR to set arguments
- ; Warning : DF must be set
- ;
- proc_print_time:
-
- mov rax, 228 ; clock_gettime
- mov rdi, 0 ; CLOCK_REALTIME
- mov rsi, ts_cur
- syscall ; updating ts_cur time
-
- mov rax, [ts_cur.tv_nsec]
- sub rax, [ts_start.tv_nsec]
- cmp rax, 0 ; Calculating elapsed ns
- jge print_time_us_cont
- add rax, 1000000000 ; negativ result
- sub qword [ts_cur.tv_sec], 1
- print_time_us_cont:
-
- ; set the nanosec chars (time_res chars) in timestr
- xor rdx, rdx
- div qword [ts_sleep.tv_nsec] ; Divide result given time_res
- mov rdi, timestr + 6
- mov rsi, rdi ; rsi points on 1st nanosec digit
- add rdi, [time_res] ; rdi points on last nanosec digit
- mov r8, 10
- print_time_us_loop:
- xor rdx, rdx
- div r8
- xchg al, dl
- add al, 0x30
- stosb
- mov al, dl
- cmp rsi, rdi
- loopne print_time_us_loop
-
- ; filling timestr with seconds & minutes chars
- mov rax, [ts_cur.tv_sec]
- sub rax, [ts_start.tv_sec] ; rax now contain elapsed seconds
-
- mov r8, 10
- mov r9, 6
-
- xor rdx, rdx
- div r8
- add dl, 0x30
- mov byte [timestr + 5], dl
-
- xor dl, dl
- div r9
- add dl, 0x30
- mov byte [timestr + 4], dl
-
- xor dl, dl
- div r8
- add dl, 0x30
- mov byte [timestr + 2], dl
-
- xor dl, dl
- div r9
- add dl, 0x30
- mov byte[timestr + 1], dl
-
- ; filling the hours buffer
- ; rcx will contain max_digits - digits_count
- ; digits max is hourslen - 1
- mov rcx, hourslen - 1
- mov rdi, hours + hourslen - 1
- mov r10, 10
- procpt_loop:
- xor dl, dl
- div r10
- cmp rax, 0
- jne procpt_loopcont
- cmp dl, 0
- je procpt_loop_end
-
- procpt_loopcont:
- xchg al, dl
- add al, 0x30
- stosb
- mov al, dl
- cmp rcx, 0
- je fault
- cmp rax, 0
- loopne procpt_loop
- procpt_loop_end:
-
- ; set rcx for 2 digits at least for hours
- mov r9, hourslen - 2
- add rcx, 1
- cmp rcx, r9
- cmovnle rcx, r9
-
- mov r10, [rsp + 8] ; leading chr as 1st argument
- mov byte [timestr+timestrlen-1], r10b
-
- mov rax, 1 ; write
- mov rdi, [rsp + 16] ; fd as 2nd argument
- mov rdx, timestrlen + hourslen
- sub rdx, rcx ; timestr + hours len
- lea rsi, [hours + rcx] ; hours start pointer
- syscall
-
- ret 16
-
- ;
- ; sig handler for SIGINT displaying lap count and time on stdout
- ;
- proc_lap_handler:
- mov rax, 1
- mov rdi, 2
- mov rsi, cr
- mov rdx, 1
- syscall ; \r on stderr
-
- mov rax, 1
- mov rdi, 1
- mov rsi, lapsmsg
- mov rdx, 4 ; "Lap "
- syscall
-
- ; increment the lapcount str directly
- mov rbx, lapcount ; first digit ptr
- add rbx, lapcountlen ; rightmost digit ptr
- sub rbx, 1
- mov r8, 1 ; counter
- lap_handler_inc_lap:
- mov r10b, [rbx]
- cmp r10b, 0x39 ; '9'
- jl lap_handler_inc_end
-
- add r8, 1
- cmp r8, [laplen]
- jl lap_handler_laplen_noupd
- mov [laplen], r8 ; update laplen
-
- lap_handler_laplen_noupd:
- mov byte [rbx], 0x30 ; set current digit to '0'
- sub rbx, 1
- cmp rbx, lapcount
- jl fault
- jmp lap_handler_inc_lap
- lap_handler_inc_end:
- add r10b, 1
- mov [rbx], r10b
-
- mov rax, 1
- mov rdi, 1 ; stdout
- mov rdx, [laplen]
- mov rsi, lapcount
- add rsi, lapcountlen
- sub rsi, rdx ; leftmost digit ptr
- syscall
-
- mov rax, 1 ; write
- mov rsi, lapsmsg + 4
- mov rdx, 3 ; " : "
- syscall
-
- std ; set DF for string operations
- push 1 ; stdout
- push 0xA ; \n
- call proc_print_time
-
- ret
-
- sig_restorer:
- mov rax, 15 ; sys_rt_sigreturn
- xor rdi, rdi
- syscall
-
- ;
- ; Argument parsing
- ;
- arg_parse:
- ; Checking argument count
- mov rax, [rsp]
- cmp rax, 1
- je arg_ok
- cmp rax, 3
- jle parse_r
- arg_err: ; badval & badarg jmp here too
- mov rax, [rsp+8] ; argv[0] program name
- mov rdi, -1 ; return status
- jmp usage
- parse_r:
- mov rax, [rsp+16] ; 1st arg should be "-r"
- mov bl, [rax]
- cmp bl, '-'
- jne badarg
- mov bl, [rax+1]
- cmp bl, 'h' ; -h
- je arg_err
- cmp bl, 'r'
- jne badarg
- mov bl, [rax+2]
- cmp bl, 0
- jne arg_nxt ; the value seems to be just after the -r like -r2
- nxt_arg:
- ; the 1st argument is -r the second must be the time_res
- ; check that the arg exists
- mov rax, [rsp]
- cmp rax, 3
- jne arg_err
- mov rax, [rsp+24]
- jmp arg_cont
- arg_nxt:
- ; check that there is no more args
- mov rbx, [rsp]
- cmp rbx, 2
- jne arg_err
- add rax, 2
- arg_cont:
- ; rax should point on the value
- mov bl, [rax+1]
- cmp bl, 0
- jne badval
- xor rbx, rbx
- mov bl, [rax]
- cmp bl, '1'
- jl badval
- cmp bl, '8'
- jg badval
- sub bl, '0'
- mov [time_res], rbx
- jmp arg_ok
-
- ; print an error message, usage and exit with rax pointing the value
- badval:
- mov rsi, badval_msg
- mov rdx, badval_msglen
- jmp arg_strerr
-
- ; print an error message, usage and exit
- badarg:
- mov rsi, badarg_msg
- mov rdx, badarg_msglen
- mov rax, [rsp+16]
- jmp arg_strerr
-
- ; rsi msg ptr, rdx msg len, rax arg ptr
- arg_strerr:
- mov r8, rax
- mov rax, 1
- mov rdi, 1
- syscall
- mov rsi, r8
- mov rax, r8
- call proc_strlen
- mov rax, 1
- syscall
- call proc_nl
- call proc_nl
- jmp arg_err
-
- ;
- ; Print usage and exit
- ; Except rax to point on programm name and rdi to be the return code
- ;
- usage:
- push rdi
- push rax
-
- mov rax, 1 ; write
- mov rdi, 1 ; stdout
- mov rsi, usage_pre
- mov rdx, usage_prelen
- syscall
-
- pop rsi
- mov rax, rsi
- call proc_strlen
- mov rax, 1
- syscall
-
- mov rax, 1
- mov rsi, usage_post
- mov rdx, usage_postlen
- syscall
-
- mov rax, 60
- pop rdi
- syscall
-
- ; With rax pointing on the string, the result will be in rdx
- proc_strlen:
- mov r8, rax
- mov r9, r8
- sub r9, 1
- strlen_loop:
- add r9, 1
- mov al, [r9]
- cmp al, 0
- jne strlen_loop
- sub r9, r8
- mov rdx, r9
- ret
-
- ; with rdi the fd
- proc_nl:
- mov rax, 1
- mov rsi, nl
- mov rdx, 1
- syscall
- ret
-
- .end:
|