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

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