Browse Source

Add argument parsing

Yann Weber 6 years ago
parent
commit
055357bfb8
1 changed files with 134 additions and 2 deletions
  1. 134
    2
      wtfstopw.asm

+ 134
- 2
wtfstopw.asm View File

66
 
66
 
67
 	ts_sleep:
67
 	ts_sleep:
68
 		tv_sleep_s dq 0
68
 		tv_sleep_s dq 0
69
-		tv_sleep_us dq 10000000
69
+		tv_sleep_us dq 10000000 ; set for 4 digits bellow seconds
70
 
70
 
71
 	;; 1/2s sleep
71
 	;; 1/2s sleep
72
 	;ts_sleep:
72
 	;ts_sleep:
81
 	db 0xA
81
 	db 0xA
82
 	startmsglen: equ $ - startmsg
82
 	startmsglen: equ $ - startmsg
83
 
83
 
84
+	usage_pre: db "Usage : "
85
+	usage_prelen: equ $ - usage_pre
86
+
87
+	usage_post: db " [-h] [-r NDIGITS]", 0xA
88
+		db "Options :", 0xA
89
+		db 0x9, "-h print this help and exit", 0xA
90
+		db 0x9, "-r Number of digits bellow seconds, between [1..8]"
91
+		db 0xA
92
+	usage_postlen: equ $ - usage_post
93
+
94
+	badarg_msg: db "Unexpected argument : "
95
+	badarg_msglen: equ $ - badarg_msg
96
+
97
+	badval_msg: db "Value for -r should be in [1..8] but got "
98
+	badval_msglen: equ $ - badval_msg
99
+
84
 	hours: db "000000000"
100
 	hours: db "000000000"
85
 	timestr: db ":00:00.0           ", 0x0a
101
 	timestr: db ":00:00.0           ", 0x0a
86
 	timestrlen: equ $ - timestr
102
 	timestrlen: equ $ - timestr
87
 
103
 
88
-	time_res: dq 3 ; 3 digits bellow seconds
104
+	time_res: dq 4 ; 4 digits bellow seconds
89
 
105
 
90
 	nl: db 0x0A
106
 	nl: db 0x0A
91
 	buf: db 0
107
 	buf: db 0
103
 global _start
119
 global _start
104
 _start:
120
 _start:
105
 
121
 
122
+; parse arguments and set time_res value
123
+jmp arg_parse
124
+arg_ok:
125
+
106
 ; set stdin non blocking
126
 ; set stdin non blocking
107
 xor rdx, rdx
127
 xor rdx, rdx
108
 xor rdi, rdi
128
 xor rdi, rdi
432
 	xor rdi, rdi
452
 	xor rdi, rdi
433
 	syscall
453
 	syscall
434
 
454
 
455
+;
456
+;	Argument parsing
457
+;
458
+arg_parse:
459
+	; Checking argument count
460
+	mov rax, [rsp]
461
+	cmp rax, 3
462
+	je parse_r
463
+	cmp rax, 1
464
+	je arg_ok
465
+	; If 1 argument only it can only be -h
466
+	arg_err: ; badval & badarg jmp here too
467
+		mov rax, [rsp+8] ; argv[0] program name
468
+		mov rdi, -1 ; return status
469
+		jmp usage
470
+	parse_r:
471
+	mov rax, [rsp+16] ; 1st arg should be "-r"
472
+	mov bl, [rax]
473
+	cmp bl, '-'
474
+	jne badarg
475
+	mov bl, [rax+1]
476
+	cmp bl, 'h' ; -h given with another argument
477
+	je arg_err
478
+	cmp bl, 'r'
479
+	jne badarg
480
+	mov bl, [rax+2]
481
+	cmp bl, 0
482
+	; the 1st argument is -r the second must be a 1 digit integer in [1..8]
483
+	mov rax, [rsp+24]
484
+	xor rbx, rbx
485
+	mov bl, [rax]
486
+	cmp bl, '1'
487
+	jl badval
488
+	cmp bl, '8'
489
+	jg badval
490
+	sub bl, '0'
491
+	dbg1:
492
+	mov [time_res], rbx
493
+	jmp arg_ok
494
+
495
+badval: ; print an error message, usage and exit
496
+	mov rsi, badval_msg
497
+	mov rdx, badval_msglen
498
+	mov rax, [rsp+24]
499
+	jmp arg_strerr
500
+
501
+badarg: ; print an error message, usage and exit
502
+	mov rsi, badarg_msg
503
+	mov rdx, badarg_msglen
504
+	mov rax, [rsp+16]
505
+	jmp arg_strerr
506
+
507
+arg_strerr: ; rsi msg ptr, rdx msg len, rax arg ptr
508
+	mov r8, rax
509
+	mov rax, 1
510
+	mov rdi, 1
511
+	syscall
512
+	mov rsi, r8
513
+	mov rax, r8
514
+	call strlen
515
+	mov rax, 1
516
+	syscall
517
+	mov rax, 1
518
+	mov rsi, nl
519
+	mov rdx, 1
520
+	syscall
521
+	syscall
522
+	jmp arg_err
523
+
524
+;
525
+; Print usage and exit
526
+; 	Except rax to point on programm name and rdi to be the return code
527
+;
528
+usage:
529
+	push rdi
530
+	push rax
531
+
532
+	mov rax, 1 ; write
533
+	mov rdi, 1 ; stdout
534
+	mov rsi, usage_pre
535
+	mov rdx, usage_prelen
536
+	syscall
537
+
538
+	pop rsi
539
+	mov rax, rsi
540
+	call strlen
541
+	mov rax, 1
542
+	syscall
543
+
544
+	mov rax, 1
545
+	mov rsi, usage_post
546
+	mov rdx, usage_postlen
547
+	syscall
548
+
549
+	mov rax, 60
550
+	pop rdi
551
+	syscall
552
+
553
+; With rax pointing on the string, the result will be in rdx
554
+strlen:
555
+	mov r8, rax
556
+	mov r9, r8
557
+	sub r9, 1
558
+	strlen_loop:
559
+		add r9, 1
560
+		mov al, [r9]
561
+		cmp al, 0
562
+		jne strlen_loop
563
+	sub r9, r8
564
+	mov rdx, r9
565
+	ret
566
+
435
 .end:
567
 .end:

Loading…
Cancel
Save