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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. STRUC TIMESPEC_STRUC
  30. .tv_sec: resq 1
  31. .tv_nsec: resq 1
  32. ENDSTRUC
  33. %macro TIMESPEC 3
  34. %1: ISTRUC TIMESPEC_STRUC
  35. at TIMESPEC_STRUC.tv_sec, dq %2
  36. at TIMESPEC_STRUC.tv_nsec, dq %3
  37. IEND
  38. %define %1.tv_sec %1+TIMESPEC_STRUC.tv_sec
  39. %define %1.tv_nsec %1+TIMESPEC_STRUC.tv_nsec
  40. %endmacro
  41. STRUC SIGACTION_STRUC
  42. .sa_handler: resq 1
  43. .sa_flags: resq 1
  44. .sa_restorer: resq 1
  45. .sa_mask: resb 128
  46. ENDSTRUC
  47. section .data
  48. sigaction: ISTRUC SIGACTION_STRUC
  49. at SIGACTION_STRUC.sa_handler, dq 0
  50. at SIGACTION_STRUC.sa_flags, dq 0
  51. at SIGACTION_STRUC.sa_restorer, dq 0
  52. at SIGACTION_STRUC.sa_mask, times 128 db 0
  53. IEND
  54. %define sigaction.sa_handler sigaction+SIGACTION_STRUC.sa_handler
  55. %define sigaction.sa_flags sigaction+SIGACTION_STRUC.sa_flags
  56. %define sigaction.sa_restorer sigaction+SIGACTION_STRUC.sa_restorer
  57. %define sigaction.sa_mask sigaction+SIGACTION_STRUC.sa_mask
  58. TIMESPEC ts_start, 0, 0
  59. TIMESPEC ts_cur, 0, 0
  60. TIMESPEC ts_sleep, 0, 100000000 ; set before mainloop
  61. faultmsg: db "Fault !", 0xA
  62. faultmsglen: equ $ - faultmsg
  63. startmsg: db "Press Enter or ctrl+d to exit and ctrl+c for new lap."
  64. db 0xA
  65. startmsglen: equ $ - startmsg
  66. usage_pre: db "Usage : "
  67. usage_prelen: equ $ - usage_pre
  68. usage_post: db " [-h] [-r NDIGITS]", 0xA
  69. db "Options :", 0xA
  70. db 0x9, "-h print this help and exit", 0xA
  71. db 0x9, "-r Number of digits bellow seconds, between [1..8]"
  72. db 0xA
  73. db "Interactions :", 0xA
  74. db 0x9, "Press enter or close stdin to exit", 0xA
  75. db 0x9, "Send SIGINT (with kill -2 or ctrl + c) for a new lap"
  76. db 0xA
  77. db 0x9, "Elapsed time is sent on stderr and laps infos on stdout"
  78. db 0xA
  79. usage_postlen: equ $ - usage_post
  80. badarg_msg: db "Unexpected argument : "
  81. badarg_msglen: equ $ - badarg_msg
  82. badval_msg: db "Value for -r should be in [1..8] but got "
  83. badval_msglen: equ $ - badval_msg
  84. hours: db "000000000" ; increase size of this string to increase hours max
  85. timestr: db ":00:00.0 ", 0x0a
  86. timestrlen: equ $ - timestr
  87. hourslen: equ timestr - hours ; hours len -> the max number of digits
  88. time_res: dq 2 ; 2 digits bellow seconds can grow to 8
  89. nl: db 0x0A
  90. buf: db 0
  91. lapsmsg: db 0x0d, "Lap : "
  92. lapsmsglen: equ $ - lapsmsg
  93. lapcount: db "00000000"
  94. lapcountlen: equ $ - lapcount
  95. laplen: dq 2
  96. fcntl_flag: dq 0
  97. section .text
  98. global _start
  99. _start:
  100. ; parse arguments and set time_res value
  101. jmp arg_parse
  102. arg_ok:
  103. ; set stdin non blocking
  104. xor rdx, rdx
  105. xor rdi, rdi
  106. mov rax, 72 ; fcntl
  107. mov rsi, 3 ; F_GETFL
  108. syscall
  109. mov [fcntl_flag], rax
  110. mov rdx, rax
  111. or rdx, 0x800 ; O_NONBLOCK
  112. mov rax, 72 ; fcntl
  113. mov rsi, 4 ; F_SETFL
  114. syscall
  115. cmp rax, 0
  116. jne fault
  117. ; initializing lapptr
  118. ; preparing SIGINT catch
  119. mov rax, proc_lap_handler
  120. mov qword [sigaction.sa_handler], rax
  121. mov eax, 0x14000000 ; SA_RESTART | SA_RESTORER
  122. mov dword [sigaction.sa_flags], eax
  123. mov rax, sig_restorer
  124. mov qword [sigaction.sa_restorer], rax
  125. mov rax, 13 ; sys_rt_sigaction
  126. mov rdi, 2 ; SIGINT
  127. mov rsi, sigaction
  128. mov rdx, 0 ; NULL
  129. mov r10, 8 ; sig_size
  130. syscall
  131. cmp rax, 0
  132. jne fault
  133. mov rax, 228 ; clock_gettime
  134. mov rdi, 0 ; CLOCK_REALTIME
  135. mov rsi, ts_start
  136. syscall
  137. mov rax, 1 ; write
  138. mov rdi, 2 ; stderr
  139. mov rsi, startmsg
  140. mov rdx, startmsglen
  141. syscall
  142. ; set value for ts_sleep.tv_nsec given time_res
  143. ; div sleep time by 10 for each digits added bellow seconds
  144. mov rax, 100000000
  145. mov r8, [time_res]
  146. mov r9, 10
  147. xor rdx, rdx
  148. setsleep_loop:
  149. cmp r8, 1
  150. jle setsleep_endloop
  151. div r9
  152. sub r8, 1
  153. jmp setsleep_loop
  154. setsleep_endloop:
  155. mov [ts_sleep.tv_nsec], rax
  156. std ; set DF for string operations
  157. main_loop:
  158. mov rax, 2 ; stderr
  159. mov rdi, 0xD ; \r
  160. call proc_print_time
  161. ; Attempt to read from stdin
  162. ; if something read, enter has been pressed
  163. mov rax, 0
  164. mov rdi, 0
  165. mov rsi, buf
  166. mov rdx, 1
  167. syscall
  168. cmp rax, 0
  169. jge flush_stdin ; flush stdin and exit
  170. mov rax, 35 ; nanosleep
  171. mov rdi, ts_sleep
  172. mov rsi, 0
  173. syscall
  174. jmp main_loop ; main_loop
  175. flush_stdin:
  176. mov rax, 0
  177. mov rdi, 0
  178. mov rsi, buf
  179. mov rdx, 1
  180. syscall
  181. cmp rax, 0
  182. je newline_exit
  183. jg flush_stdin
  184. mov rdi, 0 ; EXIT OK
  185. ;
  186. ; Expect rdi to be the return code
  187. ;
  188. exit:
  189. push rdi
  190. ; restoring stdin state
  191. mov rax, 72 ; fcntl
  192. xor rdi, rdi
  193. mov rsi, 4 ; F_SETFL
  194. mov rdx, [fcntl_flag]
  195. syscall
  196. cmp rax, 0
  197. je exit_end
  198. pop rdi ; failed to restore
  199. push 1 ; exit FAIL
  200. exit_end:
  201. mov rax, 60 ; sys_exit
  202. pop rdi ; return code
  203. syscall
  204. fault:
  205. mov rdi, 2 ; stderr
  206. call proc_nl
  207. mov rax, 1
  208. mov rsi, faultmsg
  209. mov rdx, faultmsglen
  210. syscall
  211. mov rdi, 1 ; failure
  212. jmp exit
  213. newline_exit:
  214. mov rdi, 1
  215. call proc_nl
  216. mov rdi, 0 ; exit OK
  217. jmp exit
  218. ;
  219. ; Print current time on FD and add a leading char CHR
  220. ; push FD & push CHR to set arguments
  221. ; rax the FS & rdi the leading CHR
  222. ; Warning : DF must be set
  223. ;
  224. proc_print_time:
  225. push rax
  226. push rdi
  227. ; updating ts_cur time
  228. mov rax, 228 ; clock_gettime
  229. mov rdi, 0 ; CLOCK_REALTIME
  230. mov rsi, ts_cur
  231. syscall
  232. ; Calculating elapsed ns
  233. mov rax, [ts_cur.tv_nsec]
  234. sub rax, [ts_start.tv_nsec]
  235. cmp rax, 0
  236. jge print_time_us_cont
  237. ; negativ result
  238. add rax, 1000000000
  239. sub qword [ts_cur.tv_sec], 1
  240. print_time_us_cont:
  241. ; Divide result given time_res
  242. xor rdx, rdx
  243. div qword [ts_sleep.tv_nsec]
  244. ; set the nsec char in timestr
  245. mov rdi, timestr + 6
  246. mov rsi, rdi
  247. add rdi, [time_res] ; r8 points on last char before \r
  248. mov r8, 10
  249. print_time_us_loop:
  250. xor rdx, rdx
  251. div r8
  252. xchg al, dl
  253. add al, 0x30
  254. stosb
  255. mov al, dl
  256. cmp rsi, rdi
  257. loopne print_time_us_loop
  258. ; handling seconds, minutes & hours
  259. mov rax, [ts_cur.tv_sec]
  260. sub rax, [ts_start.tv_sec]
  261. ; rax now contain elapsed seconds
  262. ; filling timestr with seconds & minutes
  263. xor rdx, rdx
  264. mov rcx, 10
  265. div rcx
  266. add dl, 0x30
  267. mov byte [timestr + 5], dl
  268. xor dl, dl
  269. mov rcx, 6
  270. div rcx
  271. add dl, 0x30
  272. mov byte [timestr + 4], dl
  273. xor dl, dl
  274. mov rcx, 10
  275. div rcx
  276. add dl, 0x30
  277. mov byte [timestr + 2], dl
  278. xor dl, dl
  279. mov rcx, 6
  280. div rcx
  281. add dl, 0x30
  282. mov byte[timestr + 1], dl
  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. print_time_hours_loop:
  290. xor dl, dl
  291. div r10
  292. cmp rax, 0
  293. jne print_time_hours_print_mod
  294. cmp dl, 0
  295. je print_time_hours_loop_end
  296. print_time_hours_print_mod:
  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 print_time_hours_loop
  305. print_time_hours_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. pop r10 ; pop leading char argument
  312. mov byte [timestr+timestrlen-1], r10b
  313. mov rax, 1 ; write
  314. pop rdi ; pop fd argument
  315. mov rdx, timestrlen + 9
  316. sub rdx, rcx ; timestr + hours len
  317. lea rsi, [hours + rcx] ; hours start pointer
  318. syscall
  319. ret
  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, 1
  326. mov rsi, lapsmsg
  327. mov rdx, 5 ; "Lap "
  328. syscall
  329. ; increment the lapcount str directly
  330. mov rbx, lapcount ; first digit ptr
  331. add rbx, lapcountlen ; rightmost digit ptr
  332. sub rbx, 1
  333. mov r8, 1 ; counter
  334. lap_handler_inc_lap:
  335. mov r10b, [rbx]
  336. cmp r10b, 0x39 ; '9'
  337. jl lap_handler_inc_end
  338. add r8, 1
  339. cmp r8, [laplen]
  340. jl lap_handler_laplen_noupd
  341. mov [laplen], r8 ; update laplen
  342. lap_handler_laplen_noupd:
  343. mov byte [rbx], 0x30 ; set current digit to '0'
  344. sub rbx, 1
  345. cmp rbx, lapcount
  346. jl fault
  347. jmp lap_handler_inc_lap
  348. lap_handler_inc_end:
  349. add r10b, 1
  350. mov [rbx], r10b
  351. mov rax, 1
  352. mov rdi, 1
  353. mov rdx, [laplen]
  354. mov rsi, lapcount
  355. add rsi, lapcountlen
  356. sub rsi, rdx ; leftmost digit ptr
  357. syscall
  358. mov rax, 1
  359. mov rdi, 1
  360. mov rsi, lapsmsg + 4
  361. mov rdx, 3 ; " : "
  362. syscall
  363. mov rax, 1 ; stdout
  364. mov rdi, 0xA ; \n
  365. std ; set DF for string operations
  366. call proc_print_time
  367. ret
  368. sig_restorer:
  369. mov rax, 15 ; sys_rt_sigreturn
  370. xor rdi, rdi
  371. syscall
  372. ;
  373. ; Argument parsing
  374. ;
  375. arg_parse:
  376. ; Checking argument count
  377. mov rax, [rsp]
  378. cmp rax, 1
  379. je arg_ok
  380. cmp rax, 3
  381. jle parse_r
  382. arg_err: ; badval & badarg jmp here too
  383. mov rax, [rsp+8] ; argv[0] program name
  384. mov rdi, -1 ; return status
  385. jmp usage
  386. parse_r:
  387. mov rax, [rsp+16] ; 1st arg should be "-r"
  388. mov bl, [rax]
  389. cmp bl, '-'
  390. jne badarg
  391. mov bl, [rax+1]
  392. cmp bl, 'h' ; -h
  393. je arg_err
  394. cmp bl, 'r'
  395. jne badarg
  396. mov bl, [rax+2]
  397. cmp bl, 0
  398. jne arg_nxt ; the value seems to be just after the -r like -r2
  399. nxt_arg:
  400. ; the 1st argument is -r the second must be the time_res
  401. ; check that the arg exists
  402. mov rax, [rsp]
  403. cmp rax, 3
  404. jne arg_err
  405. mov rax, [rsp+24]
  406. jmp arg_cont
  407. arg_nxt:
  408. ; check that there is no more args
  409. mov rbx, [rsp]
  410. cmp rbx, 2
  411. jne arg_err
  412. add rax, 2
  413. arg_cont:
  414. ; rax should point on the value
  415. mov bl, [rax+1]
  416. cmp bl, 0
  417. jne badval
  418. xor rbx, rbx
  419. mov bl, [rax]
  420. cmp bl, '1'
  421. jl badval
  422. cmp bl, '8'
  423. jg badval
  424. sub bl, '0'
  425. mov [time_res], rbx
  426. jmp arg_ok
  427. ; print an error message, usage and exit with rax pointing the value
  428. badval:
  429. mov rsi, badval_msg
  430. mov rdx, badval_msglen
  431. jmp arg_strerr
  432. ; print an error message, usage and exit
  433. badarg:
  434. mov rsi, badarg_msg
  435. mov rdx, badarg_msglen
  436. mov rax, [rsp+16]
  437. jmp arg_strerr
  438. ; rsi msg ptr, rdx msg len, rax arg ptr
  439. arg_strerr:
  440. mov r8, rax
  441. mov rax, 1
  442. mov rdi, 1
  443. syscall
  444. mov rsi, r8
  445. mov rax, r8
  446. call proc_strlen
  447. mov rax, 1
  448. syscall
  449. call proc_nl
  450. call proc_nl
  451. jmp arg_err
  452. ;
  453. ; Print usage and exit
  454. ; Except rax to point on programm name and rdi to be the return code
  455. ;
  456. usage:
  457. push rdi
  458. push rax
  459. mov rax, 1 ; write
  460. mov rdi, 1 ; stdout
  461. mov rsi, usage_pre
  462. mov rdx, usage_prelen
  463. syscall
  464. pop rsi
  465. mov rax, rsi
  466. call proc_strlen
  467. mov rax, 1
  468. syscall
  469. mov rax, 1
  470. mov rsi, usage_post
  471. mov rdx, usage_postlen
  472. syscall
  473. mov rax, 60
  474. pop rdi
  475. syscall
  476. ; With rax pointing on the string, the result will be in rdx
  477. proc_strlen:
  478. mov r8, rax
  479. mov r9, r8
  480. sub r9, 1
  481. strlen_loop:
  482. add r9, 1
  483. mov al, [r9]
  484. cmp al, 0
  485. jne strlen_loop
  486. sub r9, r8
  487. mov rdx, r9
  488. ret
  489. ; with rdi the fd
  490. proc_nl:
  491. mov rax, 1
  492. mov rsi, nl
  493. mov rdx, 1
  494. syscall
  495. ret
  496. .end: