Преглед изворни кода

More main_loop and _start code enhancement

Yann Weber пре 6 година
родитељ
комит
36abef3e4e
1 измењених фајлова са 52 додато и 62 уклоњено
  1. 52
    62
      wtfstopw.asm

+ 52
- 62
wtfstopw.asm Прегледај датотеку

@@ -148,17 +148,14 @@ xor rdi, rdi
148 148
 mov rax, 72 ; fcntl
149 149
 mov rsi, 3 ; F_GETFL
150 150
 syscall
151
-					mov rdx, O_NONBLOCK
152
-					not rdx
153
-					and rax, rdx
154 151
 mov [RBXPTR(fcntl_flag)], rax
155 152
 mov rdx, rax
156
-or rdx, O_NONBLOCK
153
+or dx, O_NONBLOCK
157 154
 mov rax, 72 ; fcntl
158 155
 mov rsi, 4 ; F_SETFL
159 156
 syscall
160
-cmp rax, 0
161
-jne fault
157
+test al, al
158
+js fault
162 159
 
163 160
 ; preparing SIGINT catch
164 161
 mov rax, proc_lap_handler
@@ -208,70 +205,63 @@ setsleep_loop:
208 205
 setsleep_loopend:
209 206
 
210 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 213
 std ; set DF for string operations
215 214
 
216 215
 align 16
217 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 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 222
 	syscall
234
-	test rax, rax
223
+	test al, al
235 224
 	jns flush_stdin ; flush & exit
236
-
225
+	
237 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 229
 	syscall
242 230
 
243 231
 jmp main_loop ; main_loop
244 232
 
245 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 238
 	syscall
251
-	test rax, rax
239
+	test al, al
252 240
 	jz newline_exit
253 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 254
 exit:
260
-	push rdi
261 255
 	; restoring stdin state
262 256
 	mov rax, 72 ; fcntl
263 257
 	xor rdi, rdi
264 258
 	mov rsi, 4 ; F_SETFL
265 259
 	mov rdx, [fcntl_flag]
266 260
 	syscall
261
+	mov rdi, rbx ; return code
267 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 264
 	mov rax, 60 ; sys_exit
274
-	pop rdi ; return code
275 265
 	syscall
276 266
 fault:
277 267
 	mov rdi, 2 ; stderr
@@ -281,19 +271,11 @@ fault:
281 271
 	mov rdx, faultmsglen
282 272
 	syscall
283 273
 
284
-	mov rdi, 1 ; failure
274
+	mov rbx, 1 ; failure
285 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 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 279
 ; 	Warning : DF must be set
298 280
 ;
299 281
 align 16
@@ -318,7 +300,8 @@ proc_print_time:
318 300
 	xor rdx, rdx
319 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 305
 	mov byte [RSIPTR(timestrend - 1)], bl ; set last chr from 1st argument
323 306
 
324 307
 	; set the nanosec chars (time_res chars) in timestr
@@ -370,16 +353,16 @@ proc_print_time:
370 353
 		div rdi
371 354
 		shl rbx, 8
372 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 364
 	jz procpt_looph2_end
382
-	procpt_looph2:
365
+	procpt_looph2: ; end rbx left shift before sto
383 366
 		shl rbx, 8
384 367
 		loopne procpt_looph2
385 368
 	procpt_looph2_end:
@@ -389,7 +372,7 @@ proc_print_time:
389 372
 	mov [RSIPTR(hours)], rbx
390 373
 
391 374
 	xor rdi, rdi
392
-	mov dil, [rsp + 8] ; fd as 2nd argument
375
+	mov dil, r12b ; fd as 2nd argument
393 376
 	mov rdx, timestrlen + HOURSLEN
394 377
 	sub rdx, rax ; timestr + hours len
395 378
 	lea rsi, [RSIPTR(hours + rax)] ; hours start pointer
@@ -398,7 +381,14 @@ proc_print_time:
398 381
 	test rax, rax
399 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 392
 %undef RSIPTR
403 393
 
404 394
 
@@ -455,7 +445,7 @@ proc_lap_handler:
455 445
 	js fault
456 446
 
457 447
 	std ; set DF for string operations
458
-	push 0x0A01 ; \n & stdout
448
+	mov r12w, 0x0A01 ; \n & stdout
459 449
 	call proc_print_time
460 450
 
461 451
 	ret

Loading…
Откажи
Сачувај