diff --git a/fasm/lib/itoa.inc b/fasm/lib/itoa.inc index af315c6..de3293f 100644 --- a/fasm/lib/itoa.inc +++ b/fasm/lib/itoa.inc @@ -1,58 +1,49 @@ ;; rax contains the integer ;; rbx contains a pointer to the string buffer itoa: - mov byte [rbx], '0' - ;; Special case for the number '0' cmp rax, 0 jne .not_zero + mov byte [rbx], '0' mov byte [rbx + 1], 0 ret .not_zero: - ;; Fill 6 digits with zeroes - mov byte [rbx + 1], '0' - mov byte [rbx + 2], '0' - mov byte [rbx + 3], '0' - mov byte [rbx + 4], '0' - mov byte [rbx + 5], '0' + mov rdx, 0 + mov rcx, 100000 + div rcx + add rax, '0' + mov [rbx], al - sub rax, 100000 - inc byte [rbx] - cmp rax, 0 - jge itoa - add rax, 100000 - dec byte [rbx] -.ten_thousand: - sub rax, 10000 - inc byte [rbx + 1] - cmp rax, 0 - jge .ten_thousand - add rax, 10000 - dec byte [rbx + 1] -.one_thousand: - sub rax, 1000 - inc byte [rbx + 2] - cmp rax, 0 - jge .one_thousand - add rax, 1000 - dec byte [rbx + 2] -.one_hundred: - sub rax, 100 - inc byte [rbx + 3] - cmp rax, 0 - jge .one_hundred - add rax, 100 - dec byte [rbx + 3] -.ten: - sub rax, 10 - inc byte [rbx + 4] - cmp rax, 0 - jge .ten - add rax, 10 - dec byte [rbx + 4] - add [rbx + 5], rax + mov rax, rdx + mov rdx, 0 + mov rcx, 10000 + div rcx + add rax, '0' + mov [rbx + 1], al + mov rax, rdx + mov rdx, 0 + mov rcx, 1000 + div rcx + add rax, '0' + mov [rbx + 2], al + + mov rax, rdx + mov rdx, 0 + mov rcx, 100 + div rcx + add rax, '0' + mov [rbx + 3], al + + mov rax, rdx + mov rdx, 0 + mov rcx, 10 + div rcx + add rax, '0' + mov byte [rbx + 4], al + add dl, '0' + mov byte [rbx + 5], dl ;; Time to shift everything left ;; End the current string with a null byte