SIGINT now trigger new lap on stdout

This commit is contained in:
Yann Weber 2018-08-21 16:06:50 +02:00
commit 5fda46d4e9

View file

@ -1,22 +1,27 @@
;WTFStopW : a simple stopwatch
;Copyright (C) 2018 Weber Yann
;
;This program is free software; you can redistribute it and/or modify
;it under the terms of the GNU General Public License as published by
;the Free Software Foundation; either version 3 of the License, or
;any later version.
;
;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;GNU General Public License for more details.
;
;You should have received a copy of the GNU General Public License
;along with this program. If not, see <http://www.gnu.org/licenses/>.
; WTFStopW : a simple stopwatch
; Copyright (C) 2018 Weber Yann
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 3 of the License, or
; any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
;
; A simple precise stopwatch
; Build with :
; nasm -felf64 wtfstopw.asm && ld wtfstopw.o -o wtfstopw
; Build : nasm -felf64 wtfstopw.asm && ld wtfstopw.o -o wtfstopw
;
; Usage : ./wtfstopw
; press enter to exit
; send SIGINT (with kill -2 or ctrl + c) for new lap on stdout
;
[bits 64]
@ -76,13 +81,14 @@ section .data
msglen: equ $ - msg
hours: db "000000000"
timestr: db ":00:00.0000 ", 0x0D
timestr: db ":00:00.0000 ", 0x0a
timestrlen: equ $ - timestr
nl: db 0x0A
buf: db 0
dbgmsg: db "SigINT", 0xa
lapsmsg: db 0x0d, "New lap : "
lapsmsglen: equ $ - lapsmsg
section .text
global _start
@ -96,7 +102,6 @@ mov rsi, 3 ; F_GETFL
syscall
mov rdx, rax
or rdx, 0x800 ; O_NONBLOCK
dbg:
mov rax, 72 ; fcntl
mov rsi, 4 ; F_SETFL
syscall
@ -126,8 +131,75 @@ mov rdi, 0 ; CLOCK_REALTIME
mov rsi, ts_start
syscall
main_loop:
push 2 ; stderr
push 0x0D ; \r
call print_time
; Attempt to read from stdin
; if something read, enter has been pressed
mov rax, 0
mov rdi, 0
mov rsi, buf
mov rdx, 1
syscall
cmp rax, 0
jge flush_stdin ; flush stdin and exit
mov rax, 35 ; nanosleep
mov rdi, ts_sleep
mov rsi, 0
syscall
jmp main_loop
flush_stdin:
mov rax, 0
mov rdi, 0
mov rsi, buf
mov rdx, 1
syscall
cmp rax, 0
je newline_exit
jg flush_stdin
exit:
mov rax, 60 ; sys_exit
mov rdi, 0 ; OK
syscall
fault:
mov rax, 1 ; write
mov rdi, 2 ; stderr
mov rsi, nl
mov rdx, 1
syscall
mov rax, 1
mov rsi, faultmsg
mov rdx, faultmsglen
syscall
mov rax, 60 ; sys_exit
mov rdi, 1 ; failure
syscall
newline_exit:
mov rax, 1
mov rdi, 1
mov rsi, nl
mov rdx, 1
syscall
jmp exit
;
; Print current time on FD r10 and put r13b as leading char
;
print_time:
pop r8
pop r9
pop r10
push r8
push r10
push r9
; updating ts_cur time
mov rax, 228 ; clock_gettime
mov rdi, 0 ; CLOCK_REALTIME
@ -232,76 +304,32 @@ print_time:
sub rcx, r8 ; rcx is hours size
add rcx, timestrlen ; add to timestrlen
; Set leading char
pop r10
mov byte [timestr+timestrlen-1], r10b
mov rax, 1 ; write
mov rdi, 1 ; stdout
pop r10 ; print_time FD arg
mov rdi, r10 ; print_time arg
mov rsi, r9 ; start hours pointer
mov rdx, rcx ; size to timestr end
syscall
; Attempt to read from stdin
; if something read, enter has been pressed
mov rax, 0
mov rdi, 0
mov rsi, buf
mov rdx, 1
syscall
cmp rax, 0
jge flush_stdin ; flush stdin and exit
ret
mov rax, 35 ; nanosleep
mov rdi, ts_sleep
mov rsi, 0
syscall
jmp print_time ; main loop
flush_stdin:
mov rax, 0
mov rdi, 0
mov rsi, buf
mov rdx, 1
syscall
cmp rax, 0
je newline_exit
jg flush_stdin
exit:
mov rax, 60 ; sys_exit
mov rdi, 0 ; OK
syscall
fault:
mov rax, 1 ; write
mov rdi, 2 ; stderr
mov rsi, nl
mov rdx, 1
syscall
mov rax, 1
mov rsi, faultmsg
mov rdx, faultmsglen
syscall
mov rax, 60 ; sys_exit
mov rdi, 1 ; failure
syscall
newline_exit:
mov rax, 1
mov rdi, 1
mov rsi, nl
mov rdx, 1
syscall
jmp exit
lap_handler:
pop r8
mov rax, 1
mov rdi, 1
mov rsi, dbgmsg
mov rdx, 7
mov rsi, lapsmsg
mov rdx, lapsmsglen
syscall
push r8 ; push back ret addr
push 1 ; stdout
push 0x0A ; \n
call print_time
dbg2:
ret
sig_restorer: