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

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