Browse Source

More main_loop and _start code enhancement

Yann Weber 6 years ago
parent
commit
36abef3e4e
1 changed files with 52 additions and 62 deletions
  1. 52
    62
      wtfstopw.asm

+ 52
- 62
wtfstopw.asm View File

148
 mov rax, 72 ; fcntl
148
 mov rax, 72 ; fcntl
149
 mov rsi, 3 ; F_GETFL
149
 mov rsi, 3 ; F_GETFL
150
 syscall
150
 syscall
151
-					mov rdx, O_NONBLOCK
152
-					not rdx
153
-					and rax, rdx
154
 mov [RBXPTR(fcntl_flag)], rax
151
 mov [RBXPTR(fcntl_flag)], rax
155
 mov rdx, rax
152
 mov rdx, rax
156
-or rdx, O_NONBLOCK
153
+or dx, O_NONBLOCK
157
 mov rax, 72 ; fcntl
154
 mov rax, 72 ; fcntl
158
 mov rsi, 4 ; F_SETFL
155
 mov rsi, 4 ; F_SETFL
159
 syscall
156
 syscall
160
-cmp rax, 0
161
-jne fault
157
+test al, al
158
+js fault
162
 
159
 
163
 ; preparing SIGINT catch
160
 ; preparing SIGINT catch
164
 mov rax, proc_lap_handler
161
 mov rax, proc_lap_handler
208
 setsleep_loopend:
205
 setsleep_loopend:
209
 
206
 
210
 mov [RBXPTR(ts_sleep.tv_nsec)], rax
207
 mov [RBXPTR(ts_sleep.tv_nsec)], rax
211
-mov r12, proc_print_time
212
-mov r10, 0x0d02
208
+mov r12w, 0x0d02
209
+mov r13, proc_print_time
210
+mov r14, buf ; nanosleep
211
+mov r15, ts_sleep
213
 
212
 
214
 std ; set DF for string operations
213
 std ; set DF for string operations
215
 
214
 
216
 align 16
215
 align 16
217
 main_loop:
216
 main_loop:
218
 
217
 
219
-	push rbx
220
-	push r10 ; \r & stderr
221
-	call r12
218
+	call r13 ; proc_print_time
222
 
219
 
223
-	pop rbx
224
-	; Attempt to read from stdin
220
+	; Attempt to read from stdin (syscall prepared by proc_print_time)
225
 	; if something read, enter has been pressed
221
 	; if something read, enter has been pressed
226
-	xor rax, rax
227
-	push rax
228
-	push rax
229
-	pop rdi
230
-	mov rsi, rbx
231
-	add rsi, buf - ts_start
232
-	mov rdx, 1
233
 	syscall
222
 	syscall
234
-	test rax, rax
223
+	test al, al
235
 	jns flush_stdin ; flush & exit
224
 	jns flush_stdin ; flush & exit
236
-
225
+	
237
 	mov rax, 35 ; nanosleep
226
 	mov rax, 35 ; nanosleep
238
-	mov rdi, rbx
239
-	add rdi, ts_sleep - ts_start
240
-	pop rsi
227
+	mov rdi, r15
228
+	xor rsi, rsi
241
 	syscall
229
 	syscall
242
 
230
 
243
 jmp main_loop ; main_loop
231
 jmp main_loop ; main_loop
244
 
232
 
245
 flush_stdin:
233
 flush_stdin:
246
-	mov rax, 0
247
-	mov rdi, 0
248
-	mov rsi, buf
249
-	mov rdx, 1
234
+	xor rax, rax
235
+	;xor rdi, rdi ; done in main_loop
236
+	;mov rsi, r14
237
+	;mov rdx, 1
250
 	syscall
238
 	syscall
251
-	test rax, rax
239
+	test al, al
252
 	jz newline_exit
240
 	jz newline_exit
253
 	jns flush_stdin
241
 	jns flush_stdin
254
 
242
 
255
-mov rdi, 0 ; EXIT OK
243
+	xor rbx, rbx ; EXIT OK
244
+	jmp exit
245
+
246
+	newline_exit:
247
+	call proc_nl
248
+	xor rbx, rbx ; exit OK
249
+	jmp exit
250
+
256
 ;
251
 ;
257
-; Expect rdi to be the return code
252
+; Expect rbx to be the return code
258
 ;
253
 ;
259
 exit:
254
 exit:
260
-	push rdi
261
 	; restoring stdin state
255
 	; restoring stdin state
262
 	mov rax, 72 ; fcntl
256
 	mov rax, 72 ; fcntl
263
 	xor rdi, rdi
257
 	xor rdi, rdi
264
 	mov rsi, 4 ; F_SETFL
258
 	mov rsi, 4 ; F_SETFL
265
 	mov rdx, [fcntl_flag]
259
 	mov rdx, [fcntl_flag]
266
 	syscall
260
 	syscall
261
+	mov rdi, rbx ; return code
267
 	test rax, rax
262
 	test rax, rax
