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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 -felf64 -l wtfstopw.lst wtfstopw.asm && ld -melf_x86_64 wtfstopw.o -o wtfstopw
  20. ;
  21. ; Usage : ./wtfstopw
  22. ; press enter to exit
  23. ; send SIGINT (with kill -2 or ctrl + c) for new lap on stdout
  24. ;
  25. [bits 64]
  26. STRUC TIMESPEC_STRUC
  27. .tv_sec: resq 1
  28. .tv_nsec: resq 1
  29. ENDSTRUC
  30. %macro TIMESPEC 1
  31. %1: ISTRUC TIMESPEC_STRUC
  32. at TIMESPEC_STRUC.tv_sec, dq 0
  33. at TIMESPEC_STRUC.tv_nsec, dq 0
  34. IEND
  35. %define %1.tv_sec %1+TIMESPEC_STRUC.tv_sec
  36. %define %1.tv_nsec %1+TIMESPEC_STRUC.tv_nsec
  37. %endmacro
  38. STRUC SIGACTION_STRUC
  39. .sa_handler: resq 1
  40. .sa_flags: resq 1
  41. .sa_restorer: resq 1
  42. .sa_mask: resb 128
  43. ENDSTRUC
  44. section .data
  45. sigaction: ISTRUC SIGACTION_STRUC
  46. at SIGACTION_STRUC.sa_handler, dq 0
  47. at SIGACTION_STRUC.sa_flags, dq 0
  48. at SIGACTION_STRUC.sa_restorer, dq 0
  49. at SIGACTION_STRUC.sa_mask, times 128 db 0
  50. IEND
  51. %define sigaction.sa_handler sigaction+SIGACTION_STRUC.sa_handler
  52. %define sigaction.sa_flags sigaction+SIGACTION_STRUC.sa_flags
  53. %define sigaction.sa_restorer sigaction+SIGACTION_STRUC.sa_restorer
  54. %define sigaction.sa_mask sigaction+SIGACTION_STRUC.sa_mask
  55. TIMESPEC ts_start
  56. TIMESPEC ts_cur
  57. ts_sleep:
  58. tv_sleep_s dq 0
  59. tv_sleep_us dq 10000000
  60. ;; 1/2s sleep
  61. ;ts_sleep:
  62. ; tv_sleep_s dq 0
  63. ; tv_sleep_us dq 500000000
  64. faultmsg: db "Fault !", 0xA
  65. faultmsglen: equ $ - faultmsg
  66. startmsg: db "Press Enter or ctrl+d to exit and ctrl+c for new lap."
  67. db 0xA
  68. startmsglen: equ $ - startmsg
  69. hours: db "000000000"
  70. timestr: db ":00:00.0000 ", 0x0a
  71. timestrlen: equ $ - timestr
  72. nl: db 0x0A
  73. buf: db 0
  74. lapsmsg: db 0x0d, "Lap : "
  75. lapsmsglen: equ $ - lapsmsg
  76. lapcount: db "00000000"
  77. lapcountlen: equ $ - lapcount
  78. laplen: dq 2
  79. fcntl_flag: dq 0
  80. section .text
  81. global _start
  82. _start:
  83. ; set stdin non blocking
  84. xor rdx, rdx
  85. xor rdi, rdi
  86. mov rax, 72 ; fcntl
  87. mov rsi, 3 ; F_GETFL
  88. syscall
  89. mov [fcntl_flag], rax
  90. mov rdx, rax
  91. or rdx, 0x800 ; O_NONBLOCK
  92. mov rax, 72 ; fcntl
  93. mov rsi, 4 ; F_SETFL
  94. syscall
  95. cmp rax, 0
  96. jne fault
  97. ; initializing lapptr
  98. ; preparing SIGINT catch
  99. mov rax, proc_lap_handler
  100. mov qword [sigaction.sa_handler], rax
  101. mov eax, 0x14000000 ; SA_RESTART | SA_RESTORER
  102. mov dword [sigaction.sa_flags], eax
  103. mov rax, sig_restorer
  104. mov qword [sigaction.sa_restorer], rax
  105. mov rax, 13 ; sys_rt_sigaction
  106. mov rdi, 2 ; SIGINT
  107. mov rsi, sigaction
  108. mov rdx, 0 ; NULL
  109. mov r10, 8 ; sig_size
  110. syscall
  111. cmp rax, 0
  112. jne fault
  113. mov rax, 228 ; clock_gettime
  114. mov rdi, 0 ; CLOCK_REALTIME
  115. mov rsi, ts_start
  116. syscall
  117. mov rax, 1 ; write
  118. mov rdi, 2 ; stderr
  119. mov rsi, startmsg
  120. mov rdx, startmsglen
  121. syscall
  122. main_loop:
  123. push 2 ; stderr
  124. push 0x0D ; \r
  125. call proc_print_time
  126. ; Attempt to read from stdin
  127. ; if something read, enter has been pressed
  128. mov rax, 0
  129. mov rdi, 0
  130. mov rsi, buf
  131. mov rdx, 1
  132. syscall
  133. cmp rax, 0
  134. jge flush_stdin ; flush stdin and exit
  135. mov rax, 35 ; nanosleep
  136. mov rdi, ts_sleep
  137. mov rsi, 0
  138. syscall
  139. jmp main_loop ; main_loop
  140. flush_stdin:
  141. mov rax, 0
  142. mov rdi, 0
  143. mov rsi, buf
  144. mov rdx, 1
  145. syscall
  146. cmp rax, 0
  147. je newline_exit
  148. jg flush_stdin
  149. mov rdi, 0 ; EXIT OK
  150. ;
  151. ; Expect rdi to be the return code
  152. ;
  153. exit:
  154. push rdi
  155. ; restoring stdin state
  156. mov rax, 72 ; fcntl
  157. xor rdi, rdi
  158. mov rsi, 4 ; F_SETFL
  159. mov rdx, [fcntl_flag]
  160. syscall
  161. cmp rax, 0
  162. je exit_end
  163. pop rdi ; failed to restore
  164. push 1 ; exit FAIL
  165. exit_end:
  166. mov rax, 60 ; sys_exit
  167. pop rdi ; return code
  168. syscall
  169. fault:
  170. mov rax, 1 ; write
  171. mov rdi, 2 ; stderr
  172. mov rsi, nl
  173. mov rdx, 1
  174. syscall
  175. mov rax, 1
  176. mov rsi, faultmsg
  177. mov rdx, faultmsglen
  178. syscall
  179. mov rdi, 1 ; failure
  180. jmp exit
  181. newline_exit:
  182. mov rax, 1
  183. mov rdi, 1
  184. mov rsi, nl
  185. mov rdx, 1
  186. syscall
  187. mov rdi, 0 ; exit OK
  188. jmp exit
  189. ;
  190. ; Print current time on FD and add a leading char CHR
  191. ; push FD & push CHR to set arguments
  192. ;
  193. proc_print_time:
  194. ; push ret addr before arguments
  195. pop r8
  196. pop r9
  197. pop r10
  198. push r8
  199. push r10
  200. push r9
  201. ; updating ts_cur time
  202. mov rax, 228 ; clock_gettime
  203. mov rdi, 0 ; CLOCK_REALTIME
  204. mov rsi, ts_cur
  205. syscall
  206. ; Calculating elapsed ns
  207. mov rax, [ts_cur.tv_nsec]
  208. mov rbx, [ts_start.tv_nsec]
  209. sub rax, rbx
  210. cmp rax, 0
  211. jge print_time_us_cont
  212. ; negativ result
  213. add rax, 1000000000
  214. mov rbx, [ts_cur.tv_sec]
  215. sub rbx, 1
  216. mov [ts_cur.tv_sec], rbx
  217. print_time_us_cont:
  218. xor rdx, rdx
  219. mov rcx, 100000
  220. div rcx
  221. ; set the us char in timestr
  222. mov r8, timestr
  223. add r8, 10 ; r8 points on last char before \r
  224. mov r9, 4 ; r9 count the number of digits
  225. print_time_us_loop:
  226. xor rdx, rdx
  227. mov rcx, 10
  228. div rcx
  229. add dl, 0x30
  230. mov [r8], dl
  231. sub r8, 1
  232. sub r9, 1
  233. cmp r9, 0
  234. jg print_time_us_loop
  235. ; handling seconds, minutes & hours
  236. mov rax, [ts_cur.tv_sec]
  237. mov rbx, [ts_start.tv_sec]
  238. sub rax, rbx
  239. ; rax now contain elapsed seconds
  240. ; filling timestr with seconds & minutes
  241. xor rdx, rdx
  242. mov rcx, 10
  243. div rcx
  244. add dl, 0x30
  245. mov byte [timestr + 5], dl
  246. xor rdx, rdx
  247. mov rcx, 6
  248. div rcx
  249. push rax
  250. add dl, 0x30
  251. mov byte [timestr + 4], dl
  252. xor rdx, rdx
  253. mov rcx, 10
  254. div rcx
  255. add dl, 0x30
  256. mov byte [timestr + 2], dl
  257. pop rax
  258. xor rdx, rdx
  259. mov rcx, 6
  260. div rcx
  261. add dl, 0x30
  262. mov byte[timestr + 1], dl
  263. ; filling the hours buffer
  264. ; r8 will contain our digits counter : max is 8
  265. mov r8, 8
  266. print_time_hours_loop:
  267. mov rcx, 10
  268. xor rdx, rdx
  269. div rcx
  270. add dl, 0x30
  271. cmp rax, 0
  272. jne print_time_hours_print_mod
  273. cmp rdx, 0
  274. je print_time_hours_loop_end
  275. print_time_hours_print_mod:
  276. mov r9, hours
  277. add r9, r8
  278. mov byte [r9], dl
  279. cmp r8, 0
  280. je fault
  281. sub r8, 1
  282. cmp rax, 0
  283. jne print_time_hours_loop
  284. print_time_hours_loop_end:
  285. ; print hours + timestr
  286. add r8, 1
  287. cmp r8, 7
  288. jle print_time_hours_cont
  289. mov r8, 7 ; maximum value for r8
  290. print_time_hours_cont:
  291. mov r9, hours
  292. add r9, r8
  293. mov rcx, 9
  294. sub rcx, r8 ; rcx is hours size
  295. add rcx, timestrlen ; add to timestrlen
  296. ; Set leading char
  297. pop r10
  298. mov byte [timestr+timestrlen-1], r10b
  299. mov rax, 1 ; write
  300. pop r10 ; print_time FD arg
  301. mov rdi, r10 ; print_time arg
  302. mov rsi, r9 ; start hours pointer
  303. mov rdx, rcx ; size to timestr end
  304. syscall
  305. ret
  306. ;
  307. ; sig handler for SIGINT displaying lap count and time on stdout
  308. ;
  309. proc_lap_handler:
  310. mov rax, 1
  311. mov rdi, 1
  312. mov rsi, lapsmsg
  313. mov rdx, 5 ; "Lap "
  314. syscall
  315. ; increment the lapcount str directly
  316. mov rbx, lapcount ; first digit ptr
  317. add rbx, lapcountlen ; rightmost digit ptr
  318. sub rbx, 1
  319. mov r8, 1 ; counter
  320. lap_handler_inc_lap:
  321. mov r10b, [rbx]
  322. cmp r10b, 0x39 ; '9'
  323. jl lap_handler_inc_end
  324. add r8, 1
  325. cmp r8, [laplen]
  326. jl lap_handler_laplen_noupd
  327. mov [laplen], r8 ; update laplen
  328. lap_handler_laplen_noupd:
  329. mov byte [rbx], 0x30 ; set current digit to '0'
  330. sub rbx, 1
  331. cmp rbx, lapcount
  332. jl fault
  333. jmp lap_handler_inc_lap
  334. lap_handler_inc_end:
  335. add r10b, 1
  336. mov [rbx], r10b
  337. mov rax, 1
  338. mov rdi, 1
  339. mov rdx, [laplen]
  340. mov rsi, lapcount
  341. add rsi, lapcountlen
  342. sub rsi, rdx ; leftmost digit ptr
  343. syscall
  344. mov rax, 1
  345. mov rdi, 1
  346. mov rsi, lapsmsg + 4
  347. mov rdx, 3 ; " : "
  348. syscall
  349. push 1 ; stdout
  350. push 0x0A ; \n
  351. call proc_print_time
  352. ret
  353. sig_restorer:
  354. mov rax, 15 ; sys_rt_sigreturn
  355. xor rdi, rdi
  356. syscall
  357. .end: