Add a lap counter

This commit is contained in:
Yann Weber 2018-08-21 17:51:38 +02:00
commit 5091c3a0d5

View file

@ -87,9 +87,13 @@ section .data
nl: db 0x0A
buf: db 0
lapsmsg: db 0x0d, "New lap : "
lapsmsg: db 0x0d, "Lap : "
lapsmsglen: equ $ - lapsmsg
lapcount: db "00000000"
lapcountlen: equ $ - lapcount
laplen: dq 2
section .text
global _start
_start:
@ -108,8 +112,10 @@ syscall
cmp rax, 0
jne fault
; initializing lapptr
; preparing SIGINT catch
mov rax, lap_handler
mov rax, proc_lap_handler
mov qword [sigaction.sa_handler], rax
mov eax, 0x10000000 ; SA_RESTART
or eax, 0x04000000 ; SA_RESTORER
@ -134,7 +140,7 @@ syscall
main_loop:
push 2 ; stderr
push 0x0D ; \r
call print_time
call proc_print_time
; Attempt to read from stdin
; if something read, enter has been pressed
@ -193,7 +199,7 @@ newline_exit:
;
; Print current time on FD r10 and put r13b as leading char
;
print_time:
proc_print_time:
pop r8
pop r9
pop r10
@ -317,19 +323,60 @@ print_time:
ret
lap_handler:
;
; sig handler for SIGINT displaying lap count and time on stdout
;
proc_lap_handler:
mov rax, 1
mov rdi, 1
mov rsi, lapsmsg
mov rdx, lapsmsglen
mov rdx, 5 ; "Lap "
syscall
; increment the lapcount str directly
mov rbx, lapcount ; first digit ptr
add rbx, lapcountlen ; rightmost digit ptr
sub rbx, 1
mov r8, 1 ; counter
lap_handler_inc_lap:
mov r10b, [rbx]
cmp r10b, 0x39 ; '9'
jl lap_handler_inc_end
add r8, 1
cmp r8, [laplen]
jl lap_handler_laplen_noupd
mov [laplen], r8 ; update laplen
lap_handler_laplen_noupd:
mov byte [rbx], 0x30 ; set current digit to '0'
sub rbx, 1
cmp rbx, lapcount
jl fault
jmp lap_handler_inc_lap
lap_handler_inc_end:
add r10b, 1
mov [rbx], r10b
mov rax, 1
mov rdi, 1
mov rdx, [laplen]
mov rsi, lapcount
add rsi, lapcountlen
sub rsi, rdx ; leftmost digit ptr
syscall
mov rax, 1
mov rdi, 1
mov rsi, lapsmsg + 4
mov rdx, 3 ; " : "
syscall
push 1 ; stdout
push 0x0A ; \n
call print_time
call proc_print_time
dbg2:
ret
sig_restorer: