Browse Source

proc_print_time enhancement

Yann Weber 5 years ago
parent
commit
a12fa7fc53
1 changed files with 47 additions and 65 deletions
  1. 47
    65
      wtfstopw.asm

+ 47
- 65
wtfstopw.asm View File

@@ -100,9 +100,10 @@ section .data
100 100
 	badval_msg: db "Value for -r should be in [1..8] but got "
101 101
 	badval_msglen: equ $ - badval_msg
102 102
 
103
-	hours: db "000000000"
103
+	hours: db "000000000" ; increase size of this string to increase hours max
104 104
 	timestr: db ":00:00.0           ", 0x0a
105 105
 	timestrlen: equ $ - timestr
106
+	hourslen: equ timestr - hours ; hours len -> the max number of digits
106 107
 
107 108
 	time_res: dq 2 ; 2 digits bellow seconds can grow to 8
108 109
 
@@ -186,6 +187,7 @@ setsleep_loop:
186 187
 setsleep_endloop:
187 188
 mov [ts_sleep.tv_nsec], rax
188 189
 
190
+std ; set DF for string operations
189 191
 main_loop:
190 192
 	mov rax, 2 ; stderr
191 193
 	mov rdi, 0xD ; \r
@@ -261,6 +263,7 @@ newline_exit:
261 263
 ; Print current time on FD and add a leading char CHR
262 264
 ; push FD & push CHR to set arguments
263 265
 ; rax the FS & rdi the leading CHR
266
+; 	Warning : DF must be set
264 267
 ;
265 268
 proc_print_time:
266 269
 	
@@ -284,41 +287,27 @@ proc_print_time:
284 287
 
285 288
 	print_time_us_cont:
286 289
 	; Divide result given time_res
287
-	mov r10, rax
288
-	mov rax, 1000000000
289
-	mov r9, [time_res]
290
-	mov r8, 10
291
-	xor rdx, rdx
292
-	print_time_respow:
293
-		div r8
294
-		sub r9, 1
295
-		cmp r9, 0
296
-		jg print_time_respow
297
-	mov rcx, rax
298
-	mov rax, r10
299 290
 	xor rdx, rdx
300
-	div rcx
291
+	div qword [ts_sleep.tv_nsec]
301 292
 
302
-	; set the us char in timestr
303
-	mov r8, timestr
304
-	mov r9, [time_res] ; r9 count the number of digits
305
-	add r8, 6 ; first digits after seconds
306
-	add r8, r9 ; r8 points on last char before \r
293
+	; set the nsec char in timestr
294
+	mov rdi, timestr + 6
295
+	mov rsi, rdi
296
+	add rdi, [time_res] ; r8 points on last char before \r
297
+	mov r8, 10
307 298
 	print_time_us_loop:
308 299
 		xor rdx, rdx
309
-		mov rcx, 10
310
-		div rcx
311
-		add dl, 0x30
312
-		mov [r8], dl
313
-		sub r8, 1
314
-		sub r9, 1
315
-		cmp r9, 0
316
-		jg print_time_us_loop
300
+		div r8
301
+		xchg al, dl
302
+		add al, 0x30
303
+		stosb
304
+		mov al, dl
305
+		cmp rsi, rdi
306
+		loopne print_time_us_loop
317 307
 
318 308
 	; handling seconds, minutes & hours
319 309
 	mov rax, [ts_cur.tv_sec]
320
-	mov rbx, [ts_start.tv_sec]
321
-	sub rax, rbx
310
+	sub rax, [ts_start.tv_sec]
322 311
 	; rax now contain elapsed seconds
323 312
 	; filling timestr with seconds & minutes
324 313
 
@@ -328,71 +317,63 @@ proc_print_time:
328 317
 	add dl, 0x30
329 318
 	mov byte [timestr + 5], dl
330 319
 
331
-	xor rdx, rdx
320
+	xor dl, dl
332 321
 	mov rcx, 6
333 322
 	div rcx
334
-	push rax
335 323
 	add dl, 0x30
336 324
 	mov byte [timestr + 4], dl
337 325
 
338
-	xor rdx, rdx
326
+	xor dl, dl
339 327
 	mov rcx, 10
340 328
 	div rcx
341 329
 	add dl, 0x30
342 330
 	mov byte [timestr + 2], dl
343 331
 
344
-	pop rax
345
-	xor rdx, rdx
332
+	xor dl, dl
346 333
 	mov rcx, 6
347 334
 	div rcx
348 335
 	add dl, 0x30
349 336
 	mov byte[timestr + 1], dl
350 337
 
351 338
 	; filling the hours buffer
352
-	; r8 will contain our digits counter : max is 8
353
-	mov r8, 8
339
+	; rcx will contain max_digits - digits_count
340
+	; digits max is hourslen - 1
341
+	mov rcx, hourslen - 1
342
+	mov rdi, hours + hourslen - 1
343
+	mov r10, 10
354 344
 	print_time_hours_loop:
355
-		mov rcx, 10
356
-		xor rdx, rdx
357
-		div rcx
358
-		add dl, 0x30
345
+		xor dl, dl
346
+		div r10
359 347
 		cmp rax, 0
360 348
 		jne print_time_hours_print_mod
361
-		cmp rdx, 0
349
+		cmp dl, 0
362 350
 		je print_time_hours_loop_end
363 351
 
364 352
 		print_time_hours_print_mod:
365
-		mov r9, hours
366
-		add r9, r8
367
-		mov byte [r9], dl
368
-		cmp r8, 0
353
+		xchg al, dl
354
+		add al, 0x30
355
+		stosb
356
+		mov al, dl
357
+		cmp rcx, 0
369 358
 		je fault
370
-		sub r8, 1
371 359
 		cmp rax, 0
372
-		jne print_time_hours_loop
360
+		loopne print_time_hours_loop
373 361
 	print_time_hours_loop_end:
374 362
 
375
-	; print hours + timestr
376
-	add r8, 1
377
-	cmp r8, 7
378
-	jle print_time_hours_cont
379
-	mov r8, 7 ; maximum value for r8
380
-	print_time_hours_cont:
381
-	mov r9, hours
382
-	add r9, r8
383
-	mov rcx, 9
384
-	sub rcx, r8 ; rcx is hours size 
385
-	add rcx, timestrlen ; add to timestrlen
386
-
387
-	; Set leading char
388
-	pop r10
363
+	; set rcx for 2 digits at least for hours
364
+	mov r9, hourslen - 2
365
+	add rcx, 1
366
+	cmp rcx, r9
367
+	cmovnle rcx, r9
368
+
369
+	pop r10 ; pop leading char argument
389 370
 	mov byte [timestr+timestrlen-1], r10b
390 371
 
391 372
 	mov rax, 1 ; write
392
-	pop r10 ; print_time FD arg
393
-	mov rdi, r10 ; print_time arg
394
-	mov rsi, r9 ; start hours pointer
395
-	mov rdx, rcx ; size to timestr end
373
+	pop rdi ; pop fd argument
374
+	mov rdx, timestrlen + 9
375
+	sub rdx, rcx ; timestr + hours len
376
+	lea rsi, [hours + rcx] ; hours start pointer
396 377
 	syscall
397 378
 
398 379
 	ret
@@ -449,6 +430,7 @@ proc_lap_handler:
449 430
 
450 431
 	mov rax, 1 ; stdout
451 432
 	mov rdi, 0xA ; \n
433
+	std ; set DF for string operations
452 434
 	call proc_print_time
453 435
 
454 436
 	ret

Loading…
Cancel
Save