268
-	jz exit_end
269
-	pop rdi ; failed to restore
270
-	push 1 ; exit FAIL
271
-
272
-	exit_end:
263
+	cmovnz rdi, rax ; exit FAIL
273
 	mov rax, 60 ; sys_exit
264
 	mov rax, 60 ; sys_exit
274
-	pop rdi ; return code
275
 	syscall
265
 	syscall
276
 fault:
266
 fault:
277
 	mov rdi, 2 ; stderr
267
 	mov rdi, 2 ; stderr
281
 	mov rdx, faultmsglen
271
 	mov rdx, faultmsglen
282
 	syscall
272
 	syscall
283
 
273
 
284
-	mov rdi, 1 ; failure
274
+	mov rbx, 1 ; failure
285
 	jmp exit
275
 	jmp exit
286
-
287
-newline_exit:
288
-	mov rdi, 1
289
-	call proc_nl
290
-
291
-	mov rdi, 0 ; exit OK
292
-	jmp exit
293
-
294
 ;
276
 ;
295
 ; Print current time on FD and add a leading char CHR
277
 ; Print current time on FD and add a leading char CHR
296
-; push FD & push CHR to set arguments
278
+; argument are in r12w r12b is FD and r12w(h) is CHR
297
 ; 	Warning : DF must be set
279
 ; 	Warning : DF must be set
298
 ;
280
 ;
299
 align 16
281
 align 16
318
 	xor rdx, rdx
300
 	xor rdx, rdx
319
 	div qword [RSIPTR(ts_sleep.tv_nsec)] ; Divide result given time_res
301
 	div qword [RSIPTR(ts_sleep.tv_nsec)] ; Divide result given time_res
320
 
302
 
321
-	mov bl, [rsp + 9]
303
+	mov bx, r12w
304
+	shr bx, 8
322
 	mov byte [RSIPTR(timestrend - 1)], bl ; set last chr from 1st argument
305
 	mov byte [RSIPTR(timestrend - 1)], bl ; set last chr from 1st argument
323
 
306
 
324
 	; set the nanosec chars (time_res chars) in timestr
307
 	; set the nanosec chars (time_res chars) in timestr
370
 		div rdi
353
 		div rdi
371
 		shl rbx, 8
354
 		shl rbx, 8
372
 		xchg bl, dl
355
 		xchg bl, dl
373
-		cmp rax, 0
374
-		loopne procpt_looph
375
-	procpt_looph_end:
376
-
377
-	mov rax, HOURSLEN - 2
378
-	cmp rcx, HOURSLEN - 2
379
-	cmovl rax, rcx ; rsi stores hours digit count (at least 2)
380
-	cmp rcx, 0
356
+		test rax, rax
357
+		loopnz procpt_looph
358
+
359
+	xor rax, rax
360
+	mov al, HOURSLEN - 2
361
+	cmp cx, HOURSLEN - 2
362
+	cmovl ax, cx ; rax stores hours digit count (at least 2)
363
+	test cl, cl
381
 	jz procpt_looph2_end
364
 	jz procpt_looph2_end
382
-	procpt_looph2:
365
+	procpt_looph2: ; end rbx left shift before sto
383
 		shl rbx, 8
366
 		shl rbx, 8
384
 		loopne procpt_looph2
367
 		loopne procpt_looph2
385
 	procpt_looph2_end:
368
 	procpt_looph2_end:
389
 	mov [RSIPTR(hours)], rbx
372
 	mov [RSIPTR(hours)], rbx
390
 
373
 
391
 	xor rdi, rdi
374
 	xor rdi, rdi
392
-	mov dil, [rsp + 8] ; fd as 2nd argument
375
+	mov dil, r12b ; fd as 2nd argument
393
 	mov rdx, timestrlen + HOURSLEN
376
 	mov rdx, timestrlen + HOURSLEN
394
 	sub rdx, rax ; timestr + hours len
377
 	sub rdx, rax ; timestr + hours len
395
 	lea rsi, [RSIPTR(hours + rax)] ; hours start pointer
378
 	lea rsi, [RSIPTR(hours + rax)] ; hours start pointer
398
 	test rax, rax
381
 	test rax, rax
399
 	js fault
382
 	js fault
400
 
383
 
401
-	ret 8
384
+; ALLIGN TRICK : prepare read syscall for main_loop
385
+
386
+	xor rax, rax
387
+	xor rdi, rdi
388
+	mov rsi, r14
389
+	mov rdx, 1
390
+
391
+	ret
402
 %undef RSIPTR
392
 %undef RSIPTR
403
 
393
 
404
 
394
 
455
 	js fault
445
 	js fault
456
 
446
 
457
 	std ; set DF for string operations
447
 	std ; set DF for string operations
458
-	push 0x0A01 ; \n & stdout
448
+	mov r12w, 0x0A01 ; \n & stdout
459
 	call proc_print_time
449
 	call proc_print_time
460
 
450
 
461
 	ret
451
 	ret

Loading…
Cancel
Save