Browse Source

Reducing proc_print_time size

Using smaller operations
Yann Weber 5 years ago
parent
commit
9009013637
1 changed files with 52 additions and 42 deletions
  1. 52
    42
      wtfstopw.asm

+ 52
- 42
wtfstopw.asm View File

@@ -27,6 +27,8 @@
27 27
 ; 	press enter to exit
28 28
 ; 	send SIGINT (with kill -2 ir ctrl + c) for a new lap
29 29
 [bits 64]
30
+%use smartalign
31
+ALIGNMODE k8
30 32
 
31 33
 %define O_NONBLOCK 0x800
32 34
 
@@ -72,10 +74,13 @@ align 8
72 74
 
73 75
 section .data
74 76
 align 8
77
+
75 78
 	TIMESPEC ts_start
76 79
 	TIMESPEC ts_cur
77 80
 	TIMESPEC ts_sleep
78 81
 
82
+	time_res: dq 2 ; 2 digits bellow seconds can grow to 8
83
+
79 84
 	hours: times 8 db '0' ; 8 digits is the max for hours
80 85
 	timestr: db ":00:00." 
81 86
 		times 8 db '0' ; 8 digits for nanoseconds
@@ -85,8 +90,6 @@ align 8
85 90
 	timestrlen: equ timestrend - timestr
86 91
 	%define HOURSLEN 8
87 92
 
88
-	time_res: dq 2 ; 2 digits bellow seconds can grow to 8
89
-
90 93
 	fcntl_flag: dq 0
91 94
 	SIGACTION sigaction
92 95
 
@@ -197,6 +200,8 @@ setsleep_endloop:
197 200
 mov [ts_sleep.tv_nsec], rax
198 201
 
199 202
 std ; set DF for string operations
203
+
204
+align 32
200 205
 main_loop:
201 206
 	push 2 ; stderr
202 207
 	push 0xD ; \r
@@ -225,9 +230,9 @@ flush_stdin:
225 230
 	mov rsi, buf
226 231
 	mov rdx, 1
227 232
 	syscall
228
-	cmp rax, 0
229
-	je newline_exit
230
-	jg flush_stdin
233
+	test rax, rax
234
+	jz newline_exit
235
+	jns flush_stdin
231 236
 
232 237
 mov rdi, 0 ; EXIT OK
233 238
 ;
@@ -241,8 +246,8 @@ exit:
241 246
 	mov rsi, 4 ; F_SETFL
242 247
 	mov rdx, [fcntl_flag]
243 248
 	syscall
244
-	cmp rax, 0
245
-	je exit_end
249
+	test rax, rax
250
+	jz exit_end
246 251
 	pop rdi ; failed to restore
247 252
 	push 1 ; exit FAIL
248 253
 
@@ -268,42 +273,43 @@ newline_exit:
268 273
 	mov rdi, 0 ; exit OK
269 274
 	jmp exit
270 275
 
271
-
272
-
273
-align 32
274 276
 ;
275 277
 ; Print current time on FD and add a leading char CHR
276 278
 ; push FD & push CHR to set arguments
277 279
 ; 	Warning : DF must be set
278 280
 ;
281
+align 32
279 282
 proc_print_time:
280 283
 	
281 284
 	mov rax, 228 ; clock_gettime
282
-	mov rdi, 0 ; CLOCK_REALTIME
285
+	xor rdi, rdi ; CLOCK_REALTIME
283 286
 	mov rsi, ts_cur
284 287
 	syscall ; updating ts_cur time
285 288
 
286
-	mov rax, [ts_cur.tv_nsec]
287
-	sub rax, [ts_start.tv_nsec]
288
-	cmp rax, 0 ; Calculating elapsed ns
289
+	mov rsi, ts_start.tv_sec
290
+%define RSIPTR(x) rsi + x - ts_start.tv_sec
291
+
292
+	mov rax, [RSIPTR(ts_cur.tv_nsec)] ;mov rax, [ts_cur.tv_nsec]
293
+	sub rax, [RSIPTR(ts_start.tv_nsec)] ;sub rax, [ts_start.tv_nsec]
289 294
 	jge print_time_us_cont
290 295
 	add rax, 1000000000 ; negativ result
291
-	sub qword [ts_cur.tv_sec], 1
296
+	sub qword [RSIPTR(ts_cur.tv_sec)], 1 ;sub qword [ts_cur.tv_sec], 1
292 297
 	print_time_us_cont:
293 298
 
294 299
 	xor rdx, rdx
295
-	div qword [ts_sleep.tv_nsec] ; Divide result given time_res
300
+	div qword [RSIPTR(ts_sleep.tv_nsec)] ; Divide result given time_res
296 301
 
