Browse Source

Reducing proc_print_time size

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

Loading…
Cancel
Save