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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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, "New lap : "
  73. lapsmsglen: equ $ - lapsmsg
  74. section .text
  75. global _start
  76. _start:
  77. ; set stdin non blocking
  78. xor rdx, rdx
  79. xor rdi, rdi
  80. mov rax, 72 ; fcntl
  81. mov rsi, 3 ; F_GETFL
  82. syscall
  83. mov rdx, rax
  84. or rdx, 0x800 ; O_NONBLOCK
  85. mov rax, 72 ; fcntl
  86. mov rsi, 4 ; F_SETFL
  87. syscall
  88. cmp rax, 0
  89. jne fault
  90. ; preparing SIGINT catch
  91. mov rax, lap_handler
  92. mov qword [sigaction.sa_handler], rax
  93. mov eax, 0x10000000 ; SA_RESTART
  94. or eax, 0x04000000 ; SA_RESTORER
  95. mov dword [sigaction.sa_flags], eax
  96. mov rax, sig_restorer
  97. mov qword [sigaction.sa_restorer], rax
  98. mov rax, 13 ; sys_rt_sigaction
  99. mov rdi, 2 ; SIGINT
  100. mov rsi, sigaction
  101. mov rdx, 0 ; NULL
  102. mov r10, 8 ; sig_size
  103. syscall
  104. cmp rax, 0
  105. jne fault
  106. mov rax, 228 ; clock_gettime
  107. mov rdi, 0 ; CLOCK_REALTIME
  108. mov rsi, ts_start
  109. syscall
  110. main_loop:
  111. push 2 ; stderr
  112. push 0x0D ; \r
  113. call print_time
  114. ; Attempt to read from stdin
  115. ; if something read, enter has been pressed
  116. mov rax, 0
  117. mov rdi, 0
  118. mov rsi, buf
  119. mov rdx, 1
  120. syscall
  121. cmp rax, 0
  122. jge flush_stdin ; flush stdin and exit
  123. mov rax, 35 ; nanosleep
  124. mov rdi, ts_sleep
  125. mov rsi, 0
  126. syscall
  127. jmp main_loop
  128. flush_stdin:
  129. mov rax, 0
  130. mov rdi, 0
  131. mov rsi, buf
  132. mov rdx, 1
  133. syscall
  134. cmp rax, 0
  135. je newline_exit
  136. jg flush_stdin
  137. exit:
  138. mov rax, 60 ; sys_exit
  139. mov rdi, 0 ; OK
  140. syscall
  141. fault:
  142. mov rax, 1 ; write
  143. mov rdi, 2 ; stderr
  144. mov rsi, nl
  145. mov rdx, 1
  146. syscall
  147. mov rax, 1
  148. mov rsi, faultmsg
  149. mov rdx, faultmsglen
  150. syscall
  151. mov rax, 60 ; sys_exit
  152. mov rdi, 1 ; failure
  153. syscall
  154. newline_exit:
  155. mov rax, 1
  156. mov rdi, 1
  157. mov rsi, nl
  158. mov rdx, 1
  159. syscall
  160. jmp exit
  161. ;
  162. ; Print current time on FD r10 and put r13b as leading char
  163. ;
  164. print_time:
  165. pop r8
  166. pop r9
  167. pop r10
  168. push r8
  169. push r10
  170. push r9
  171. ; updating ts_cur time
  172. mov rax, 228 ; clock_gettime
  173. mov rdi, 0 ; CLOCK_REALTIME
  174. mov rsi, ts_cur
  175. syscall
  176. mov rax, [ts_cur.tv_nsec]
  177. mov rbx, [ts_start.tv_nsec]
  178. sub rax, rbx
  179. cmp rax, 0
  180. jge print_time_us_cont
  181. ; negativ result
  182. add rax, 1000000000
  183. mov rbx, [ts_cur.tv_sec]
  184. sub rbx, 1
  185. mov [ts_cur.tv_sec], rbx
  186. print_time_us_cont:
  187. xor rdx, rdx
  188. mov rcx, 100000
  189. div rcx
  190. ; set the us char in timestr
  191. mov r8, timestr
  192. add r8, 10 ; r8 points on last char before \r
  193. mov r9, 4 ; r9 count the number of digits
  194. print_time_us_loop:
  195. xor rdx, rdx
  196. mov rcx, 10
  197. div rcx
  198. add dl, 0x30
  199. mov [r8], dl
  200. sub r8, 1
  201. sub r9, 1
  202. cmp r9, 0
  203. jg print_time_us_loop
  204. ; handling seconds, minutes & hours
  205. mov rax, [ts_cur.tv_sec]
  206. mov rbx, [ts_start.tv_sec]
  207. sub rax, rbx
  208. ; rax now contain elapsed seconds
  209. ; filling timestr with seconds & minutes
  210. xor rdx, rdx
  211. mov rcx, 10
  212. div rcx
  213. add dl, 0x30
  214. mov byte [timestr + 5], dl
  215. xor rdx, rdx
  216. mov rcx, 6
  217. div rcx
  218. push rax
  219. add dl, 0x30
  220. mov byte [timestr + 4], dl
  221. xor rdx, rdx
  222. mov rcx, 10
  223. div rcx
  224. add dl, 0x30
  225. mov byte [timestr + 2], dl
  226. pop rax
  227. xor rdx, rdx
  228. mov rcx, 6
  229. div rcx
  230. add dl, 0x30
  231. mov byte[timestr + 1], dl
  232. ; filling the hours buffer
  233. ; r8 will contain our digits counter : max is 8
  234. mov r8, 8
  235. print_time_hours_loop:
  236. mov rcx, 10
  237. xor rdx, rdx
  238. div rcx
  239. add dl, 0x30
  240. cmp rax, 0
  241. jne print_time_hours_print_mod
  242. cmp rdx, 0
  243. je print_time_hours_loop_end
  244. print_time_hours_print_mod:
  245. mov r9, hours
  246. add r9, r8
  247. mov byte [r9], dl
  248. cmp r8, 0
  249. je fault
  250. sub r8, 1
  251. cmp rax, 0
  252. jne print_time_hours_loop
  253. print_time_hours_loop_end:
  254. ; print hours + timestr
  255. add r8, 1
  256. cmp r8, 7
  257. jle print_time_hours_cont
  258. mov r8, 7 ; maximum value for r8
  259. print_time_hours_cont:
  260. mov r9, hours
  261. add r9, r8
  262. mov rcx, 9
  263. sub rcx, r8 ; rcx is hours size
  264. add rcx, timestrlen ; add to timestrlen
  265. ; Set leading char
  266. pop r10
  267. mov byte [timestr+timestrlen-1], r10b
  268. mov rax, 1 ; write
  269. pop r10 ; print_time FD arg
  270. mov rdi, r10 ; print_time arg
  271. mov rsi, r9 ; start hours pointer
  272. mov rdx, rcx ; size to timestr end
  273. syscall
  274. ret
  275. lap_handler:
  276. mov rax, 1
  277. mov rdi, 1
  278. mov rsi, lapsmsg
  279. mov rdx, lapsmsglen
  280. syscall
  281. push 1 ; stdout
  282. push 0x0A ; \n
  283. call print_time
  284. dbg2:
  285. ret
  286. sig_restorer:
  287. mov rax, 15 ; sys_rt_sigreturn
  288. xor rdi, rdi
  289. syscall
  290. .end: