|
@@ -68,10 +68,6 @@ ENDSTRUC
|
68
|
68
|
%define sigaction.sa_mask sigaction+SIGACTION_STRUC.sa_mask
|
69
|
69
|
%endmacro
|
70
|
70
|
|
71
|
|
-section .bss
|
72
|
|
-align 8
|
73
|
|
- buf: resb 1
|
74
|
|
-
|
75
|
71
|
section .data
|
76
|
72
|
align 8
|
77
|
73
|
|
|
@@ -90,6 +86,8 @@ align 8
|
90
|
86
|
timestrlen: equ timestrend - timestr
|
91
|
87
|
%define HOURSLEN 8
|
92
|
88
|
|
|
89
|
+ buf: db 0
|
|
90
|
+
|
93
|
91
|
fcntl_flag: dq 0
|
94
|
92
|
SIGACTION sigaction
|
95
|
93
|
|
|
@@ -141,13 +139,19 @@ _start:
|
141
|
139
|
jmp arg_parse
|
142
|
140
|
arg_ok:
|
143
|
141
|
|
|
142
|
+mov rbx, ts_start
|
|
143
|
+%define RBXPTR(x) rbx + x - ts_start
|
|
144
|
+
|
144
|
145
|
; set stdin reads non blocking
|
145
|
146
|
xor rdx, rdx
|
146
|
147
|
xor rdi, rdi
|
147
|
148
|
mov rax, 72 ; fcntl
|
148
|
149
|
mov rsi, 3 ; F_GETFL
|
149
|
150
|
syscall
|
150
|
|
-mov [fcntl_flag], rax
|
|
151
|
+ mov rdx, O_NONBLOCK
|
|
152
|
+ not rdx
|
|
153
|
+ and rax, rdx
|
|
154
|
+mov [RBXPTR(fcntl_flag)], rax
|
151
|
155
|
mov rdx, rax
|
152
|
156
|
or rdx, O_NONBLOCK
|
153
|
157
|
mov rax, 72 ; fcntl
|
|
@@ -158,15 +162,17 @@ jne fault
|
158
|
162
|
|
159
|
163
|
; preparing SIGINT catch
|
160
|
164
|
mov rax, proc_lap_handler
|
161
|
|
-mov qword [sigaction.sa_handler], rax
|
162
|
|
-mov eax, 0x14000000 ; SA_RESTART | SA_RESTORER
|
163
|
|
-mov dword [sigaction.sa_flags], eax
|
164
|
|
-mov rax, sig_restorer
|
165
|
|
-mov qword [sigaction.sa_restorer], rax
|
|
165
|
+mov qword [RBXPTR(sigaction.sa_handler)], rax
|
|
166
|
+mov ecx, 0x14000000 ; SA_RESTART | SA_RESTORER
|
|
167
|
+mov dword [RBXPTR(sigaction.sa_flags)], ecx
|
|
168
|
+add rax, sig_restorer - proc_lap_handler
|
|
169
|
+mov qword [RBXPTR(sigaction.sa_restorer)], rax
|
166
|
170
|
|
167
|
171
|
mov rax, 13 ; sys_rt_sigaction
|
168
|
172
|
mov rdi, 2 ; SIGINT
|
169
|
|
-mov rsi, sigaction
|
|
173
|
+mov rsi, rbx
|
|
174
|
+add rsi, sigaction - ts_start
|
|
175
|
+;mov rsi, sigaction
|
170
|
176
|
mov rdx, 0 ; NULL
|
171
|
177
|
mov r10, 8 ; sig_size
|
172
|
178
|
syscall
|
|
@@ -175,12 +181,13 @@ jne fault
|
175
|
181
|
|
176
|
182
|
mov rax, 228 ; clock_gettime
|
177
|
183
|
mov rdi, 0 ; CLOCK_REALTIME
|
178
|
|
-mov rsi, ts_start
|
|
184
|
+mov rsi, rbx
|
179
|
185
|
syscall
|
180
|
186
|
|
181
|
187
|
mov rax, 1 ; write
|
182
|
188
|
mov rdi, 2 ; stderr
|
183
|
|
-mov rsi, startmsg
|
|
189
|
+mov rsi, rbx
|
|
190
|
+add rsi, startmsg - ts_start
|
184
|
191
|
mov rdx, startmsglen
|
185
|
192
|
syscall
|
186
|
193
|
|
|
@@ -188,41 +195,49 @@ syscall
|
188
|
195
|
; div sleep time by 10 for each digits added bellow seconds
|
189
|
196
|
mov rax, 100000000
|
190
|
197
|
xor rcx, rcx
|
191
|
|
-mov cl, [time_res]
|
|
198
|
+mov cl, [RBXPTR(time_res)]
|
192
|
199
|
dec cl
|
193
|
200
|
test cl, cl
|
194
|
201
|
jz setsleep_loopend
|
195
|
202
|
|
196
|
|
-mov rbx, 10
|
|
203
|
+mov rsi, 10
|
197
|
204
|
xor rdx, rdx
|
198
|
205
|
setsleep_loop:
|
199
|
|
- div rbx
|
|
206
|
+ div rsi
|
200
|
207
|
loop setsleep_loop
|
201
|
208
|
setsleep_loopend:
|
202
|
209
|
|
203
|
|
-mov [ts_sleep.tv_nsec], rax
|
|
210
|
+mov [RBXPTR(ts_sleep.tv_nsec)], rax
|
|
211
|
+mov r12, proc_print_time
|
|
212
|
+mov r10, 0x0d02
|
204
|
213
|
|
205
|
214
|
std ; set DF for string operations
|
206
|
215
|
|
207
|
|
-align 32
|
|
216
|
+align 16
|
208
|
217
|
main_loop:
|
209
|
|
- push 2 ; stderr
|
210
|
|
- push 0xD ; \r
|
211
|
|
- call proc_print_time
|
212
|
218
|
|
|
219
|
+ push rbx
|
|
220
|
+ push r10 ; \r & stderr
|
|
221
|
+ call r12
|
|
222
|
+
|
|
223
|
+ pop rbx
|
213
|
224
|
; Attempt to read from stdin
|
214
|
225
|
; if something read, enter has been pressed
|
215
|
226
|
xor rax, rax
|
216
|
|
- xor rdi, rdi
|
217
|
|
- mov rsi, buf
|
|
227
|
+ push rax
|
|
228
|
+ push rax
|
|
229
|
+ pop rdi
|
|
230
|
+ mov rsi, rbx
|
|
231
|
+ add rsi, buf - ts_start
|
218
|
232
|
mov rdx, 1
|
219
|
233
|
syscall
|
220
|
234
|
test rax, rax
|
221
|
235
|
jns flush_stdin ; flush & exit
|
222
|
236
|
|
223
|
237
|
mov rax, 35 ; nanosleep
|
224
|
|
- mov rdi, ts_sleep
|
225
|
|
- xor rsi, rsi
|
|
238
|
+ mov rdi, rbx
|
|
239
|
+ add rdi, ts_sleep - ts_start
|
|
240
|
+ pop rsi
|
226
|
241
|
syscall
|
227
|
242
|
|
228
|
243
|
jmp main_loop ; main_loop
|
|
@@ -281,7 +296,7 @@ newline_exit:
|
281
|
296
|
; push FD & push CHR to set arguments
|
282
|
297
|
; Warning : DF must be set
|
283
|
298
|
;
|
284
|
|
-align 32
|
|
299
|
+align 16
|
285
|
300
|
proc_print_time:
|
286
|
301
|
|
287
|
302
|
mov rax, 228 ; clock_gettime
|
|
@@ -303,7 +318,7 @@ proc_print_time:
|
303
|
318
|
xor rdx, rdx
|
304
|
319
|
div qword [RSIPTR(ts_sleep.tv_nsec)] ; Divide result given time_res
|
305
|
320
|
|
306
|
|
- mov rbx, [rsp + 8]
|
|
321
|
+ mov bl, [rsp + 9]
|
307
|
322
|
mov byte [RSIPTR(timestrend - 1)], bl ; set last chr from 1st argument
|
308
|
323
|
|
309
|
324
|
; set the nanosec chars (time_res chars) in timestr
|
|
@@ -373,7 +388,8 @@ proc_print_time:
|
373
|
388
|
or rbx, rdi
|
374
|
389
|
mov [RSIPTR(hours)], rbx
|
375
|
390
|
|
376
|
|
- mov rdi, [rsp + 16] ; fd as 2nd argument
|
|
391
|
+ xor rdi, rdi
|
|
392
|
+ mov dil, [rsp + 8] ; fd as 2nd argument
|
377
|
393
|
mov rdx, timestrlen + HOURSLEN
|
378
|
394
|
sub rdx, rax ; timestr + hours len
|
379
|
395
|
lea rsi, [RSIPTR(hours + rax)] ; hours start pointer
|
|
@@ -382,7 +398,8 @@ proc_print_time:
|
382
|
398
|
test rax, rax
|
383
|
399
|
js fault
|
384
|
400
|
|
385
|
|
- ret 16
|
|
401
|
+ ret 8
|
|
402
|
+%undef RSIPTR
|
386
|
403
|
|
387
|
404
|
|
388
|
405
|
|
|
@@ -438,8 +455,7 @@ proc_lap_handler:
|
438
|
455
|
js fault
|
439
|
456
|
|
440
|
457
|
std ; set DF for string operations
|
441
|
|
- push 1 ; stdout
|
442
|
|
- push 0xA ; \n
|
|
458
|
+ push 0x0A01 ; \n & stdout
|
443
|
459
|
call proc_print_time
|
444
|
460
|
|
445
|
461
|
ret
|