From 5091c3a0d5d2e3c2b7cd34a284457b58cc532ffe Mon Sep 17 00:00:00 2001 From: Yann Weber Date: Tue, 21 Aug 2018 17:51:38 +0200 Subject: [PATCH] Add a lap counter --- wtfstopw.asm | 65 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/wtfstopw.asm b/wtfstopw.asm index 7529a43..a4b841d 100644 --- a/wtfstopw.asm +++ b/wtfstopw.asm @@ -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: