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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. %use smartalign
  30. ALIGNMODE k8
  31. %define O_NONBLOCK 0x800
  32. STRUC TIMESPEC_STRUC
  33. .tv_sec: resq 1
  34. .tv_nsec: resq 1
  35. ENDSTRUC
  36. %macro TIMESPEC 1
  37. %1: ISTRUC TIMESPEC_STRUC
  38. at TIMESPEC_STRUC.tv_sec, dq 0
  39. at TIMESPEC_STRUC.tv_nsec, dq 0
  40. IEND
  41. %define %1.tv_sec %1+TIMESPEC_STRUC.tv_sec
  42. %define %1.tv_nsec %1+TIMESPEC_STRUC.tv_nsec
  43. %endmacro
  44. STRUC SIGACTION_STRUC
  45. .sa_handler: resq 1
  46. .sa_flags: resq 1
  47. .sa_restorer: resq 1
  48. .sa_mask: resb 128
  49. ENDSTRUC
  50. ; from https://www.linuxnasm.be/home/library/lib-includes/signals-inc
  51. %macro SIGACTION 1
  52. %1: ISTRUC SIGACTION_STRUC
  53. at SIGACTION_STRUC.sa_handler, dq 0
  54. at SIGACTION_STRUC.sa_flags, dq 0
  55. at SIGACTION_STRUC.sa_restorer, dq 0
  56. at SIGACTION_STRUC.sa_mask, times 128 db 0
  57. IEND
  58. %define sigaction.sa_handler sigaction+SIGACTION_STRUC.sa_handler
  59. %define sigaction.sa_flags sigaction+SIGACTION_STRUC.sa_flags
  60. %define sigaction.sa_restorer sigaction+SIGACTION_STRUC.sa_restorer
  61. %define sigaction.sa_mask sigaction+SIGACTION_STRUC.sa_mask
  62. %endmacro
  63. section .data
  64. align 8
  65. TIMESPEC ts_start
  66. TIMESPEC ts_cur
  67. TIMESPEC ts_sleep
  68. time_res: dq 2 ; 2 digits bellow seconds can grow to 8
  69. hours: times 8 db '0' ; 8 digits is the max for hours
  70. timestr: db ":00:00."
  71. times 8 db '0' ; 8 digits for nanoseconds
  72. times 0xE db ' '
  73. db 0
  74. timestrend: align 8
  75. timestrlen: equ timestrend - timestr
  76. %define HOURSLEN 8
  77. buf: db 0
  78. fcntl_flag: dq 0
  79. SIGACTION sigaction
  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. mov rbx, ts_start
  117. %define RBXPTR(x) rbx + x - ts_start
  118. ; set stdin reads non blocking
  119. xor rdx, rdx
  120. xor rdi, rdi
  121. mov rax, 72 ; fcntl
  122. mov rsi, 3 ; F_GETFL
  123. syscall
  124. mov rdx, O_NONBLOCK
  125. not rdx
  126. and rax, rdx
  127. mov [RBXPTR(fcntl_flag)], rax
  128. mov rdx, rax
  129. or rdx, O_NONBLOCK
  130. mov rax, 72 ; fcntl
  131. mov rsi, 4 ; F_SETFL
  132. syscall
  133. cmp rax, 0
  134. jne fault
  135. ; preparing SIGINT catch
  136. mov rax, proc_lap_handler
  137. mov qword [RBXPTR(sigaction.sa_handler)], rax
  138. mov ecx, 0x14000000 ; SA_RESTART | SA_RESTORER
  139. mov dword [RBXPTR(sigaction.sa_flags)], ecx
  140. add rax, sig_restorer - proc_lap_handler
  141. mov qword [RBXPTR(sigaction.sa_restorer)], rax
  142. mov rax, 13 ; sys_rt_sigaction
  143. mov rdi, 2 ; SIGINT
  144. mov rsi, rbx
  145. add rsi, sigaction - ts_start
  146. ;mov rsi, sigaction
  147. mov rdx, 0 ; NULL
  148. mov r10, 8 ; sig_size
  149. syscall
  150. cmp rax, 0
  151. jne fault
  152. mov rax, 228 ; clock_gettime
  153. mov rdi, 0 ; CLOCK_REALTIME
  154. mov rsi, rbx
  155. syscall
  156. mov rax, 1 ; write
  157. mov rdi, 2 ; stderr
  158. mov rsi, rbx
  159. add rsi, startmsg - ts_start
  160. mov rdx, startmsglen
  161. syscall
  162. ; set value for ts_sleep.tv_nsec given time_res
  163. ; div sleep time by 10 for each digits added bellow seconds
  164. mov rax, 100000000
  165. xor rcx, rcx
  166. mov cl, [RBXPTR(time_res)]
  167. dec cl
  168. test cl, cl
  169. jz setsleep_loopend
  170. mov rsi, 10
  171. xor rdx, rdx
  172. setsleep_loop:
  173. div rsi
  174. loop setsleep_loop
  175. setsleep_loopend:
  176. mov [RBXPTR(ts_sleep.tv_nsec)], rax
  177. mov r12, proc_print_time
  178. mov r10, 0x0d02
  179. std ; set DF for string operations
  180. align 16
  181. main_loop:
  182. push rbx
  183. push r10 ; \r & stderr
  184. call r12
  185. pop rbx
  186. ; Attempt to read from stdin
  187. ; if something read, enter has been pressed
  188. xor rax, rax
  189. push rax
  190. push rax
  191. pop rdi
  192. mov rsi, rbx
  193. add rsi, buf - ts_start
  194. mov rdx, 1
  195. syscall
  196. test rax, rax
  197. jns flush_stdin ; flush & exit
  198. mov rax, 35 ; nanosleep
  199. mov rdi, rbx
  200. add rdi, ts_sleep - ts_start
  201. pop rsi
  202. syscall
  203. jmp main_loop ; main_loop
  204. flush_stdin:
  205. mov rax, 0
  206. mov rdi, 0
  207. mov rsi, buf
  208. mov rdx, 1
  209. syscall
  210. test rax, rax
  211. jz newline_exit
  212. jns flush_stdin
  213. mov rdi, 0 ; EXIT OK
  214. ;
  215. ; Expect rdi to be the return code
  216. ;
  217. exit:
  218. push rdi
  219. ; restoring stdin state
  220. mov rax, 72 ; fcntl
  221. xor rdi, rdi
  222. mov rsi, 4 ; F_SETFL
  223. mov rdx, [fcntl_flag]
  224. syscall
  225. test rax, rax
  226. jz exit_end
  227. pop rdi ; failed to restore
  228. push 1 ; exit FAIL
  229. exit_end:
  230. mov rax, 60 ; sys_exit
  231. pop rdi ; return code
  232. syscall
  233. fault:
  234. mov rdi, 2 ; stderr
  235. call proc_nl
  236. mov rax, 1
  237. mov rsi, faultmsg
  238. mov rdx, faultmsglen
  239. syscall
  240. mov rdi, 1 ; failure
  241. jmp exit
  242. newline_exit:
  243. mov rdi, 1
  244. call proc_nl
  245. mov rdi, 0 ; exit OK
  246. jmp exit
  247. ;
  248. ; Print current time on FD and add a leading char CHR
  249. ; push FD & push CHR to set arguments
  250. ; Warning : DF must be set
  251. ;
  252. align 16
  253. proc_print_time:
  254. mov rax, 228 ; clock_gettime
  255. xor rdi, rdi ; CLOCK_REALTIME
  256. mov rsi, ts_cur
  257. syscall ; updating ts_cur time
  258. mov rsi, ts_start.tv_sec
  259. %define RSIPTR(x) rsi + x - ts_start.tv_sec
  260. mov rax, [ts_cur.tv_nsec] ; allign procpt_loopns
  261. sub rax, [ts_start.tv_nsec] ; allign procpt_loopns
  262. jge print_time_us_cont
  263. add rax, 1000000000 ; negativ result
  264. sub qword [RSIPTR(ts_cur.tv_sec)], 1
  265. print_time_us_cont:
  266. xor rdx, rdx
  267. div qword [RSIPTR(ts_sleep.tv_nsec)] ; Divide result given time_res
  268. mov bl, [rsp + 9]
  269. mov byte [RSIPTR(timestrend - 1)], bl ; set last chr from 1st argument
  270. ; set the nanosec chars (time_res chars) in timestr
  271. mov rbx, 0x2020202020202020
  272. mov rcx, [RSIPTR(time_res)]
  273. mov rdi, 10
  274. xor rdx, rdx
  275. align 16 ; should be allready alligned
  276. procpt_loopns:
  277. div rdi
  278. or dl, 0x30 ; '0'
  279. shl rbx, 8
  280. xchg bl, dl
  281. loop procpt_loopns
  282. mov [timestr+7], rbx
  283. ; filling timestr with seconds & minutes chars
  284. mov rax, [RSIPTR(ts_cur.tv_sec)]
  285. sub rax, [RSIPTR(ts_start.tv_sec)] ; rax now contain elapsed seconds
  286. mov rcx, 10
  287. mov rdi, 6
  288. xor rdx, rdx
  289. div rcx
  290. mov bh, dl
  291. xor dl, dl
  292. div rdi
  293. mov bl, dl
  294. or bx, 0x3030
  295. mov word [RSIPTR(timestr + 4)], bx
  296. xor dl, dl
  297. div rcx
  298. mov bh, dl
  299. xor dl, dl
  300. div rdi
  301. mov bl, dl
  302. or bx, 0x3030
  303. mov word [RSIPTR(timestr + 1)], bx
  304. ; using rbx to stores the hours string
  305. xor rbx, rbx
  306. mov rcx, HOURSLEN
  307. mov rdi, 10
  308. xor dl, dl
  309. align 16
  310. procpt_looph:
  311. div rdi
  312. shl rbx, 8
  313. xchg bl, dl
  314. cmp rax, 0
  315. loopne procpt_looph
  316. procpt_looph_end:
  317. mov rax, HOURSLEN - 2
  318. cmp rcx, HOURSLEN - 2
  319. cmovl rax, rcx ; rsi stores hours digit count (at least 2)
  320. cmp rcx, 0
  321. jz procpt_looph2_end
  322. procpt_looph2:
  323. shl rbx, 8
  324. loopne procpt_looph2
  325. procpt_looph2_end:
  326. mov rdi, 0x3030303030303030 ; ASCII convertion
  327. or rbx, rdi
  328. mov [RSIPTR(hours)], rbx
  329. xor rdi, rdi
  330. mov dil, [rsp + 8] ; fd as 2nd argument
  331. mov rdx, timestrlen + HOURSLEN
  332. sub rdx, rax ; timestr + hours len
  333. lea rsi, [RSIPTR(hours + rax)] ; hours start pointer
  334. mov rax, 1 ; write
  335. syscall
  336. test rax, rax
  337. js fault
  338. ret 8
  339. %undef RSIPTR
  340. ;
  341. ; sig handler for SIGINT displaying lap count and time on stdout
  342. ;
  343. proc_lap_handler:
  344. mov rax, 1
  345. mov rdi, 2
  346. mov rsi, cr
  347. mov rdx, 1
  348. syscall ; \r on stderr
  349. test rax, rax
  350. js fault
  351. mov rax, 1
  352. mov rdi, 1
  353. mov rsi, lapsmsg
  354. mov rdx, 4 ; "Lap "
  355. syscall
  356. test rax, rax
  357. js fault
  358. ; increment the lapcount str directly
  359. mov rcx, lapcountlen - 1; digit counter starting by the right most
  360. proclap_loop:
  361. cmp byte [lapcount + rcx], '9'
  362. jl proclap_loopend
  363. mov byte [lapcount + rcx], '0' ; set current digit to '0'
  364. loop proclap_loop
  365. proclap_loopend:
  366. add byte [lapcount + rcx], 1 ; increment current digit
  367. mov rdx, lapcountlen
  368. sub rdx, rcx
  369. cmp rdx, [laplen] ; update laplen if needed
  370. cmovl rdx, [laplen]
  371. mov [laplen], rdx
  372. mov rsi, lapcount + lapcountlen
  373. sub rsi, rdx
  374. mov rax, 1
  375. mov rdi, 1 ; stdout
  376. syscall
  377. test rax, rax
  378. js fault
  379. mov rax, 1 ; write
  380. mov rsi, lapsmsg + 3
  381. mov rdx, 3 ; " : "
  382. syscall
  383. test rax, rax
  384. js fault
  385. std ; set DF for string operations
  386. push 0x0A01 ; \n & stdout
  387. call proc_print_time
  388. ret
  389. sig_restorer:
  390. mov rax, 15 ; sys_rt_sigreturn
  391. xor rdi, rdi
  392. syscall
  393. ;
  394. ; Argument parsing
  395. ;
  396. arg_parse:
  397. ; Checking argument count
  398. mov rax, [rsp]
  399. cmp rax, 1
  400. je arg_ok
  401. cmp rax, 3
  402. jle parse_r
  403. arg_err: ; badval & badarg jmp here too
  404. mov rax, [rsp+8] ; argv[0] program name
  405. mov rdi, -1 ; return status
  406. jmp usage
  407. parse_r:
  408. mov rax, [rsp+16] ; 1st arg should be "-r"
  409. mov bl, [rax]
  410. cmp bl, '-'
  411. jne badarg
  412. mov bl, [rax+1]
  413. cmp bl, 'h' ; -h
  414. je arg_err
  415. cmp bl, 'r'
  416. jne badarg
  417. mov bl, [rax+2]
  418. cmp bl, 0
  419. jne arg_nxt ; the value seems to be just after the -r like -r2
  420. nxt_arg:
  421. ; the 1st argument is -r the second must be the time_res
  422. ; check that the arg exists
  423. mov rax, [rsp]
  424. cmp rax, 3
  425. jne arg_err
  426. mov rax, [rsp+24]
  427. jmp arg_cont
  428. arg_nxt:
  429. ; check that there is no more args
  430. mov rbx, [rsp]
  431. cmp rbx, 2
  432. jne arg_err
  433. add rax, 2
  434. arg_cont:
  435. ; rax should point on the value
  436. mov bl, [rax+1]
  437. cmp bl, 0
  438. jne badval
  439. xor rbx, rbx
  440. mov bl, [rax]
  441. cmp bl, '1'
  442. jl badval
  443. cmp bl, '8'
  444. jg badval
  445. sub bl, '0'
  446. mov [time_res], rbx
  447. jmp arg_ok
  448. ; print an error message, usage and exit with rax pointing the value
  449. badval:
  450. mov rsi, badval_msg
  451. mov rdx, badval_msglen
  452. jmp arg_strerr
  453. ; print an error message, usage and exit
  454. badarg:
  455. mov rsi, badarg_msg
  456. mov rdx, badarg_msglen
  457. mov rax, [rsp+16]
  458. jmp arg_strerr
  459. ; rsi msg ptr, rdx msg len, rax arg ptr
  460. arg_strerr:
  461. mov r8, rax
  462. mov rax, 1
  463. mov rdi, 1
  464. syscall
  465. mov rsi, r8
  466. mov rax, r8
  467. call proc_strlen
  468. mov rax, 1
  469. syscall
  470. call proc_nl
  471. call proc_nl
  472. jmp arg_err
  473. ;
  474. ; Print usage and exit
  475. ; Except rax to point on programm name and rdi to be the return code
  476. ;
  477. usage:
  478. push rdi
  479. push rax
  480. mov rax, 1 ; write
  481. mov rdi, 1 ; stdout
  482. mov rsi, usage_pre
  483. mov rdx, usage_prelen
  484. syscall
  485. pop rsi
  486. mov rax, rsi
  487. call proc_strlen
  488. mov rax, 1
  489. syscall
  490. mov rax, 1
  491. mov rsi, usage_post
  492. mov rdx, usage_postlen
  493. syscall
  494. mov rax, 60
  495. pop rdi
  496. syscall
  497. ; With rax pointing on the string, the result will be in rdx
  498. proc_strlen:
  499. mov r8, rax
  500. mov r9, r8
  501. dec r9
  502. strlen_loop:
  503. inc r9
  504. mov al, [r9]
  505. cmp al, 0
  506. jne strlen_loop
  507. sub r9, r8
  508. mov rdx, r9
  509. ret
  510. ; with rdi the fd
  511. proc_nl:
  512. mov rax, 1
  513. mov rsi, nl
  514. mov rdx, 1
  515. syscall
  516. ret
  517. .end: