Browse Source

SIGINT now trigger new lap on stdout

Yann Weber 5 years ago
parent
commit
5fda46d4e9
1 changed files with 105 additions and 77 deletions
  1. 105
    77
      wtfstopw.asm

+ 105
- 77
wtfstopw.asm View File

@@ -1,22 +1,27 @@
1
-;WTFStopW : a simple stopwatch
2
-;Copyright (C) 2018 Weber Yann
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
+
3 17
 ;
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.
18
+; A simple precise stopwatch
19
+; Build : nasm -felf64 wtfstopw.asm && ld wtfstopw.o -o wtfstopw
8 20
 ;
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.
21
+; Usage : ./wtfstopw
22
+;	press enter to exit
23
+;	send SIGINT (with kill -2 or ctrl + c) for new lap on stdout
13 24
 ;
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 with : 
19
-; nasm -felf64 wtfstopw.asm && ld wtfstopw.o -o wtfstopw
20 25
 
21 26
 [bits 64]
22 27
 
@@ -76,13 +81,14 @@ section .data
76 81
 	msglen: equ $ - msg
77 82
 
78 83
 	hours: db "000000000"
79
-	timestr: db ":00:00.0000    ", 0x0D
84
+	timestr: db ":00:00.0000    ", 0x0a
80 85
 	timestrlen: equ $ - timestr
81 86
 
82 87
 	nl: db 0x0A
83 88
 	buf: db 0
84 89
 
85
-	dbgmsg: db "SigINT", 0xa
90
+	lapsmsg: db 0x0d, "New lap : "
91
+	lapsmsglen: equ $ - lapsmsg
86 92
 
87 93
 section .text
88 94
 global _start
@@ -96,7 +102,6 @@ mov rsi, 3 ; F_GETFL
96 102
 syscall
97 103
 mov rdx, rax
98 104
 or rdx, 0x800 ; O_NONBLOCK
99
-dbg:
100 105
 mov rax, 72 ; fcntl
101 106
 mov rsi, 4 ; F_SETFL
102 107
 syscall
@@ -126,8 +131,75 @@ mov rdi, 0 ; CLOCK_REALTIME
126 131
 mov rsi, ts_start
127 132
 syscall
128 133
 
134
+main_loop:
135
+	push 2 ; stderr
136
+	push 0x0D ; \r
137
+	call print_time
138
+
139
+	; Attempt to read from stdin
140
+	; if something read, enter has been pressed
141
+	mov rax, 0
142
+	mov rdi, 0
143
+	mov rsi, buf
144
+	mov rdx, 1
145
+	syscall
146
+	cmp rax, 0
147
+	jge flush_stdin ; flush stdin and exit
148
+
149
+	mov rax, 35 ; nanosleep
150
+	mov rdi, ts_sleep
151
+	mov rsi, 0
152
+	syscall
153
+
154
+	jmp main_loop
155
+
156
+flush_stdin:
157
+	mov rax, 0
158
+	mov rdi, 0
159
+	mov rsi, buf
160
+	mov rdx, 1
161
+	syscall
162
+	cmp rax, 0
163
+	je newline_exit
164
+	jg flush_stdin
165
+
166
+exit:
167
+	mov rax, 60 ; sys_exit
168
+	mov rdi, 0 ; OK
169
+	syscall
170
+fault:
171
+	mov rax, 1 ; write
172
+	mov rdi, 2 ; stderr
173
+	mov rsi, nl
174
+	mov rdx, 1
175
+	syscall
176
+	mov rax, 1
177
+	mov rsi, faultmsg
178
+	mov rdx, faultmsglen
179
+	syscall
180
+
181
+	mov rax, 60 ; sys_exit
182
+	mov rdi, 1 ; failure
183
+	syscall
184
+
185
+newline_exit:
186
+	mov rax, 1
187
+	mov rdi, 1
188
+	mov rsi, nl
189
+	mov rdx, 1
190
+	syscall
191
+	jmp exit
129 192
 
193
+;
194
+; Print current time on FD r10 and put r13b as leading char
195
+;
130 196
 print_time:
197
+	pop r8
198
+	pop r9
199
+	pop r10
200
+	push r8
201
+	push r10
202
+	push r9
131 203
 	; updating ts_cur time
132 204
 	mov rax, 228 ; clock_gettime
133 205
 	mov rdi, 0 ; CLOCK_REALTIME
@@ -232,76 +304,32 @@ print_time:
232 304
 	sub rcx, r8 ; rcx is hours size 
233 305
 	add rcx, timestrlen ; add to timestrlen
234 306
 
307
+	; Set leading char
308
+	pop r10
309
+	mov byte [timestr+timestrlen-1], r10b
310
+
235 311
 	mov rax, 1 ; write
236
-	mov rdi, 1 ; stdout
312
+	pop r10 ; print_time FD arg
313
+	mov rdi, r10 ; print_time arg
237 314
 	mov rsi, r9 ; start hours pointer
238 315
 	mov rdx, rcx ; size to timestr end
239 316
 	syscall
240 317
 
241
-	; Attempt to read from stdin
242
-	; if something read, enter has been pressed
243
-	mov rax, 0
244
-	mov rdi, 0
245
-	mov rsi, buf
246
-	mov rdx, 1
247
-	syscall
248
-	cmp rax, 0
249
-	jge flush_stdin ; flush stdin and exit
250
-
251
-	mov rax, 35 ; nanosleep
252
-	mov rdi, ts_sleep
253
-	mov rsi, 0
254
-	syscall
255
-
256
-	jmp print_time ; main loop
257
-
258
-flush_stdin:
259
-	mov rax, 0
260
-	mov rdi, 0
261
-	mov rsi, buf
262
-	mov rdx, 1
263
-	syscall
264
-	cmp rax, 0
265
-	je newline_exit
266
-	jg flush_stdin
267
-
268
-exit:
269
-	mov rax, 60 ; sys_exit
270
-	mov rdi, 0 ; OK
271
-	syscall
272
-fault:
273
-	mov rax, 1 ; write
274
-	mov rdi, 2 ; stderr
275
-	mov rsi, nl
276
-	mov rdx, 1
277
-	syscall
278
-	mov rax, 1
279
-	mov rsi, faultmsg
280
-	mov rdx, faultmsglen
281
-	syscall
282
-
283
-	mov rax, 60 ; sys_exit
284
-	mov rdi, 1 ; failure
285
-	syscall
318
+	ret
286 319
 
287
-newline_exit:
288
-	mov rax, 1
289
-	mov rdi, 1
290
-	mov rsi, nl
291
-	mov rdx, 1
292
-	syscall
293
-	jmp exit
294 320
 
295 321
 lap_handler:
296
-	pop r8
297
-	
298 322
 	mov rax, 1
299 323
 	mov rdi, 1
300
-	mov rsi, dbgmsg
301
-	mov rdx, 7
324
+	mov rsi, lapsmsg
325
+	mov rdx, lapsmsglen
302 326
 	syscall
303 327
 
304
-	push r8 ; push back ret addr
328
+	push 1 ; stdout
329
+	push 0x0A ; \n
330
+	call print_time
331
+
332
+dbg2:
305 333
 	ret
306 334
 
307 335
 sig_restorer:

Loading…
Cancel
Save