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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 -melf_x86_64 wtfstopw.o -o wtfstopw
  19. ; Build Debug : nasm -felf64 -l wtfstopw.lst wtfstopw.asm && ld -s -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. section .text
  80. global _start
  81. _start:
  82. ; set stdin non blocking
  83. xor rdx, rdx
  84. xor rdi, rdi
  85. mov rax, 72 ; fcntl
  86. mov rsi, 3 ; F_GETFL
  87. syscall
  88. mov rdx, rax
  89. or rdx, 0x800 ; O_NONBLOCK
  90. mov rax, 72 ; fcntl
  91. mov rsi, 4 ; F_SETFL
  92. syscall
  93. cmp rax, 0
  94. jne fault
  95. ; initializing lapptr
  96. ; preparing SIGINT catch
  97. mov rax, proc_lap_handler
  98. mov qword [sigaction.sa_handler], rax
  99. mov eax, 0x10000000 ; SA_RESTART
  100. or eax, 0x04000000 ; SA_RESTORER
  101. mov dword [sigaction.sa_flags], eax
  102. mov rax, sig_restorer
  103. mov qword [sigaction.sa_restorer], rax
  104. mov rax, 13 ; sys_rt_sigaction
  105. mov rdi, 2 ; SIGINT
  106. mov rsi, sigaction
  107. mov rdx, 0 ; NULL
  108. mov r10, 8 ; sig_size
  109. syscall
  110. cmp rax, 0
  111. jne fault
  112. mov rax, 228 ; clock_gettime
  113. mov rdi, 0 ; CLOCK_REALTIME
  114. mov rsi, ts_start
  115. syscall
  116. mov rax, 1 ; write
  117. mov rdi, 2 ; stderr
  118. mov rsi, startmsg
  119. mov rdx, startmsglen
  120. syscall
  121. main_loop:
  122. push 2 ; stderr
  123. push 0x0D ; \r
  124. call proc_print_time
  125. ; Attempt to read from stdin
  126. ; if something read, enter has been pressed
  127. mov rax, 0
  128. mov rdi, 0
  129. mov rsi, buf
  130. mov rdx, 1
  131. syscall
  132. cmp rax, 0
  133. jge flush_stdin ; flush stdin and exit
  134. mov rax, 35 ; nanosleep
  135. mov rdi, ts_sleep
  136. mov rsi, 0
  137. syscall
  138. jmp main_loop ; main_loop
  139. flush_stdin:
  140. mov rax, 0
  141. mov rdi, 0
  142. mov rsi, buf
  143. mov rdx, 1
  144. syscall
  145. cmp rax, 0
  146. je newline_exit
  147. jg flush_stdin
  148. exit:
  149. mov rax, 60 ; sys_exit
  150. mov rdi, 0 ; OK
  151. syscall
  152. fault:
  153. mov rax, 1 ; write
  154. mov rdi, 2 ; stderr
  155. mov rsi, nl
  156. mov rdx, 1
  157. syscall
  158. mov rax, 1
  159. mov rsi, faultmsg
  160. mov rdx, faultmsglen
  161. syscall
  162. mov rax, 60 ; sys_exit
  163. mov rdi, 1 ; failure
  164. syscall
  165. newline_exit:
  166. mov rax, 1
  167. mov rdi, 1
  168. mov rsi, nl
  169. mov rdx, 1
  170. syscall
  171. jmp exit
  172. ;
  173. ; Print current time on FD r10 and put r13b as leading char
  174. ;
  175. proc_print_time:
  176. ; push ret addr before arguments
  177. pop r8
  178. pop r9
  179. pop r10
  180. push r8
  181. push r10
  182. push r9
  183. ; updating ts_cur time
  184. mov rax, 228 ; clock_gettime
  185. mov rdi, 0 ; CLOCK_REALTIME
  186. mov rsi, ts_cur
  187. syscall
  188. ; Calculating elapsed ns
  189. mov rax, [ts_cur.tv_nsec]
  190. mov rbx, [ts_start.tv_nsec]
  191. sub rax, rbx
  192. cmp rax, 0
  193. jge print_time_us_cont
  194. ; negativ result
  195. add rax, 1000000000
  196. mov rbx, [ts_cur.tv_sec]
  197. sub rbx, 1
  198. mov [ts_cur.tv_sec], rbx
  199. print_time_us_cont:
  200. xor rdx, rdx
  201. mov rcx, 100000
  202. div rcx
  203. ; set the us char in timestr
  204. mov r8, timestr
  205. add r8, 10 ; r8 points on last char before \r
  206. mov r9, 4 ; r9 count the number of digits
  207. print_time_us_loop:
  208. xor rdx, rdx
  209. mov rcx, 10
  210. div rcx
  211. add dl, 0x30
  212. mov [r8], dl
  213. sub r8, 1
  214. sub r9, 1
  215. cmp r9, 0
  216. jg print_time_us_loop
  217. ; handling seconds, minutes & hours
  218. mov rax, [ts_cur.tv_sec]
  219. mov rbx, [ts_start.tv_sec]
  220. sub rax, rbx
  221. ; rax now contain elapsed seconds
  222. ; filling timestr with seconds & minutes
  223. xor rdx, rdx
  224. mov rcx, 10
  225. div rcx
  226. add dl, 0x30
  227. mov byte [timestr + 5], dl
  228. xor rdx, rdx
  229. mov rcx, 6
  230. div rcx
  231. push rax
  232. add dl, 0x30
  233. mov byte [timestr + 4], dl
  234. xor rdx, rdx
  235. mov rcx, 10
  236. div rcx
  237. add dl, 0x30
  238. mov byte [timestr + 2], dl
  239. pop rax
  240. xor rdx, rdx
  241. mov rcx, 6
  242. div rcx
  243. add dl, 0x30
  244. mov byte[timestr + 1], dl
  245. ; filling the hours buffer
  246. ; r8 will contain our digits counter : max is 8
  247. mov r8, 8
  248. print_time_hours_loop:
  249. mov rcx, 10
  250. xor rdx, rdx
  251. div rcx
  252. add dl, 0x30
  253. cmp rax, 0
  254. jne print_time_hours_print_mod
  255. cmp rdx, 0
  256. je print_time_hours_loop_end
  257. print_time_hours_print_mod:
  258. mov r9, hours
  259. add r9, r8
  260. mov byte [r9], dl
  261. cmp r8, 0
  262. je fault
  263. sub r8, 1
  264. cmp rax, 0
  265. jne print_time_hours_loop
  266. print_time_hours_loop_end:
  267. ; print hours + timestr
  268. add r8, 1
  269. cmp r8, 7
  270. jle print_time_hours_cont
  271. mov r8, 7 ; maximum value for r8
  272. print_time_hours_cont:
  273. mov r9, hours
  274. add r9, r8
  275. mov rcx, 9
  276. sub rcx, r8 ; rcx is hours size
  277. add rcx, timestrlen ; add to timestrlen
  278. ; Set leading char
  279. pop r10
  280. mov byte [timestr+timestrlen-1], r10b
  281. mov rax, 1 ; write
  282. pop r10 ; print_time FD arg
  283. mov rdi, r10 ; print_time arg
  284. mov rsi, r9 ; start hours pointer
  285. mov rdx, rcx ; size to timestr end
  286. syscall
  287. ret
  288. ;
  289. ; sig handler for SIGINT displaying lap count and time on stdout
  290. ;
  291. proc_lap_handler:
  292. mov rax, 1
  293. mov rdi, 1
  294. mov rsi, lapsmsg
  295. mov rdx, 5 ; "Lap "
  296. syscall
  297. ; increment the lapcount str directly
  298. mov rbx, lapcount ; first digit ptr
  299. add rbx, lapcountlen ; rightmost digit ptr
  300. sub rbx, 1
  301. mov r8, 1 ; counter
  302. lap_handler_inc_lap:
  303. mov r10b, [rbx]
  304. cmp r10b, 0x39 ; '9'
  305. jl lap_handler_inc_end
  306. add r8, 1
  307. cmp r8, [laplen]
  308. jl lap_handler_laplen_noupd
  309. mov [laplen], r8 ; update laplen
  310. lap_handler_laplen_noupd:
  311. mov byte [rbx], 0x30 ; set current digit to '0'
  312. sub rbx, 1
  313. cmp rbx, lapcount
  314. jl fault
  315. jmp lap_handler_inc_lap
  316. lap_handler_inc_end:
  317. add r10b, 1
  318. mov [rbx], r10b
  319. mov rax, 1
  320. mov rdi, 1
  321. mov rdx, [laplen]
  322. mov rsi, lapcount
  323. add rsi, lapcountlen
  324. sub rsi, rdx ; leftmost digit ptr
  325. syscall
  326. mov rax, 1
  327. mov rdi, 1
  328. mov rsi, lapsmsg + 4
  329. mov rdx, 3 ; " : "
  330. syscall
  331. push 1 ; stdout
  332. push 0x0A ; \n
  333. call proc_print_time
  334. ret
  335. sig_restorer:
  336. mov rax, 15 ; sys_rt_sigreturn
  337. xor rdi, rdi
  338. syscall
  339. .end: