;; rax contains the integer ;; rbx contains a pointer to the string buffer itoa: ;; Fill 6 digits with zeroes mov byte [rbx], '0' 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' ;; Special case for the number '0' cmp rax, 0 jne .not_zero mov byte [rbx + 1], 0 ret .not_zero: 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 ;; Time to shift everything left ;; End the current string with a null byte mov byte [rbx + 6], 0 .shift_left_loop: cmp byte [rbx], '0' jne .done mov rcx, rbx inc rcx mov rdx, [rcx] and rdx, 255 mov [rbx], dx inc rcx mov rdx, [rcx] and rdx, 255 mov [rbx + 1], dx inc rcx mov rdx, [rcx] and rdx, 255 mov [rbx + 2], dx inc rcx mov rdx, [rcx] and rdx, 255 mov [rbx + 3], dx inc rcx mov rdx, [rcx] and rdx, 255 mov [rbx + 4], dx inc rcx mov rdx, [rcx] and rdx, 255 mov [rbx + 5], dx jmp .shift_left_loop .done: ret