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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. [bits 64]
  2. section .text
  3. global _start
  4. _start:
  5. ; set stdin non blocking
  6. mov rax, 72 ; fcntl
  7. mov rdi, 0 ; stdin
  8. mov rsi, 3 ; F_GETFL
  9. mov rdx, 0
  10. syscall
  11. mov rdx, rax
  12. or rdx, 4 ; O_NONBLOCK
  13. mov rax, 72 ; fcntl
  14. mov rdi, 0 ; stdin
  15. mov rsi, 4 ; F_SETFL
  16. syscall
  17. mov rax, 228 ; clock_gettime
  18. mov rdi, 0 ; CLOCK_REALTIME
  19. mov rsi, ts_start
  20. syscall
  21. print_time:
  22. ; updating ts_cur time
  23. mov rax, 228 ; clock_gettime
  24. mov rdi, 0 ; CLOCK_REALTIME
  25. mov rsi, ts_cur
  26. syscall
  27. mov rax, [tv_cur_us]
  28. mov rbx, [tv_start_us]
  29. sub rax, rbx
  30. cmp rax, 0
  31. jge print_time_us_cont
  32. ; negativ result
  33. add rax, 1000000000
  34. mov rbx, [tv_cur_s]
  35. sub rbx, 1
  36. mov [tv_cur_s], rbx
  37. print_time_us_cont:
  38. xor rdx, rdx
  39. mov rcx, 100000
  40. div rcx
  41. ; set the us char in timestr
  42. mov r8, timestr
  43. add r8, 10 ; r8 points on last char before \r
  44. mov r9, 4 ; r9 count the number of digits
  45. print_time_us_loop:
  46. xor rdx, rdx
  47. mov rcx, 10
  48. div rcx
  49. add dl, 0x30
  50. mov [r8], dl
  51. sub r8, 1
  52. sub r9, 1
  53. cmp r9, 0
  54. jg print_time_us_loop
  55. ; handling seconds, minutes & hours
  56. mov rax, [tv_cur_s]
  57. mov rbx, [tv_start_s]
  58. sub rax, rbx
  59. ; rax now contain elapsed seconds
  60. ; filling timestr with seconds & minutes
  61. add rax, 3559
  62. xor rdx, rdx
  63. mov rcx, 10
  64. div rcx
  65. push rax
  66. mov rax, rdx
  67. add al, 0x30
  68. mov byte [timestr + 5], al
  69. pop rax
  70. xor rdx, rdx
  71. mov rcx, 6
  72. div rcx
  73. push rax
  74. mov rax, rdx
  75. add al, 0x30
  76. mov byte [timestr + 4], al
  77. pop rax
  78. xor rdx, rdx
  79. mov rcx, 10
  80. div rcx
  81. push rax
  82. mov rax, rdx
  83. add al, 0x30
  84. mov byte [timestr + 2], al
  85. pop rax
  86. xor rdx, rdx
  87. mov rcx, 6
  88. div rcx
  89. push rax
  90. mov rax, rdx
  91. add al, 0x30
  92. mov byte[timestr + 1], al
  93. ; filling the hours buffer
  94. ; r8 will contain our digits counter : max is 8
  95. mov r8, 8
  96. pop rax
  97. print_time_hours_loop:
  98. mov rcx, 10
  99. xor rdx, rdx
  100. div rcx
  101. add dl, 0x30
  102. cmp rax, 0
  103. jne print_time_hours_print_mod
  104. cmp rdx, 0
  105. je print_time_hours_loop_end
  106. print_time_hours_print_mod:
  107. mov r9, hours
  108. add r9, r8
  109. mov byte [r9], dl
  110. cmp r8, 0
  111. je fault
  112. sub r8, 1
  113. cmp rax, 0
  114. jne print_time_hours_loop
  115. print_time_hours_loop_end:
  116. ; print hours + timestr
  117. add r8, 1
  118. cmp r8, 7
  119. jle print_time_hours_cont
  120. mov r8, 7
  121. print_time_hours_cont:
  122. mov r9, hours
  123. add r9, r8
  124. mov rcx, 9
  125. sub rcx, r8
  126. add rcx, timestrlen
  127. mov rax, 1 ; write
  128. mov rdi, 1 ; stdout
  129. mov rsi, r9
  130. mov rdx, rcx
  131. syscall
  132. ; Attempt to read from stdin
  133. ; if something read, enter has been pressed
  134. mov rax, 0
  135. mov rdi, 0
  136. mov rsi, buf
  137. mov rdx, 1
  138. syscall
  139. cmp rax, 0
  140. jge flush_stdin ; flush stdin and exit
  141. sleep:
  142. mov rax, 35
  143. mov rdi, ts_sleep
  144. mov rsi, 0
  145. syscall
  146. jmp print_time ; main loop
  147. flush_stdin:
  148. mov rax, 0
  149. mov rdi, 0
  150. mov rsi, buf
  151. mov rdx, 1
  152. syscall
  153. cmp rax, 0
  154. je newline
  155. jg flush_stdin
  156. exit:
  157. mov rax, 60 ; sys_exit
  158. mov rdi, 0 ; OK
  159. syscall
  160. fault:
  161. mov rax, 60 ; sys_exit
  162. mov rdi, 1 ; failure
  163. syscall
  164. newline:
  165. mov rax, 1
  166. mov rdi, 1
  167. mov rsi, nl
  168. mov rdx, 1
  169. syscall
  170. jmp exit
  171. section .data
  172. ts_start:
  173. tv_start_s dq 0
  174. tv_start_us dq 0
  175. msg: db "0 Hello, world!", 10
  176. msglen: equ $ - msg
  177. hours: db "000000000"
  178. timestr: db ":00:00.0000 ", 0x0D
  179. timestrlen: equ $ - timestr
  180. nl: db 10
  181. buf: db 0
  182. ts_cur:
  183. tv_cur_s dq 0
  184. tv_cur_us dq 0
  185. ts_sleep:
  186. tv_sleep_s dd 0,0
  187. tv_sleep_us dd 10000000,0
  188. ;; 1/2s sleep
  189. ;ts_sleep:
  190. ; tv_sleep_s dq 0
  191. ; tv_sleep_us dq 500000000
  192. .end: