Browse Source

Argument parsing code enhancement

Using call & ret (but that's maybe not necessary...)
Reducing instruction size
Yann Weber 6 years ago
parent
commit
8b63d50095
1 changed files with 34 additions and 33 deletions
  1. 34
    33
      wtfstopw.asm

+ 34
- 33
wtfstopw.asm View File

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

Loading…
Cancel
Save