Explorar el Código

Argument parsing code enhancement

Using call & ret (but that's maybe not necessary...)
Reducing instruction size
Yann Weber hace 6 años
padre
commit
8b63d50095
Se han modificado 1 ficheros con 34 adiciones y 33 borrados
  1. 34
    33
      wtfstopw.asm

+ 34
- 33
wtfstopw.asm Ver fichero

@@ -136,8 +136,7 @@ global _start
136 136
 _start:
137 137
 
138 138
 ; parse arguments and set time_res value
139
-jmp arg_parse
140
-arg_ok:
139
+call arg_parse
141 140
 
142 141
 mov rbx, ts_start
143 142
 %define RBXPTR(x) rbx + x - ts_start
@@ -460,78 +459,80 @@ sig_restorer:
460 459
 ;
461 460
 arg_parse:
462 461
 	; Checking argument count
463
-	mov rax, [rsp]
464
-	cmp rax, 1
462
+	mov rcx, [rsp+8] ; argc
463
+	mov rsi, [rsp+24] ; argv[1]
464
+	mov rdi, badarg_msg
465
+
466
+	cmp rcx, 1
465 467
 	je arg_ok
466
-	cmp rax, 3
468
+	cmp rcx, 3
467 469
 	jle parse_r
468 470
 	arg_err: ; badval & badarg jmp here too
469
-		mov rax, [rsp+8] ; argv[0] program name
471
+		mov rax, [rsp+16] ; argv[0] program name
470 472
 		mov rdi, -1 ; return status
471 473
 		jmp usage
472 474
 	parse_r:
473
-	mov rax, [rsp+16] ; 1st arg should be "-r"
474
-	mov bl, [rax]
475
-	cmp bl, '-'
475
+	mov rax, rsi
476
+	cmp byte [rsi], '-'
476 477
 	jne badarg
477
-	mov bl, [rax+1]
478
+	mov bl, [rsi+1]
478 479
 	cmp bl, 'h' ; -h
479 480
 	je arg_err
480 481
 	cmp bl, 'r'
481 482
 	jne badarg
482
-	mov bl, [rax+2]
483
-	cmp bl, 0
483
+	cmp byte [rsi+2], 0
484 484
 	jne arg_nxt ; the value seems to be just after the -r like -r2
485 485
 	nxt_arg:
486 486
 		; the 1st argument is -r the second must be the time_res
487 487
 		; check that the arg exists
488
-		mov rax, [rsp]
489
-		cmp rax, 3
488
+		cmp cl, 3
490 489
 		jne arg_err
491
-		mov rax, [rsp+24]
490
+		mov rsi, [rsp+32]
492 491
 		jmp arg_cont
493 492
 	arg_nxt:
494 493
 		; check that there is no more args
495
-		mov rbx, [rsp]
496
-		cmp rbx, 2
494
+		cmp cl, 2
497 495
 		jne arg_err
498
-		add rax, 2
496
+		add rsi, 2
499 497
 	arg_cont:
500
-	; rax should point on the value
501
-	mov bl, [rax+1]
502
-	cmp bl, 0
498
+	; rsi should point on the ASCII digit counter
499
+	cmp byte [rsi+1], 0
503 500
 	jne badval
504 501
 	xor rbx, rbx
505
-	mov bl, [rax]
502
+	mov bl, [rsi]
506 503
 	cmp bl, '1'
507 504
 	jl badval
508 505
 	cmp bl, '8'
509 506
 	jg badval
510
-	sub bl, '0'
511
-	mov [time_res], rbx
512
-	jmp arg_ok
507
+	xor bl, 0x30 ; '0'
508
+	mov [rdi + time_res - badarg_msg], rbx
509
+
510
+	arg_ok:
511
+	ret
513 512
 
514
-; print an error message, usage and exit with rax pointing the value
513
+; print an error message, usage and exit with rax pointing the value and
514
+; rdi badarg_msg ptr
515 515
 badval:
516
-	mov rsi, badval_msg
516
+	mov rbx, rsi
517
+
518
+	lea rsi, [rdi + badval_msg - badarg_msg]
517 519
 	mov rdx, badval_msglen
518 520
 	jmp arg_strerr
519 521
 
520 522
 ; print an error message, usage and exit
521 523
 badarg:
522
-	mov rsi, badarg_msg
524
+	mov rbx, rsi
525
+	mov rsi, rdi
523 526
 	mov rdx, badarg_msglen
524
-	mov rax, [rsp+16]
525 527
 	jmp arg_strerr
526 528
 
527
-; rsi msg ptr, rdx msg len, rax arg ptr
529
+; rsi msg ptr, rdx msg len, rbx arg ptr
528 530
 arg_strerr:
529
-	mov r8, rax
530 531
 	mov rax, 1
531 532
 	mov rdi, 1
532 533
 	syscall
533
-	mov rsi, r8
534
-	mov rax, r8
534
+	mov rsi, rbx
535
+	mov rax, rbx
535 536
 	call proc_strlen
536 537
 	mov rax, 1
537 538
 	syscall

Loading…
Cancelar
Guardar