297
-	mov r9, [rsp + 8]
298
-	mov byte [timestr+timestrlen-1], r9b ; set last chr from 1st argument
302
+	mov rbx, [rsp + 8]
303
+	mov byte [RSIPTR(timestrend - 1)], bl ; set last chr from 1st argument
299 304
 
300 305
 	; set the nanosec chars (time_res chars) in timestr
301 306
 	mov rbx, 0x2020202020202020
302
-	mov rcx, [time_res]
303
-	mov r8, 10
307
+	mov rcx, [RSIPTR(time_res)]
308
+	mov rdi, 10
309
+	xor rdx, rdx
304 310
 	procpt_loopns:
305
-		xor rdx, rdx
306
-		div r8
311
+		xor dl, dl
312
+		div rdi
307 313
 		or dl, 0x30 ; '0'
308 314
 		shl rbx, 8
309 315
 		mov bl, dl
@@ -311,46 +317,46 @@ proc_print_time:
311 317
 	mov [timestr+7], rbx
312 318
 
313 319
 	; filling timestr with seconds & minutes chars
314
-	mov rax, [ts_cur.tv_sec]
315
-	sub rax, [ts_start.tv_sec] ; rax now contain elapsed seconds
320
+	mov rax, [RSIPTR(ts_cur.tv_sec)]
321
+	sub rax, [RSIPTR(ts_start.tv_sec)] ; rax now contain elapsed seconds
316 322
 
317
-	mov r8, 10
318
-	mov r9, 6
323
+	mov rcx, 10
324
+	mov rdi, 6
319 325
 
320 326
 	xor rdx, rdx
321
-	div r8
327
+	div rcx
322 328
 	mov bh, dl
323 329
 	xor dl, dl
324
-	div r9
330
+	div rdi
325 331
 	mov bl, dl
326 332
 	or bx, 0x3030
327
-	mov word [timestr + 4], bx
333
+	mov word [RSIPTR(timestr + 4)], bx
328 334
 
329 335
 	xor dl, dl
330
-	div r8
336
+	div rcx
331 337
 	mov bh, dl
332 338
 	xor dl, dl
333
-	div r9
339
+	div rdi
334 340
 	mov bl, dl
335 341
 	or bx, 0x3030
336
-	mov word [timestr + 1], bx
342
+	mov word [RSIPTR(timestr + 1)], bx
337 343
 
338 344
 	; using rbx to stores the hours string
339 345
 	xor rbx, rbx
340 346
 	mov rcx, HOURSLEN
341
-	mov r8, 10
347
+	mov rdi, 10
342 348
 	procpt_looph:
343 349
 		xor dl, dl
344
-		div r8
350
+		div rdi
345 351
 		shl rbx, 8
346 352
 		mov bl, dl
347 353
 		cmp rax, 0
348 354
 		loopne procpt_looph
349 355
 	procpt_looph_end:
350 356
 
351
-	mov r8, HOURSLEN - 2
357
+	mov rax, HOURSLEN - 2
352 358
 	cmp rcx, HOURSLEN - 2
353
-	cmovl r8, rcx ; r8 stores hours digit count (at least 2)
359
+	cmovl rax, rcx ; rsi stores hours digit count (at least 2)
354 360
 	cmp rcx, 0
355 361
 	jz procpt_looph2_end
356 362
 	procpt_looph2:
@@ -358,19 +364,23 @@ proc_print_time:
358 364
 		loopne procpt_looph2
359 365
 	procpt_looph2_end:
360 366
 
361
-	mov r9, 0x3030303030303030 ; ASCII convertion
362
-	or rbx, r9
363
-	mov [hours], rbx
367
+	mov rdi, 0x3030303030303030 ; ASCII convertion
368
+	or rbx, rdi
369
+	mov [RSIPTR(hours)], rbx
364 370
 
365
-	mov rax, 1 ; write
366 371
 	mov rdi, [rsp + 16] ; fd as 2nd argument
367
-	lea rsi, [hours + r8] ; hours start pointer
368 372
 	mov rdx, timestrlen + HOURSLEN
369
-	sub rdx, r8 ; timestr + hours len
373
+	sub rdx, rax ; timestr + hours len
374
+	lea rsi, [RSIPTR(hours + rax)] ; hours start pointer
375
+	mov rax, 1 ; write
370 376
 	syscall
377
+	test rax, rax
378
+	js fault
371 379
 
372 380
 	ret 16
373 381
 
382
+
383
+
374 384
 ;
375 385
 ;	sig handler for SIGINT displaying lap count and time on stdout
376 386
 ;

Loading…
Cancel
Save