WTFStopW : a simple stopwatch
x86-64
nasm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wtfstopw.asm 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. ; WTFStopW : a simple stopwatch
  2. ; Copyright (C) 2018 Weber Yann
  3. ;
  4. ; This program is free software; you can redistribute it and/or modify
  5. ; it under the terms of the GNU General Public License as published by
  6. ; the Free Software Foundation; either version 3 of the License, or
  7. ; any later version.
  8. ;
  9. ; This program is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;
  17. ; A simple precise stopwatch
  18. ; Build : nasm -felf64 wtfstopw.asm && ld -s -melf_x86_64 wtfstopw.o -o wtfstopw
  19. ; Build Debug : nasm -g -F dwarf -felf64 -l wtfstopw.lst wtfstopw.asm && ld -melf_x86_64 wtfstopw.o -o wtfstopw
  20. ;
  21. ; ./wtfstopw [-h] [-r NDIGITS]
  22. ; Options :
  23. ; -h print this help and exit
  24. ; -r number of digits bellow seconds to display, default is 2
  25. ; Interact :
  26. ; press enter to exit
  27. ; send SIGINT (with kill -2 ir ctrl + c) for a new lap
  28. [bits 64]
  29. %define O_NONBLOCK 0x800
  30. STRUC TIMESPEC_STRUC
  31. .tv_sec: resq 1
  32. .tv_nsec: resq 1
  33. ENDSTRUC
  34. %macro TIMESPEC 1
  35. %1: ISTRUC TIMESPEC_STRUC
  36. at TIMESPEC_STRUC.tv_sec, resq 1
  37. at TIMESPEC_STRUC.tv_nsec, resq 1
  38. IEND
  39. %define %1.tv_sec %1+TIMESPEC_STRUC.tv_sec
  40. %define %1.tv_nsec %1+TIMESPEC_STRUC.tv_nsec
  41. %endmacro
  42. STRUC SIGACTION_STRUC
  43. .sa_handler: resq 1
  44. .sa_flags: resq 1
  45. .sa_restorer: resq 1
  46. .sa_mask: resb 128
  47. ENDSTRUC
  48. ; from https://www.linuxnasm.be/home/library/lib-includes/signals-inc
  49. %macro SIGACTION 1
  50. %1: ISTRUC SIGACTION_STRUC
  51. at SIGACTION_STRUC.sa_handler, dq 0
  52. at SIGACTION_STRUC.sa_flags, dq 0
  53. at SIGACTION_STRUC.sa_restorer, dq 0
  54. at SIGACTION_STRUC.sa_mask, times 128 db 0
  55. IEND
  56. %define sigaction.sa_handler sigaction+SIGACTION_STRUC.sa_handler
  57. %define sigaction.sa_flags sigaction+SIGACTION_STRUC.sa_flags
  58. %define sigaction.sa_restorer sigaction+SIGACTION_STRUC.sa_restorer
  59. %define sigaction.sa_mask sigaction+SIGACTION_STRUC.sa_mask
  60. %endmacro
  61. section .bss
  62. align 8
  63. TIMESPEC ts_start
  64. TIMESPEC ts_cur
  65. TIMESPEC ts_sleep
  66. buf: resb 1
  67. section .data
  68. time_res: dq 2 ; 2 digits bellow seconds can grow to 8
  69. fcntl_flag: dq 0
  70. SIGACTION sigaction
  71. align 8
  72. hours: times 8 db '0' ; allows storing max hours in 1<<64 secs
  73. timestr: db ":00:00."
  74. times 8 db '0'
  75. times 0xE db ' '
  76. db 0
  77. timestrend: align 8
  78. timestrlen: equ timestrend - timestr
  79. hourslen: equ timestr - hours ; hours len -> the max number of digits
  80. lapsmsg: db "Lap : "
  81. lapsmsglen: equ $ - lapsmsg
  82. lapcount: times 20 db '0' ; allows storing decimal repr of 1<<64
  83. lapcountlen: equ $ - lapcount
  84. laplen: dq 2
  85. startmsg: db "Press Enter or ctrl+d to exit and ctrl+c for new lap."
  86. db 0xA
  87. startmsglen: equ $ - startmsg
  88. usage_pre: db "Usage : "
  89. usage_prelen: equ $ - usage_pre
  90. usage_post: db " [-h] [-r NDIGITS]", 0xA
  91. db "Options :", 0xA
  92. db 0x9, "-h print this help and exit", 0xA
  93. db 0x9, "-r Number of digits bellow seconds, between [1..8]"
  94. db 0xA
  95. db "Interactions :", 0xA
  96. db 0x9, "Press enter or close stdin to exit", 0xA
  97. db 0x9, "Send SIGINT (with kill -2 or ctrl + c) for a new lap"
  98. db 0xA
  99. db 0x9, "Elapsed time is sent on stderr and laps infos on stdout"
  100. db 0xA
  101. usage_postlen: equ $ - usage_post
  102. badarg_msg: db "Unexpected argument : "
  103. badarg_msglen: equ $ - badarg_msg
  104. badval_msg: db "Value for -r should be in [1..8] but got "
  105. badval_msglen: equ $ - badval_msg
  106. faultmsg: db "Fault !", 0xA
  107. faultmsglen: equ $ - faultmsg
  108. nl: db 0x0A
  109. cr: db 0xd
  110. section .text
  111. global _start
  112. _start:
  113. ; parse arguments and set time_res value
  114. jmp arg_parse
  115. arg_ok:
  116. ; set stdin reads non blocking
  117. xor rdx, rdx
  118. xor rdi, rdi
  119. mov rax, 72 ; fcntl
  120. mov rsi, 3 ; F_GETFL
  121. syscall
  122. mov [fcntl_flag], rax
  123. mov rdx, rax
  124. or rdx, O_NONBLOCK
  125. mov rax, 72 ; fcntl
  126. mov rsi, 4 ; F_SETFL
  127. syscall
  128. cmp rax, 0
  129. jne fault
  130. ; preparing SIGINT catch
  131. mov rax, proc_lap_handler
  132. mov qword [sigaction.sa_handler], rax
  133. mov eax, 0x14000000 ; SA_RESTART | SA_RESTORER
  134. mov dword [sigaction.sa_flags], eax
  135. mov rax, sig_restorer
  136. mov qword [sigaction.sa_restorer], rax
  137. mov rax, 13 ; sys_rt_sigaction
  138. mov rdi, 2 ; SIGINT
  139. mov rsi, sigaction
  140. mov rdx, 0 ; NULL
  141. mov r10, 8 ; sig_size
  142. syscall
  143. cmp rax, 0
  144. jne fault
  145. mov rax, 228 ; clock_gettime
  146. mov rdi, 0 ; CLOCK_REALTIME
  147. mov rsi, ts_start
  148. syscall
  149. mov rax, 1 ; write
  150. mov rdi, 2 ; stderr
  151. mov rsi, startmsg
  152. mov rdx, startmsglen
  153. syscall
  154. ; set value for ts_sleep.tv_nsec given time_res
  155. ; div sleep time by 10 for each digits added bellow seconds
  156. mov qword [ts_sleep.tv_sec], 0
  157. mov rax, 100000000
  158. mov r8, [time_res]
  159. mov r9, 10
  160. xor rdx, rdx
  161. setsleep_loop:
  162. cmp r8, 1
  163. jle setsleep_endloop
  164. div r9
  165. sub r8, 1
  166. jmp setsleep_loop
  167. setsleep_endloop:
  168. mov [ts_sleep.tv_nsec], rax
  169. std ; set DF for string operations
  170. main_loop:
  171. push 2 ; stderr
  172. push 0xD ; \r
  173. call proc_print_time
  174. ; Attempt to read from stdin
  175. ; if something read, enter has been pressed
  176. mov rax, 0
  177. mov rdi, 0
  178. mov rsi, buf
  179. mov rdx, 1
  180. syscall
  181. cmp rax, 0
  182. jge flush_stdin ; flush stdin and exit
  183. mov rax, 35 ; nanosleep
  184. mov rdi, ts_sleep
  185. mov rsi, 0
  186. syscall
  187. jmp main_loop ; main_loop
  188. flush_stdin:
  189. mov rax, 0
  190. mov rdi, 0
  191. mov rsi, buf
  192. mov rdx, 1
  193. syscall
  194. cmp rax, 0
  195. je newline_exit
  196. jg flush_stdin
  197. mov rdi, 0 ; EXIT OK
  198. ;
  199. ; Expect rdi to be the return code
  200. ;
  201. exit:
  202. push rdi
  203. ; restoring stdin state
  204. mov rax, 72 ; fcntl
  205. xor rdi, rdi
  206. mov rsi, 4 ; F_SETFL
  207. mov rdx, [fcntl_flag]
  208. syscall
  209. cmp rax, 0
  210. je exit_end
  211. pop rdi ; failed to restore
  212. push 1 ; exit FAIL
  213. exit_end:
  214. mov rax, 60 ; sys_exit
  215. pop rdi ; return code
  216. syscall
  217. fault:
  218. mov rdi, 2 ; stderr
  219. call proc_nl
  220. mov rax, 1
  221. mov rsi, faultmsg
  222. mov rdx, faultmsglen
  223. syscall
  224. mov rdi, 1 ; failure
  225. jmp exit
  226. newline_exit:
  227. mov rdi, 1
  228. call proc_nl
  229. mov rdi, 0 ; exit OK
  230. jmp exit
  231. ;
  232. ; Print current time on FD and add a leading char CHR
  233. ; push FD & push CHR to set arguments
  234. ; Warning : DF must be set
  235. ;
  236. proc_print_time:
  237. mov rax, 228 ; clock_gettime
  238. mov rdi, 0 ; CLOCK_REALTIME
  239. mov rsi, ts_cur
  240. syscall ; updating ts_cur time
  241. mov rax, [ts_cur.tv_nsec]
  242. sub rax, [ts_start.tv_nsec]
  243. cmp rax, 0 ; Calculating elapsed ns
  244. jge print_time_us_cont
  245. add rax, 1000000000 ; negativ result
  246. sub qword [ts_cur.tv_sec], 1
  247. print_time_us_cont:
  248. xor rdx, rdx
  249. div qword [ts_sleep.tv_nsec] ; Divide result given time_res
  250. ; set the nanosec chars (time_res chars) in timestr
  251. mov rbx, 0x2020202020202020
  252. mov rcx, [time_res]
  253. mov r8, 10
  254. procpt_loopns:
  255. xor rdx, rdx
  256. div r8
  257. add dl, '0'
  258. shl rbx, 8
  259. mov bl, dl
  260. loop procpt_loopns
  261. mov [timestr+7], rbx
  262. ; filling timestr with seconds & minutes chars
  263. mov rax, [ts_cur.tv_sec]
  264. sub rax, [ts_start.tv_sec] ; rax now contain elapsed seconds
  265. mov r8, 10
  266. mov r9, 6
  267. xor rdx, rdx
  268. div r8
  269. mov bh, dl
  270. xor dl, dl
  271. div r9
  272. mov bl, dl
  273. add bx, 0x3030
  274. mov word [timestr + 4], bx
  275. xor dl, dl
  276. div r8
  277. mov bh, dl
  278. xor dl, dl
  279. div r9
  280. mov bl, dl
  281. add bx, 0x3030
  282. mov word [timestr + 1], bx
  283. ; filling the hours buffer
  284. ; rcx will contain max_digits - digits_count
  285. ; digits max is hourslen - 1
  286. mov rcx, hourslen - 1
  287. mov rdi, hours + hourslen - 1
  288. mov r10, 10
  289. procpt_loop:
  290. xor dl, dl
  291. div r10
  292. cmp rax, 0
  293. jne procpt_loopcont
  294. cmp dl, 0
  295. je procpt_loop_end
  296. procpt_loopcont:
  297. xchg al, dl
  298. add al, 0x30
  299. stosb
  300. mov al, dl
  301. cmp rcx, 0
  302. je fault
  303. cmp rax, 0
  304. loopne procpt_loop
  305. procpt_loop_end:
  306. ; set rcx for 2 digits at least for hours
  307. mov r9, hourslen - 2
  308. add rcx, 1
  309. cmp rcx, r9
  310. cmovnle rcx, r9
  311. mov r10, [rsp + 8] ; leading chr as 1st argument
  312. mov byte [timestr+timestrlen-1], r10b
  313. mov rax, 1 ; write
  314. mov rdi, [rsp + 16] ; fd as 2nd argument
  315. mov rdx, timestrlen + hourslen
  316. sub rdx, rcx ; timestr + hours len
  317. lea rsi, [hours + rcx] ; hours start pointer
  318. syscall
  319. ret 16
  320. ;
  321. ; sig handler for SIGINT displaying lap count and time on stdout
  322. ;
  323. proc_lap_handler:
  324. mov rax, 1
  325. mov rdi, 2
  326. mov rsi, cr
  327. mov rdx, 1
  328. syscall ; \r on stderr
  329. mov rax, 1
  330. mov rdi, 1
  331. mov rsi, lapsmsg
  332. mov rdx, 4 ; "Lap "
  333. syscall
  334. ; increment the lapcount str directly
  335. mov rcx, lapcountlen - 1; digit counter starting by the right most
  336. proclap_loop:
  337. cmp byte [lapcount + rcx], '9'
  338. jl proclap_loopend
  339. mov byte [lapcount + rcx], '0' ; set current digit to '0'
  340. loop proclap_loop
  341. proclap_loopend:
  342. add byte [lapcount + rcx], 1 ; increment current digit
  343. mov rdx, lapcountlen
  344. sub rdx, rcx
  345. cmp rdx, [laplen] ; update laplen if needed
  346. cmovl rdx, [laplen]
  347. mov [laplen], rdx
  348. mov rsi, lapcount + lapcountlen
  349. sub rsi, rdx
  350. mov rax, 1
  351. mov rdi, 1 ; stdout
  352. syscall
  353. mov rax, 1 ; write
  354. mov rsi, lapsmsg + 3
  355. mov rdx, 3 ; " : "
  356. syscall
  357. std ; set DF for string operations
  358. push 1 ; stdout
  359. push 0xA ; \n
  360. call proc_print_time
  361. ret
  362. sig_restorer:
  363. mov rax, 15 ; sys_rt_sigreturn
  364. xor rdi, rdi
  365. syscall
  366. ;
  367. ; Argument parsing
  368. ;
  369. arg_parse:
  370. ; Checking argument count
  371. mov rax, [rsp]
  372. cmp rax, 1
  373. je arg_ok
  374. cmp rax, 3
  375. jle parse_r
  376. arg_err: ; badval & badarg jmp here too
  377. mov rax, [rsp+8] ; argv[0] program name
  378. mov rdi, -1 ; return status
  379. jmp usage
  380. parse_r:
  381. mov rax, [rsp+16] ; 1st arg should be "-r"
  382. mov bl, [rax]
  383. cmp bl, '-'
  384. jne badarg
  385. mov bl, [rax+1]
  386. cmp bl, 'h' ; -h
  387. je arg_err
  388. cmp bl, 'r'
  389. jne badarg
  390. mov bl, [rax+2]
  391. cmp bl, 0
  392. jne arg_nxt ; the value seems to be just after the -r like -r2
  393. nxt_arg:
  394. ; the 1st argument is -r the second must be the time_res
  395. ; check that the arg exists
  396. mov rax, [rsp]
  397. cmp rax, 3
  398. jne arg_err
  399. mov rax, [rsp+24]
  400. jmp arg_cont
  401. arg_nxt:
  402. ; check that there is no more args
  403. mov rbx, [rsp]
  404. cmp rbx, 2
  405. jne arg_err
  406. add rax, 2
  407. arg_cont:
  408. ; rax should point on the value
  409. mov bl, [rax+1]
  410. cmp bl, 0
  411. jne badval
  412. xor rbx, rbx
  413. mov bl, [rax]
  414. cmp bl, '1'
  415. jl badval
  416. cmp bl, '8'
  417. jg badval
  418. sub bl, '0'
  419. mov [time_res], rbx
  420. jmp arg_ok
  421. ; print an error message, usage and exit with rax pointing the value
  422. badval:
  423. mov rsi, badval_msg
  424. mov rdx, badval_msglen
  425. jmp arg_strerr
  426. ; print an error message, usage and exit
  427. badarg:
  428. mov rsi, badarg_msg
  429. mov rdx, badarg_msglen
  430. mov rax, [rsp+16]
  431. jmp arg_strerr
  432. ; rsi msg ptr, rdx msg len, rax arg ptr
  433. arg_strerr:
  434. mov r8, rax
  435. mov rax, 1
  436. mov rdi, 1
  437. syscall
  438. mov rsi, r8
  439. mov rax, r8
  440. call proc_strlen
  441. mov rax, 1
  442. syscall
  443. call proc_nl
  444. call proc_nl
  445. jmp arg_err
  446. ;
  447. ; Print usage and exit
  448. ; Except rax to point on programm name and rdi to be the return code
  449. ;
  450. usage:
  451. push rdi
  452. push rax
  453. mov rax, 1 ; write
  454. mov rdi, 1 ; stdout
  455. mov rsi, usage_pre
  456. mov rdx, usage_prelen
  457. syscall
  458. pop rsi
  459. mov rax, rsi
  460. call proc_strlen
  461. mov rax, 1
  462. syscall
  463. mov rax, 1
  464. mov rsi, usage_post
  465. mov rdx, usage_postlen
  466. syscall
  467. mov rax, 60
  468. pop rdi
  469. syscall
  470. ; With rax pointing on the string, the result will be in rdx
  471. proc_strlen:
  472. mov r8, rax
  473. mov r9, r8
  474. dec r9
  475. strlen_loop:
  476. inc r9
  477. mov al, [r9]
  478. cmp al, 0
  479. jne strlen_loop
  480. sub r9, r8
  481. mov rdx, r9
  482. ret
  483. ; with rdi the fd
  484. proc_nl:
  485. mov rax, 1
  486. mov rsi, nl
  487. mov rdx, 1
  488. syscall
  489. ret
  490. .end: