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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. ; A simple precise stopwatch
  17. ; Build with :
  18. ; nasm -felf64 wtfstopw.asm && ld wtfstopw.o -o wtfstopw
  19. [bits 64]
  20. section .data
  21. STRUC TIMESPEC_STRUC
  22. .tv_sec: resq 1
  23. .tv_nsec: resq 1
  24. ENDSTRUC
  25. %macro TIMESPEC 1
  26. %1: ISTRUC TIMESPEC_STRUC
  27. at TIMESPEC_STRUC.tv_sec, dq 0
  28. at TIMESPEC_STRUC.tv_nsec, dq 0
  29. IEND
  30. %define %1.tv_sec %1+TIMESPEC_STRUC.tv_sec
  31. %define %1.tv_nsec %1+TIMESPEC_STRUC.tv_nsec
  32. %endmacro
  33. STRUC SIGACTION_STRUC
  34. .sa_handler: resq 1
  35. .sa_flags: resq 1
  36. .sa_restorer: resq 1
  37. .sa_mask: resb 128
  38. ENDSTRUC
  39. sigaction: ISTRUC SIGACTION_STRUC
  40. at SIGACTION_STRUC.sa_handler, dq 0
  41. at SIGACTION_STRUC.sa_flags, dq 0
  42. at SIGACTION_STRUC.sa_restorer, dq 0
  43. at SIGACTION_STRUC.sa_mask, times 128 db 0
  44. IEND
  45. %define sigaction.sa_handler sigaction+SIGACTION_STRUC.sa_handler
  46. %define sigaction.sa_flags sigaction+SIGACTION_STRUC.sa_flags
  47. %define sigaction.sa_restorer sigaction+SIGACTION_STRUC.sa_restorer
  48. %define sigaction.sa_mask sigaction+SIGACTION_STRUC.sa_mask
  49. TIMESPEC ts_start
  50. TIMESPEC ts_cur
  51. ts_sleep:
  52. tv_sleep_s dd 0,0
  53. tv_sleep_us dd 10000000,0
  54. ;; 1/2s sleep
  55. ;ts_sleep:
  56. ; tv_sleep_s dq 0
  57. ; tv_sleep_us dq 500000000
  58. faultmsg: db "fault", 0xA
  59. faultmsglen: equ $ - faultmsg
  60. msg: db "0 Hello, world!", 10
  61. msglen: equ $ - msg
  62. hours: db "000000000"
  63. timestr: db ":00:00.0000 ", 0x0D
  64. timestrlen: equ $ - timestr
  65. nl: db 0x0A
  66. buf: db 0
  67. dbgmsg: db "SigINT", 0xa
  68. section .text
  69. global _start
  70. _start:
  71. ; set stdin non blocking
  72. xor rdx, rdx
  73. xor rdi, rdi
  74. mov rax, 72 ; fcntl
  75. mov rsi, 3 ; F_GETFL
  76. syscall
  77. mov rdx, rax
  78. or rdx, 0x800 ; O_NONBLOCK
  79. dbg:
  80. mov rax, 72 ; fcntl
  81. mov rsi, 4 ; F_SETFL
  82. syscall
  83. cmp rax, 0
  84. jne fault
  85. ; preparing SIGINT catch
  86. mov rax, lap_handler
  87. mov qword [sigaction.sa_handler], rax
  88. mov eax, 0x10000000 ; SA_RESTART
  89. or eax, 0x04000000 ; SA_RESTORER
  90. mov dword [sigaction.sa_flags], eax
  91. mov rax, sig_restorer
  92. mov qword [sigaction.sa_restorer], rax
  93. mov rax, 13 ; sys_rt_sigaction
  94. mov rdi, 2 ; SIGINT
  95. mov rsi, sigaction
  96. mov rdx, 0 ; NULL
  97. mov r10, 8 ; sig_size
  98. syscall
  99. cmp rax, 0
  100. jne fault
  101. mov rax, 228 ; clock_gettime
  102. mov rdi, 0 ; CLOCK_REALTIME
  103. mov rsi, ts_start
  104. syscall
  105. print_time:
  106. ; updating ts_cur time
  107. mov rax, 228 ; clock_gettime
  108. mov rdi, 0 ; CLOCK_REALTIME
  109. mov rsi, ts_cur
  110. syscall
  111. mov rax, [ts_cur.tv_nsec]
  112. mov rbx, [ts_start.tv_nsec]
  113. sub rax, rbx
  114. cmp rax, 0
  115. jge print_time_us_cont
  116. ; negativ result
  117. add rax, 1000000000
  118. mov rbx, [ts_cur.tv_sec]
  119. sub rbx, 1
  120. mov [ts_cur.tv_sec], rbx
  121. print_time_us_cont:
  122. xor rdx, rdx
  123. mov rcx, 100000
  124. div rcx
  125. ; set the us char in timestr
  126. mov r8, timestr
  127. add r8, 10 ; r8 points on last char before \r
  128. mov r9, 4 ; r9 count the number of digits
  129. print_time_us_loop:
  130. xor rdx, rdx
  131. mov rcx, 10
  132. div rcx
  133. add dl, 0x30
  134. mov [r8], dl
  135. sub r8, 1
  136. sub r9, 1
  137. cmp r9, 0
  138. jg print_time_us_loop
  139. ; handling seconds, minutes & hours
  140. mov rax, [ts_cur.tv_sec]
  141. mov rbx, [ts_start.tv_sec]
  142. sub rax, rbx
  143. ; rax now contain elapsed seconds
  144. ; filling timestr with seconds & minutes
  145. xor rdx, rdx
  146. mov rcx, 10
  147. div rcx
  148. add dl, 0x30
  149. mov byte [timestr + 5], dl
  150. xor rdx, rdx
  151. mov rcx, 6
  152. div rcx
  153. push rax
  154. add dl, 0x30
  155. mov byte [timestr + 4], dl
  156. xor rdx, rdx
  157. mov rcx, 10
  158. div rcx
  159. add dl, 0x30
  160. mov byte [timestr + 2], dl
  161. pop rax
  162. xor rdx, rdx
  163. mov rcx, 6
  164. div rcx
  165. add dl, 0x30
  166. mov byte[timestr + 1], dl
  167. ; filling the hours buffer
  168. ; r8 will contain our digits counter : max is 8
  169. mov r8, 8
  170. print_time_hours_loop:
  171. mov rcx, 10
  172. xor rdx, rdx
  173. div rcx
  174. add dl, 0x30
  175. cmp rax, 0
  176. jne print_time_hours_print_mod
  177. cmp rdx, 0
  178. je print_time_hours_loop_end
  179. print_time_hours_print_mod:
  180. mov r9, hours
  181. add r9, r8
  182. mov byte [r9], dl
  183. cmp r8, 0
  184. je fault
  185. sub r8, 1
  186. cmp rax, 0
  187. jne print_time_hours_loop
  188. print_time_hours_loop_end:
  189. ; print hours + timestr
  190. add r8, 1
  191. cmp r8, 7
  192. jle print_time_hours_cont
  193. mov r8, 7 ; maximum value for r8
  194. print_time_hours_cont:
  195. mov r9, hours
  196. add r9, r8
  197. mov rcx, 9
  198. sub rcx, r8 ; rcx is hours size
  199. add rcx, timestrlen ; add to timestrlen
  200. mov rax, 1 ; write
  201. mov rdi, 1 ; stdout
  202. mov rsi, r9 ; start hours pointer
  203. mov rdx, rcx ; size to timestr end
  204. syscall
  205. ; Attempt to read from stdin
  206. ; if something read, enter has been pressed
  207. mov rax, 0
  208. mov rdi, 0
  209. mov rsi, buf
  210. mov rdx, 1
  211. syscall
  212. cmp rax, 0
  213. jge flush_stdin ; flush stdin and exit
  214. mov rax, 35 ; nanosleep
  215. mov rdi, ts_sleep
  216. mov rsi, 0
  217. syscall
  218. jmp print_time ; main loop
  219. flush_stdin:
  220. mov rax, 0
  221. mov rdi, 0
  222. mov rsi, buf
  223. mov rdx, 1
  224. syscall
  225. cmp rax, 0
  226. je newline_exit
  227. jg flush_stdin
  228. exit:
  229. mov rax, 60 ; sys_exit
  230. mov rdi, 0 ; OK
  231. syscall
  232. fault:
  233. mov rax, 1 ; write
  234. mov rdi, 2 ; stderr
  235. mov rsi, nl
  236. mov rdx, 1
  237. syscall
  238. mov rax, 1
  239. mov rsi, faultmsg
  240. mov rdx, faultmsglen
  241. syscall
  242. mov rax, 60 ; sys_exit
  243. mov rdi, 1 ; failure
  244. syscall
  245. newline_exit:
  246. mov rax, 1
  247. mov rdi, 1
  248. mov rsi, nl
  249. mov rdx, 1
  250. syscall
  251. jmp exit
  252. lap_handler:
  253. pop r8
  254. mov rax, 1
  255. mov rdi, 1
  256. mov rsi, dbgmsg
  257. mov rdx, 7
  258. syscall
  259. push r8 ; push back ret addr
  260. ret
  261. sig_restorer:
  262. mov rax, 15 ; sys_rt_sigreturn
  263. xor rdi, rdi
  264. syscall
  265. .end: